|
31 | 31 | import os |
32 | 32 | import sys |
33 | 33 |
|
34 | | -try: |
35 | | - import codecs |
36 | | - from datetime import datetime |
37 | | - from lxml import etree |
38 | | -except ImportError: |
39 | | - sys.exit(0) |
| 34 | +import codecs |
| 35 | +from datetime import datetime |
| 36 | +from xml.etree import ElementTree |
40 | 37 |
|
41 | 38 | INCLUDE = '{http://www.w3.org/2001/XInclude}include' |
42 | 39 | TITLE = '{http://projectmallard.org/1.0/}title' |
@@ -73,10 +70,22 @@ def __init__(self, inFile, outFile, section): |
73 | 70 | self.sections = [] |
74 | 71 |
|
75 | 72 | def _parse(self): |
76 | | - self.tree = etree.ElementTree() |
77 | | - self.tree.parse(file(self.inFile)) |
| 73 | + self.tree = ElementTree.ElementTree() |
| 74 | + self.tree.parse(open(self.inFile)) |
78 | 75 | self.root = self.tree.getroot() |
79 | 76 |
|
| 77 | + # Python's standard ElementTree doesn't store an element's parent on |
| 78 | + # the element. Make a child->parent map. |
| 79 | + try: |
| 80 | + iterator = self.tree.iter() |
| 81 | + except AttributeError: |
| 82 | + # Python 2.6. |
| 83 | + iterator = self.tree.getiterator() |
| 84 | + self.parent_map = dict((c, p) for p in iterator for c in p) |
| 85 | + |
| 86 | + def _get_parent(self, ele): |
| 87 | + return self.parent_map[ele] |
| 88 | + |
80 | 89 | def _extract(self): |
81 | 90 | # Try to extract the title. |
82 | 91 | for child in self.root.getchildren(): |
@@ -152,7 +161,7 @@ def _generateSynopsis(self, synopsis): |
152 | 161 | self._writeCommand('.fi') |
153 | 162 |
|
154 | 163 | def _generateCode(self, code): |
155 | | - if code.getparent().tag == P: |
| 164 | + if self._get_parent(code).tag == P: |
156 | 165 | self._writeCommand('.B %s' % code.text) |
157 | 166 | else: |
158 | 167 | self._writeCommand('.nf') |
@@ -240,7 +249,7 @@ def _generateElement(self, ele): |
240 | 249 | d = codecs.open(f, 'r', encoding='utf-8').read() |
241 | 250 | self._writeLine(d) |
242 | 251 | else: |
243 | | - print 'unknown element type', ele |
| 252 | + print('unknown element type %s' % ele) |
244 | 253 |
|
245 | 254 | def _generateTable(self, table): |
246 | 255 | for child in table.getchildren(): |
@@ -297,7 +306,7 @@ def main(filenames, section='3'): |
297 | 306 |
|
298 | 307 | if __name__ == '__main__': |
299 | 308 | if len(sys.argv) < 3: |
300 | | - print 'usage: %s SECTION FILENAMES...' % sys.argv[0] |
| 309 | + print('usage: %s SECTION FILENAMES...' % sys.argv[0]) |
301 | 310 | sys.exit(1) |
302 | 311 | section = sys.argv[1] |
303 | 312 | main(sys.argv[2:], section) |
|
0 commit comments