Bootstrap child type casting

This commit is contained in:
clerie 2022-05-01 23:55:18 +02:00
parent 0e19927610
commit bd68cbd7d8
4 changed files with 136 additions and 2 deletions

View File

@ -139,7 +139,7 @@ class OMMClient2:
yield user
# Determine next possible ppn
next_uid = int(user["uid"]) + 1
next_uid = int(user.uid) + 1
def ping(self):
"""

View File

@ -3,6 +3,7 @@
from xml.dom.minidom import getDOMImplementation, parseString
from ..exceptions import exception_classes, OMResponseException
from ..types import cast_dict_to_childtype
class Message:
@ -74,6 +75,9 @@ class Message:
else:
object.__setattr__(self, name, value)
def __repr__(self):
return "{}({}, {}, {})".format(self.__class__.__name__, self.name, repr(self._attrs), repr(self._childs))
class Request(Message):
"""
@ -179,6 +183,11 @@ def parse(message):
new_child[item.name] = item.value
childname = child.tagName
# cast dict into child type
if response_type.CHILDS.get(childname) is not None:
new_child = cast_dict_to_childtype(response_type.CHILDS[childname], new_child)
if childname in childs:
childs[childname].append(new_child)
else:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3
from . import Request, Response, request_type, response_type
from ..types import PPUserType
@request_type
@ -14,5 +15,5 @@ class GetPPUser(Request):
@response_type
class GetPPUserResp(Response):
CHILDS = {
"user": None,
"user": PPUserType,
}

View File

@ -0,0 +1,124 @@
#!/usr/bin/env python3
class ChildType:
"""
Base type class
:param name: Name of the message
:param attrs: Message attributes
:param childs: Message children
"""
FIELDS = {}
def __init__(self, attrs={}):
self._attrs = {}
for k, v in attrs.items():
setattr(self, k, v)
def __getattr__(self, name):
if name in self.FIELDS.keys():
return self._attrs.get(name)
else:
raise AttributeError()
def __setattr__(self, name, value):
if name in self.FIELDS.keys():
if self.FIELDS[name] is not None and type(value) != self.FIELDS[name]:
raise TypeError()
self._attrs[name] = value
else:
object.__setattr__(self, name, value)
def __repr__(self):
return "{}({})".format(self.__class__.__name__, repr(self._attrs))
def cast_dict_to_childtype(t, d):
for k, v in d.items():
if k in t.FIELDS.keys():
if t.FIELDS[k] is not None and type(v) != t.FIELDS[k]:
d[k] = t.FIELDS[k](v)
else:
raise KeyError()
return t(d)
class PPUserType(ChildType):
FIELDS = {
"uid": int,
"timeStamp": int,
"relType": None, #PPRelTypeType,
"ppn": int,
"name": str,
"num": str,
"hierarchy1": str,
"hierarchy2": str,
"addId": str,
"pin": str,
"sipAuthId": str,
"sipPw": str,
"sosNum": str,
"voiceboxNum": str,
"manDownNum": str,
"forwardStateCall": None, #ForwardStateType,
"forwardTime": int,
"forwardDest": str,
"langPP": None, #LanguageType,
"holdRingBackTime": int,
"autoAnswer": str,
"microphoneMute": str,
"warningTone": str,
"allowBargeIn": str,
"callWaitingDisabled": bool,
"external": bool,
"trackingActive": bool,
"locatable": bool,
"BTlocatable": bool,
"BTsensitivity": str,
"locRight": bool,
"msgRight": bool,
"sendVcardRight": bool,
"recvVcardRight": bool,
"keepLocalPB": bool,
"vip": bool,
"sipRegisterCheck": bool,
"allowVideoStream": bool,
"conferenceServerType": str,
"conferenceServerURI": str,
"monitoringMode": str,
"CUS": None, #MonitoringStateType,
"HAS": None, #MonitoringStateType,
"HSS": None, #MonitoringStateType,
"HRS": None, #MonitoringStateType,
"HCS": None, #MonitoringStateType,
"SRS": None, #MonitoringStateType,
"SCS": None, #MonitoringStateType,
"CDS": None, #MonitoringStateType,
"HBS": None, #MonitoringStateType,
"BTS": None, #MonitoringStateType,
"SWS": None, #MonitoringStateType,
"credentialPw": str,
"configurationDataLoaded": bool,
"ppData": str,
"ppProfileId": int,
"fixedSipPort": int,
"calculatedSipPort": int,
# undocumented
"uidSec": int,
"permanent": bool,
"lang": None,
"forwardState": None,
"autoLogoutOnCharge": bool,
"hotDeskingSupport": bool,
"authenticateLogout": bool,
"useSIPUserName": None,
"useSIPUserAuthentication": None,
"serviceUserName": None,
"serviceAuthName": None,
"serviceAuthPassword": None,
"keyLockEnable": None,
"keyLockPin": None,
"keyLockTime": None,
}