Skip to content

Commit

Permalink
fix: second attempt to handle abstract classes correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelm committed Apr 11, 2022
1 parent 84abaa2 commit 63f1454
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
46 changes: 25 additions & 21 deletions broadworks_ocip/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Base classes used by the types, requests and responses as well as
other components like ElementInfo that are used by those.
"""
import logging
import re
from collections import namedtuple
from typing import Any
Expand All @@ -21,6 +22,9 @@
from broadworks_ocip.exceptions import OCIErrorUnexpectedAttribute


logger = logging.getLogger(__name__)


@attr.s(slots=True, frozen=True)
class ElementInfo:
"""
Expand Down Expand Up @@ -181,6 +185,13 @@ def etree_sub_components_(self, element: etree._Element) -> etree._Element:
if value is not None:
for subvalue in value:
self.etree_sub_element_(element, sub_element, subvalue)
elif sub_element.is_abstract:
if value is not None:
# we pull the sub elements out of the abstract instance
# we do not support multi level abstracts - ths is not the matrix
for sub_sub_element in value._elements():
sub_value = getattr(value, sub_sub_element.name)
self.etree_sub_element_(element, sub_sub_element, sub_value)
else:
self.etree_sub_element_(element, sub_element, value)
return element
Expand Down Expand Up @@ -228,19 +239,11 @@ def etree_sub_element_(
col_item = etree.SubElement(row_item, "col")
col_item.text = col
elif sub_element.is_complex:
if sub_element.is_abstract:
elem_name = self.class_to_property_(value.type_)
elem = etree.SubElement(
element,
elem_name,
nsmap=self._default_nsmap(),
)
else:
elem = etree.SubElement(
element,
sub_element.xmlname,
nsmap=self._default_nsmap(),
)
elem = etree.SubElement(
element,
sub_element.xmlname,
nsmap=self._default_nsmap(),
)
value.etree_sub_components_(elem)
else:
elem = etree.SubElement(
Expand Down Expand Up @@ -369,14 +372,15 @@ def build_from_etree_(cls, element: etree._Element, extras: Dict[str, Any] = {})
initialiser[elem.name] = result
else:
if elem.is_abstract:
for subclass in elem.type.__subclasses__():
elem_name = cls.class_to_property_(subclass.__name__)
node = element.find(elem_name)
if node is not None:
initialiser[elem.name] = subclass.build_from_node_(
elem=elem,
node=node,
)
logger.error("XML decode of abstract property not yet supported")
# for subclass in elem.type.__subclasses__():
# elem_name = cls.class_to_property_(subclass.__name__)
# node = element.find(elem_name)
# if node is not None:
# initialiser[elem.name] = subclass.build_from_node_(
# elem=elem,
# node=node,
# )
else:
node = element.find(elem.xmlname)
if node is not None:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_basic_command_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,8 @@ def test_user_modify_department_key_xml():
b'<sessionId xmlns="">00000000-1111-2222-3333-444444444444</sessionId>'
b'<command xmlns="" xsi:type="UserModifyRequest16">'
b"<userId>fred.flintstone@boulder.org</userId>"
b"<enterpriseDepartmentKey>"
b"<serviceProviderId>mysp</serviceProviderId>"
b"<name>mydept</name>"
b"</enterpriseDepartmentKey>"
b"</command>"
b"</BroadsoftDocument>"
),
Expand Down

0 comments on commit 63f1454

Please sign in to comment.