diff options
author | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 19:12:36 -0500 |
---|---|---|
committer | Kohei Yoshida <kyoshida@novell.com> | 2009-12-23 19:12:36 -0500 |
commit | fd36788f96b9a102fd56dd537e36329bc96458fc (patch) | |
tree | ff0f43576d8ded1cf370919616ce900d1a10d111 | |
parent | 0ab5dd74d986ec69db3d6ea8ccea1908601fd338 (diff) |
Cleaned up the main() function a bit.
-rwxr-xr-x | ooo-help-parser.py | 73 | ||||
-rw-r--r-- | source/docbook.py | 13 | ||||
-rw-r--r-- | source/tree.py | 2 |
3 files changed, 54 insertions, 34 deletions
diff --git a/ooo-help-parser.py b/ooo-help-parser.py index 0525156..0a297b6 100755 --- a/ooo-help-parser.py +++ b/ooo-help-parser.py @@ -36,31 +36,11 @@ def processTreeFiles (tree_dir): # Build document tree. builder = tree.TreeBuilder(rootNodes) builder.build() + return builder.root - -def main (): - parser = optparse.OptionParser() - parser.set_defaults(output=None) - parser.add_option("-o", "--output", dest="output", help="write output to FILE", metavar="FILE") - parser.add_option("-t", "--tree-dir", dest="tree_dir", help="Directory where the tree files are located. Tree files are expected to have .tree extension.") - parser.add_option("--no-convert", action="store_false", dest="convert", - help="Don't convert to docbook but simply output the parsed raw xhp structure", default=True) - options, args = parser.parse_args() - if len(args) == 0: - parser.print_help() - sys.exit(1) - - if options.tree_dir == None: - globals.error("Tree file directory is not provided.") - parser.print_help() - sys.exit(1) - - # Process the tree files first - processTreeFiles(options.tree_dir) - sys.exit(0) - +def walkDirs (fpaths): filepaths = [] - for fpath in args: + for fpath in fpaths: if os.path.isdir(fpath): # get all .xhp files recursively under this directory. for root, dirs, files in os.walk(fpath): @@ -71,13 +51,9 @@ def main (): if os.path.splitext(fpath)[1] == '.xhp': filepaths.append(os.path.abspath(fpath)) - fd = sys.stdout - if options.output != None: - if os.path.isdir(options.output): - globals.error("cannot create output file: " + optiont.output) - sys.exit(1) - fd = open(options.output, 'w') + return filepaths +def parseAllXHPFiles (filepaths): filesParsed = 0 rootNodes = {} for fpath in filepaths: @@ -94,14 +70,47 @@ def main (): p.filename = p.filename[1:] rootNodes[p.filename] = p.root filesParsed += 1 + return rootNodes, filesParsed + +def main (): + parser = optparse.OptionParser() + parser.set_defaults(output=None) + parser.add_option("-o", "--output", dest="output", help="write output to FILE", metavar="FILE") + parser.add_option("-t", "--tree-dir", dest="tree_dir", help="Directory where the tree files are located. Tree files are expected to have .tree extension.") + parser.add_option("--no-convert", action="store_false", dest="convert", + help="Don't convert to docbook but simply output the parsed raw xhp structure", default=True) + options, args = parser.parse_args() + if len(args) == 0: + parser.print_help() + sys.exit(1) + + if options.tree_dir == None: + globals.error("Tree file directory is not provided.") + parser.print_help() + sys.exit(1) + + # Process the tree files first + treeroot = processTreeFiles(options.tree_dir) + + # walk all directories and collect all files. + filepaths = walkDirs(args) + + fd = sys.stdout + if options.output != None: + if os.path.isdir(options.output): + globals.error("cannot create output file: " + optiont.output) + sys.exit(1) + fd = open(options.output, 'w') + + xhproots, filesParsed = parseAllXHPFiles(filepaths) if options.convert: - converter = docbook.DocBookConverter(rootNodes) + converter = docbook.DocBookConverter(treeroot, xhproots) converter.convert() converter.prettyPrint(fd) else: - for filename in rootNodes.keys(): - node.prettyPrint(fd, rootNodes[filename]) + for filename in xhproots.keys(): + node.prettyPrint(fd, xhproots[filename]) globals.info("%d files have been processed"%filesParsed) if fd != sys.stdout: diff --git a/source/docbook.py b/source/docbook.py index 34f5f25..cf2d648 100644 --- a/source/docbook.py +++ b/source/docbook.py @@ -100,7 +100,20 @@ class FilePathSorter: for child in _node.getChildNodes(): self.__walkToContent(child) + class DocBookConverter: + def __init__ (self, treeroot, xhproots): + self.treeroot = treeroot + self.xhproots = xhproots + self.root = node.Root() + + def convert (self): + pass + + def prettyPrint (self, fd): + node.prettyPrint(fd, self.root) + +class DocBookConverterOld: def __init__ (self, xhproots): self.__xhproots = xhproots self.root = node.Root() diff --git a/source/tree.py b/source/tree.py index b859e6e..f39a626 100644 --- a/source/tree.py +++ b/source/tree.py @@ -24,8 +24,6 @@ class TreeBuilder: for child in helpsection.getChildNodes(): self.__walkNode(child, elem) - node.prettyPrint(sys.stdout, self.root) - def __walkNode (self, src, dest): if src.nodeType != node.NodeType.Element: return |