diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-20 01:55:19 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-20 01:55:19 -0500 |
commit | 9a86535b8dcde0f56c7ee40d6fc1941f386de037 (patch) | |
tree | a146c9591b1427f3ea20f7cbfcceadba99d70a59 |
First set of files.
-rwxr-xr-x | ooo-help-parser.py | 27 | ||||
-rw-r--r-- | source/expatimpl.py | 114 | ||||
-rw-r--r-- | source/globals.py | 9 |
3 files changed, 150 insertions, 0 deletions
diff --git a/ooo-help-parser.py b/ooo-help-parser.py new file mode 100755 index 0000000..62c20d3 --- /dev/null +++ b/ooo-help-parser.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import sys, os.path, optparse +sys.path.append(sys.path[0]+"/source") + +import globals, expatimpl + +def main (): + parser = optparse.OptionParser() + options, args = parser.parse_args() + if len(args) != 1: + globals.error("Takes exactly one argument") + parser.print_help() + sys.exit(1) + + if not os.path.isfile(args[0]): + globals.error(args[0] + " is not a valid file. Aborting.") + sys.exit(1) + + file = open(args[0], 'r') + strm = file.read() + file.close() + p = expatimpl.Parser(strm) + p.parse() + +if __name__ == '__main__': + main() diff --git a/source/expatimpl.py b/source/expatimpl.py new file mode 100644 index 0000000..2a2cdf5 --- /dev/null +++ b/source/expatimpl.py @@ -0,0 +1,114 @@ +import xml.parsers.expat +import globals + +class NodeType: + Unknown = 0 + Root = 1 + Node = 2 + Content = 3 + +class NodeBase: + def __init__ (self, nodeType = NodeType.Unknown): + self.children = [] + self.nodeType = nodeType + + def appendChild (self, node): + self.children.append(node) + + def setContent (self, content): + self.content = content + + def firstChild (self): + return self.children[0] + +class Root(NodeBase): + def __init__ (self): + NodeBase.__init__(self, NodeType.Root) + +class Content(NodeBase): + def __init__ (self, content): + NodeBase.__init__(self, NodeType.Content) + self.content = content + +class Node(NodeBase): + def __init__ (self, name, attrs={}): + NodeBase.__init__(self, NodeType.Node) + self.name = name + self.attrs = attrs + + +class Parser: + + class ParseFailed(globals.Exception): + def __init__ (self): + globals.Exception.__init__(self, "parse failed") + + def __init__ (self, strm): + self.strm = strm + self.root = Root() + self.nodestack = [self.root] + + def startElement(self, name, attrs): + n = Node(name, attrs) + self.nodestack[-1].appendChild(n) + self.nodestack.append(n) + + def endElement(self, name): + if name != self.nodestack[-1].name: + raise Parser.ParseFailed() + self.nodestack.pop() + + def character(self, data): + if len(data) > 0 and data[-1] == "\n": + data = data[:-1] + + s = repr(data) + if len(s) >= 3 and s[0] == 'u' and s[1] == "'" and s[-1] == "'": + # u'foo' -> foo + s = s[2:-1] + + self.nodestack[-1].appendChild(Content(s)) + + def parse (self): + p = xml.parsers.expat.ParserCreate() + p.StartElementHandler = self.startElement + p.EndElementHandler = self.endElement + p.CharacterDataHandler = self.character + p.Parse(self.strm, 1) + self.prettyPrint() + + def prettyPrint (self): + if len(self.root.children) != 1: + return + + node = self.root.firstChild() + self.printNode(node, 0) + + def printNode (self, node, level): + singleIndent = ' '*4 + indent = singleIndent*level + if node.nodeType == NodeType.Node: + hasChildren = len(node.children) > 0 + + line = "<%s"%node.name + if len(node.attrs) > 0: + keys = node.attrs.keys() + keys.sort() + for key in keys: + line += " " + key + '="' + node.attrs[key] + '"' + if hasChildren: + line += ">" + print (indent + line) + for child in node.children: + self.printNode(child, level+1) + line = "</%s>"%node.name + print (indent + line) + else: + line += "/>" + print (indent + line) + + + elif node.nodeType == NodeType.Content: + if len(node.content) > 0: + print (indent + node.content) + diff --git a/source/globals.py b/source/globals.py new file mode 100644 index 0000000..f7e6388 --- /dev/null +++ b/source/globals.py @@ -0,0 +1,9 @@ + +import sys + +def error (msg): + sys.stderr.write(msg + "\n") + +class Exception: + def __init__ (self, msg): + self.msg = msg |