Newer
Older
# Copyright 2019 Dom Sekotill <dom.sekotill@kodo.org.uk>
#
# 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
_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))
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):
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
@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)