# Copyright 2019-2021, 2024 Dom Sekotill # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Helpers for network configuration """ from enum import Enum from enum import auto from typing import Callable from typing import Dict from typing import Optional from typing import Type from typing import TypeVar from typing import overload T = TypeVar('T') ClassDecorator = Callable[[type], type] _registry: Dict[str, type] = {} @overload def register(variable: str, cls: type) -> None: ... @overload def register(variable: str) -> ClassDecorator: ... def register(variable: str, cls: Optional[type] = None) -> Optional[ClassDecorator]: """ Register a class as the type for a config option's values Will return a class decorator if 'cls' is not provided: @register('my_option') class MyType: ... """ if cls is not None: _registry[variable] = cls return None def decorator(cls: Type[T]) -> Type[T]: _registry[variable] = cls return cls return decorator def get_type(variable: str) -> type: """Get a type-converter for a given configuration variable""" return _registry.get(variable, UnknownType) class _UnknownTypeMeta(type): def __instancecheck__(cls, instance: object) -> bool: return isinstance(instance, (str, int)) class UnknownType(metaclass=_UnknownTypeMeta): """A pseudo type for unknown variables which forces non-ints into strings""" class ConfigEnum(Enum): """Base enum class for configuration enums used with the SET_NETWORK command""" def __str__(self) -> str: return str(self.value) @staticmethod def _generate_next_value_(name: str, start: object, count: object, last_values: object) -> str: return name.replace("_", "-") @register("key_mgmt") class KeyManagementValue(ConfigEnum): """Enums for the 'key_mgmt' network setting""" WPA_PSK = auto() WPA_EAP = auto() IEEE8021X = auto() NONE = auto() WPA_NONE = auto() FT_PSK = auto() FT_EAP = auto() FT_EAP_SHA384 = auto() WPA_PSK_SHA256 = auto() WPA_EAP_SHA256 = auto() SAE = auto() FT_SAE = auto() WPA_EAP_SUITE_B = auto() WPA_EAP_SUITE_B_192 = auto() OSEN = auto() FILS_SHA256 = auto() FILS_SHA384 = auto() FT_FILS_SHA256 = auto() FT_FILS_SHA384 = auto() OWE = auto() DPP = auto() register("priority", int) register("psk", str) register("ssid", str)