|
| 1 | +#!/usr/bin/env python |
| 2 | +"""usage: %prog [options] filename |
| 3 | +
|
| 4 | +Parse a document to a simpletree tree, with optional profiling |
| 5 | +""" |
| 6 | +#RELEASE move ./examples/ |
| 7 | + |
| 8 | +import sys |
| 9 | +import os |
| 10 | +from optparse import OptionParser |
| 11 | + |
| 12 | +#RELEASE remove |
| 13 | +from src import html5parser, liberalxmlparser |
| 14 | +from src import treebuilders, serializer, treewalkers |
| 15 | +#END RELEASE |
| 16 | +#RELEASE add |
| 17 | +#from html5lib import html5parser, liberalxmlparser |
| 18 | +#from html5lib import treebuilders, serializer, treewalkers |
| 19 | +#END RELEASE |
| 20 | + |
| 21 | +def parse(): |
| 22 | + optParser = getOptParser() |
| 23 | + opts,args = optParser.parse_args() |
| 24 | + |
| 25 | + try: |
| 26 | + f = args[-1] |
| 27 | + # Try opening from the internet |
| 28 | + if f.startswith('http://'): |
| 29 | + try: |
| 30 | + import urllib |
| 31 | + f = urllib.urlopen(f).read() |
| 32 | + except: pass |
| 33 | + else: |
| 34 | + try: |
| 35 | + # Try opening from file system |
| 36 | + f = open(f) |
| 37 | + except IOError: pass |
| 38 | + except IndexError: |
| 39 | + sys.stderr.write("No filename provided. Use -h for help\n") |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + treebuilder = treebuilders.getTreeBuilder(opts.treebuilder) |
| 43 | + |
| 44 | + if opts.xml: |
| 45 | + p = liberalxmlparser.XHTMLParser(tree=treebuilder) |
| 46 | + else: |
| 47 | + p = html5parser.HTMLParser(tree=treebuilder) |
| 48 | + |
| 49 | + if opts.profile: |
| 50 | + import hotshot |
| 51 | + import hotshot.stats |
| 52 | + prof = hotshot.Profile('stats.prof') |
| 53 | + prof.runcall(p.parse, f) |
| 54 | + prof.close() |
| 55 | + # XXX - We should use a temp file here |
| 56 | + stats = hotshot.stats.load('stats.prof') |
| 57 | + stats.strip_dirs() |
| 58 | + stats.sort_stats('time') |
| 59 | + stats.print_stats() |
| 60 | + elif opts.time: |
| 61 | + import time |
| 62 | + t0 = time.time() |
| 63 | + document = p.parse(f) |
| 64 | + t1 = time.time() |
| 65 | + printOutput(p, document, opts) |
| 66 | + t2 = time.time() |
| 67 | + sys.stdout.write("\n\nRun took: %fs (plus %fs to print the output)"%(t1-t0, t2-t1)) |
| 68 | + else: |
| 69 | + document = p.parse(f) |
| 70 | + printOutput(p, document, opts) |
| 71 | + |
| 72 | +def printOutput(parser, document, opts): |
| 73 | + if opts.encoding: |
| 74 | + print "Encoding:", parser.tokenizer.stream.charEncoding |
| 75 | + if not opts.no_tree: |
| 76 | + if opts.xml: |
| 77 | + sys.stdout.write(document.toxml("utf-8")) |
| 78 | + elif opts.html: |
| 79 | + tokens = treewalkers.getTreeWalker(opts.treebuilder)(document) |
| 80 | + for text in serializer.HTMLSerializer().serialize(tokens, encoding='utf-8'): |
| 81 | + sys.stdout.write(text) |
| 82 | + elif opts.hilite: |
| 83 | + sys.stdout.write(document.hilite("utf-8")) |
| 84 | + else: |
| 85 | + sys.stdout.write(parser.tree.testSerializer(document).encode("utf-8")) |
| 86 | + if opts.error: |
| 87 | + errList=[] |
| 88 | + for pos, message in parser.errors: |
| 89 | + errList.append("Line %i Col %i"%pos + " " + message) |
| 90 | + sys.stderr.write("\nParse errors:\n" + "\n".join(errList)+"\n") |
| 91 | + |
| 92 | +def getOptParser(): |
| 93 | + parser = OptionParser(usage=__doc__) |
| 94 | + |
| 95 | + parser.add_option("-p", "--profile", action="store_true", default=False, |
| 96 | + dest="profile", help="Use the hotshot profiler to " |
| 97 | + "produce a detailed log of the run") |
| 98 | + |
| 99 | + parser.add_option("-t", "--time", |
| 100 | + action="store_true", default=False, dest="time", |
| 101 | + help="Time the run using time.time (may not be accurate on all platforms, especially for short runs)") |
| 102 | + |
| 103 | + parser.add_option("", "--no-tree", action="store_true", default=False, |
| 104 | + dest="no_tree", help="Do not print output tree") |
| 105 | + |
| 106 | + parser.add_option("-b", "--treebuilder", action="store", type="string", |
| 107 | + dest="treebuilder", default="simpleTree") |
| 108 | + |
| 109 | + parser.add_option("-e", "--error", action="store_true", default=False, |
| 110 | + dest="error", help="Print a list of parse errors") |
| 111 | + |
| 112 | + parser.add_option("-x", "--xml", action="store_true", default=False, |
| 113 | + dest="xml", help="Output as xml") |
| 114 | + |
| 115 | + parser.add_option("", "--html", action="store_true", default=False, |
| 116 | + dest="html", help="Output as html") |
| 117 | + |
| 118 | + parser.add_option("", "--hilite", action="store_true", default=False, |
| 119 | + dest="hilite", help="Output as formatted highlighted code.") |
| 120 | + |
| 121 | + parser.add_option("-c", "--encoding", action="store_true", default=False, |
| 122 | + dest="encoding", help="Print character encoding used") |
| 123 | + return parser |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + parse() |
0 commit comments