# Copyright 2019 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, auto _registry = {} def register(variable, cls=None): """ Register a class as the type for a config option's values Can be used as a decorator if 'cls' is not provided: @register('my_option') class MyType: ... """ if cls is not None: _registry[variable] = cls return None def decorator(cls): _registry[variable] = cls return cls return decorator def get_type(variable): """Get a type-converter for a given configuration variable""" return _registry.get(variable, UnknownType) class _UnknownTypeMeta(type): def __instancecheck__(cls, instance): return isinstance(instance, (str, int)) # pylint: disable=too-few-public-methods class UnknownType(metaclass=_UnknownTypeMeta): """A pseudo type for unknown variables which forces non-ints into strings""" def __new__(cls, from_instance): if not isinstance(from_instance, (str, int)): return str(from_instance) return from_instance class ConfigEnum(Enum): """Base enum class for configuration enums used with the SET_NETWORK command""" def __str__(self): return self.value @staticmethod def _generate_next_value_(name, *_): 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)