Package: python-docutils / 0.12+dfsg-1

rst2odt_prepstyles-elementtree.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Description: Port rst2odt_prepstyles to ElementTree
 rst2odt_prepstyles uses lxml to parse, modify and write XML.
 Use ElementTree, which is shipped with Python >= 2.5, in place of lxml.
Bug-Debian: http://bugs.debian.org/714319
Author: Michael Schutte <michi@debian.org>
Forwarded: https://sourceforge.net/p/docutils/patches/114/
Last-Update: 2013-08-06

--- a/tools/rst2odt_prepstyles.py
+++ b/tools/rst2odt_prepstyles.py
@@ -12,7 +12,15 @@
 #
 # Author: Michael Schutte <michi@uiae.at>
 
-from lxml import etree
+try:
+    from xml.etree import ElementTree as etree
+except ImportError:
+    try:
+        from elementtree import ElementTree as etree
+    except ImportError:
+        raise ImportError('Missing an implementation of ElementTree. ' \
+                'Please install either Python >= 2.5 or ElementTree.')
+
 import sys
 import zipfile
 from tempfile import mkstemp
@@ -27,12 +35,22 @@
 def prepstyle(filename):
     
     zin = zipfile.ZipFile(filename)
-    styles = zin.read("styles.xml")
-    
-    root = etree.fromstring(styles)
-    for el in root.xpath("//style:page-layout-properties", 
-        namespaces=NAMESPACES):
-        for attr in el.attrib:
+    styles = zin.open("styles.xml")
+
+    root = None
+    # some extra effort to preserve namespace prefixes
+    for event, elem in etree.iterparse(styles, events=("start", "start-ns")):
+        if event == "start-ns":
+            etree.register_namespace(elem[0], elem[1])
+        elif event == "start":
+            if root is None:
+                root = elem
+
+    styles.close()
+
+    for el in root.findall(".//style:page-layout-properties",
+            namespaces=NAMESPACES):
+        for attr in el.attrib.keys():
             if attr.startswith("{%s}" % NAMESPACES["fo"]):
                 del el.attrib[attr]
     
@@ -42,7 +60,7 @@
     
     for item in zin.infolist():
         if item.filename == "styles.xml":
-            zout.writestr(item, etree.tostring(root))
+            zout.writestr(item, etree.tostring(root, encoding="UTF-8"))
         else:
             zout.writestr(item, zin.read(item.filename))