2008-01-07 17:40:34 +00:00
|
|
|
"""Small helper module to parse Plist-formatted data from trees as created
|
|
|
|
by xmlTreeBuilder.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = "readPlistFromTree"
|
|
|
|
|
|
|
|
|
2015-11-05 09:03:19 +00:00
|
|
|
from .plistlib import PlistParser
|
2008-01-07 17:40:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def readPlistFromTree(tree):
|
|
|
|
"""Given a (sub)tree created by xmlTreeBuilder, interpret it
|
|
|
|
as Plist-formatted data, and return the root object.
|
|
|
|
"""
|
|
|
|
parser = PlistTreeParser()
|
|
|
|
return parser.parseTree(tree)
|
|
|
|
|
|
|
|
|
|
|
|
class PlistTreeParser(PlistParser):
|
|
|
|
|
|
|
|
def parseTree(self, tree):
|
|
|
|
element, attributes, children = tree
|
|
|
|
self.parseElement(element, attributes, children)
|
|
|
|
return self.root
|
|
|
|
|
|
|
|
def parseElement(self, element, attributes, children):
|
|
|
|
self.handleBeginElement(element, attributes)
|
|
|
|
for child in children:
|
|
|
|
if isinstance(child, tuple):
|
|
|
|
self.parseElement(child[0], child[1], child[2])
|
|
|
|
else:
|
2015-11-05 09:03:19 +00:00
|
|
|
if not isinstance(child, str):
|
2008-01-07 17:40:34 +00:00
|
|
|
# ugh, xmlTreeBuilder returns utf-8 :-(
|
2015-11-05 09:03:19 +00:00
|
|
|
child = str(child, "utf-8")
|
2008-01-07 17:40:34 +00:00
|
|
|
self.handleData(child)
|
|
|
|
self.handleEndElement(element)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-11-05 09:03:19 +00:00
|
|
|
from .xmlTreeBuilder import buildTree
|
2008-01-07 17:40:34 +00:00
|
|
|
tree = buildTree("xxx.plist", stripData=0)
|
2015-11-05 09:03:19 +00:00
|
|
|
print(readPlistFromTree(tree))
|