1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
"""Small helper module to parse Plist-formatted data from trees as created
by xmlTreeBuilder.
"""
__all__ = "readPlistFromTree"
from plistlib import PlistParser
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:
if not isinstance(child, unicode):
# ugh, xmlTreeBuilder returns utf-8 :-(
child = unicode(child, "utf-8")
self.handleData(child)
self.handleEndElement(element)
if __name__ == "__main__":
from xmlTreeBuilder import buildTree
tree = buildTree("xxx.plist", stripData=0)
print readPlistFromTree(tree)
|