[Python-modules-commits] [python-docutils] 11/14: Port rst2odt_prepstyles to ElementTree

Dmitry Shachnev mitya57 at moszumanska.debian.org
Sat Dec 10 10:40:52 UTC 2016


This is an automated email from the git hooks/post-receive script.

mitya57 pushed a commit to branch master
in repository python-docutils.

commit 794825ccfebc1181ea3b682f8c4b4d0600de556c
Author: Michael Schutte <michi at debian.org>
Date:   Thu Oct 8 11:57:11 2015 -0700

    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
    Forwarded: https://sourceforge.net/p/docutils/patches/114/
    Last-Update: 2013-08-06
    
    Patch-Name: rst2odt_prepstyles-elementtree.diff
---
 tools/rst2odt_prepstyles.py | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/tools/rst2odt_prepstyles.py b/tools/rst2odt_prepstyles.py
index b0b7dcc..10accb2 100755
--- a/tools/rst2odt_prepstyles.py
+++ b/tools/rst2odt_prepstyles.py
@@ -12,7 +12,15 @@ specifications from styles.xml in STYLE_FILE.odt.
 #
 # Author: Michael Schutte <michi at 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 @@ NAMESPACES = {
 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 @@ def prepstyle(filename):
     
     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))
     

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-docutils.git



More information about the Python-modules-commits mailing list