From bd68cbd7d8bdc8d057f0f2c7798cf040d5cc7b5f Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 1 May 2022 23:55:18 +0200 Subject: [PATCH] Bootstrap child type casting --- mitel_ommclient2/client.py | 2 +- mitel_ommclient2/messages/__init__.py | 9 ++ mitel_ommclient2/messages/getppuser.py | 3 +- mitel_ommclient2/types/__init__.py | 124 +++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 mitel_ommclient2/types/__init__.py diff --git a/mitel_ommclient2/client.py b/mitel_ommclient2/client.py index 98972a4..fe393f9 100644 --- a/mitel_ommclient2/client.py +++ b/mitel_ommclient2/client.py @@ -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): """ diff --git a/mitel_ommclient2/messages/__init__.py b/mitel_ommclient2/messages/__init__.py index 2e7e560..464b17d 100644 --- a/mitel_ommclient2/messages/__init__.py +++ b/mitel_ommclient2/messages/__init__.py @@ -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: diff --git a/mitel_ommclient2/messages/getppuser.py b/mitel_ommclient2/messages/getppuser.py index f06547a..cfa1f25 100644 --- a/mitel_ommclient2/messages/getppuser.py +++ b/mitel_ommclient2/messages/getppuser.py @@ -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, } diff --git a/mitel_ommclient2/types/__init__.py b/mitel_ommclient2/types/__init__.py new file mode 100644 index 0000000..9f33500 --- /dev/null +++ b/mitel_ommclient2/types/__init__.py @@ -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, + }