def test_totree(parametrized_pl): pl, use_builtin_types = parametrized_pl tree = etree.fromstring(TESTDATA)[0] # ignore root 'plist' element tree2 = plistlib.totree(pl, use_builtin_types=use_builtin_types) assert tree.tag == tree2.tag == "dict" for (_, e1), (_, e2) in zip(etree.iterwalk(tree), etree.iterwalk(tree2)): assert e1.tag == e2.tag assert e1.attrib == e2.attrib assert len(e1) == len(e2) # ignore whitespace assert _strip(e1.text) == _strip(e2.text)
def fromtree( tree: etree.Element, use_builtin_types: Optional[bool] = None, dict_type: Type[MutableMapping[str, Any]] = dict, ) -> Any: """Convert an XML tree to a plist structure. Args: tree: An ``etree`` ``Element``. use_builtin_types: If True, binary data is deserialized to bytes strings. If False, it is wrapped in :py:class:`Data` objects. Defaults to True if not provided. Deprecated. dict_type: What type to use for dictionaries. Returns: An object (usually a dictionary). """ target = PlistTarget(use_builtin_types=use_builtin_types, dict_type=dict_type) for action, element in etree.iterwalk(tree, events=("start", "end")): if action == "start": target.start(element.tag, element.attrib) elif action == "end": # if there are no children, parse the leaf's data if not len(element): # always pass str, not None target.data(element.text or "") target.end(element.tag) return target.close()
def fromtree(tree, use_builtin_types=None, dict_type=dict): target = PlistTarget(use_builtin_types=use_builtin_types, dict_type=dict_type) for action, element in etree.iterwalk(tree, events=("start", "end")): if action == "start": target.start(element.tag, element.attrib) elif action == "end": # if there are no children, parse the leaf's data if not len(element): # always pass str, not None target.data(element.text or "") target.end(element.tag) return target.close()