Refactor extensions parsing

This commit is contained in:
clerie 2022-09-02 21:51:55 +02:00
parent ba0d687b63
commit d72ff01372
3 changed files with 69 additions and 20 deletions

View File

@ -40,22 +40,3 @@ class Config:
def check(self):
return self.dect.check()
class ExtensionConfig(ConfigBase):
def __init__(self, c):
self.num = c[0]
self._c = c[1]
class Extensions:
def __init__(self, c):
self._c = c
self.extensions = []
for e in self._c.get("extensions", {}).items():
self.extensions.append(ExtensionConfig(e))
def extensions_by_type(self, t):
for e in self.extensions:
if e.type == t:
yield e

67
fieldpoc/extensions.py Normal file
View File

@ -0,0 +1,67 @@
#!/usr/bin/env python3
class ExtensionConfig:
num = None
callgroup_members = []
dect_claim_token = None
dect_ipei = None
dialout_allowed = False
name = None
outgoing_extension = None
sip_password = None
static_target = None
trunk = False
type = None
def __init__(self, **kwargs):
for name, value in kwargs.items():
if hasattr(type(self), name):
self.__setattr__(name, value)
else:
raise Exception("Invalid config option {}".format(name))
if self.type is None:
raise Exception("Type field for extension {} is required".format(self.num))
elif self.type == "dect":
if self.dect_ipei is None and self.dect_claim_token is None:
raise Exception("dect_ipei can't be used together with dect_claim_token in extension {}".format(self.num))
elif self.type == "sip":
if self.sip_password is None:
raise Exception("sip_password is required for sip extension {}".format(self.num))
elif self.type == "static":
if self.static_target is None:
raise Exception("static_target is required for static extension {}".format(self.num))
def check_global(self, extensions):
if self.type == "callgroup":
for member in self.callgroup_members:
if member not in extensions.extensions_by_num.keys():
raise Exception("Callgroup member {} of callgroup {} does not exist as extension".format(member, self.num))
class Extensions:
extensions = []
extensions_by_num = {}
def __init__(self, c):
for num, e in c.get("extensions", {}).items():
extension_config = ExtensionConfig(**{
"num": num,
} | e)
self.extensions.append(extension_config)
if extension_config.num in self.extensions_by_num.keys():
raise Exception("Extension num already used {}".format(num))
self.extensions_by_num[num] = extension_config
for e in self.extensions:
e.check_global(self)
def extensions_by_type(self, t):
for e in self.extensions:
if e.type == t:
yield e

View File

@ -8,6 +8,7 @@ import threading
from . import config
from . import controller
from . import extensions
from . import dect
from . import routing
from . import ywsd
@ -91,4 +92,4 @@ class FieldPOC:
self.config = config.Config(json.loads(self.config_file_path.read_text()))
def _load_extensions(self):
self.extensions = config.Extensions(json.loads(self.extensions_file_path.read_text()))
self.extensions = extensions.Extensions(json.loads(self.extensions_file_path.read_text()))