fonttools/Lib/ufoLib/plistFromTree.py
Tal Leming 45910b6131 Initial work on reading single file UFO.
This is a work in progress update of UFOReader and UFOWriter that
supports UFO in its package and zipped forms. Reading works. Writing is
not yet implemented.

I'm building a base file system (that lives on top of fs for now and
maybe in the long term) that the reader and writer then subclass. This
base class implements the file system interaction so that the reader
and writer can be blissfully ignorant about file systems.

Additionally, I ran into a problem with the local plistlib.py creating
an import error, so I've temporarily renamed it plistlibShim.py so that
I can continue working.

Did I mention that this is a work in progress? It's a work in progress.
2016-05-02 23:06:25 -04:00

39 lines
994 B
Python

from ufoLib.plistlibShim import PlistParser
"""
Small helper module to parse Plist-formatted data from trees as created
by xmlTreeBuilder.
"""
__all__ = "readPlistFromTree"
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:
self.handleData(child)
self.handleEndElement(element)
if __name__ == "__main__":
from ufoLib.xmlTreeBuilder import buildTree
tree = buildTree("xxx.plist", stripData=0)
print(readPlistFromTree(tree))