From: Steinar H. Gunderson Date: Thu, 17 Aug 2017 22:09:35 +0000 (+0200) Subject: Add Python 3 support. X-Git-Url: https://git.sesse.net/?p=xml-template;a=commitdiff_plain;h=cba8eef965a5c243cd8bf51059f3bdd98793a22c Add Python 3 support. Patch by Kristian Klette. --- diff --git a/python/attribute-empty.py b/python/attribute-empty.py index fed55b5..99a8cbc 100644 --- a/python/attribute-empty.py +++ b/python/attribute-empty.py @@ -5,4 +5,4 @@ doc = xmltemplate.process_file("../xml/clone.xml", { "color": "blue", "#things": [] }); -print doc.toxml() +print(doc.toxml()) diff --git a/python/attribute.py b/python/attribute.py index 19d6076..450e097 100644 --- a/python/attribute.py +++ b/python/attribute.py @@ -10,4 +10,4 @@ doc = xmltemplate.process_file("../xml/clone.xml", { { "li": "Warm, woolen mittens", "li/class": "even" } ] }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/attribute2.py b/python/attribute2.py index 4f25464..deb2f43 100644 --- a/python/attribute2.py +++ b/python/attribute2.py @@ -10,4 +10,4 @@ doc = xmltemplate.process_file("../xml/clone.xml", { { "li": "Warm, woolen mittens"}, ], "odd", "even") }); -print doc.toxml() +print(doc.toxml()) diff --git a/python/attribute3.py b/python/attribute3.py index 9c6ffbf..c420d94 100644 --- a/python/attribute3.py +++ b/python/attribute3.py @@ -11,4 +11,4 @@ doc = xmltemplate.process_file("../xml/clone.xml", { { "li": "Warm, woolen mittens"}, ], "odd", "even") }); -print doc.toxml() +print(doc.toxml()) diff --git a/python/clone.py b/python/clone.py index 134cdb5..694b1b5 100644 --- a/python/clone.py +++ b/python/clone.py @@ -10,4 +10,4 @@ doc = xmltemplate.process_file("../xml/clone.xml", { { 'li': 'Warm, woolen mittens' } ] }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/include.py b/python/include.py index 0c87fba..3bab9f3 100644 --- a/python/include.py +++ b/python/include.py @@ -9,4 +9,4 @@ master = xmltemplate.process_file("../xml/master.xml", { "h1": "Nice heading here", "contents": doc }); -print master.toxml() +print(master.toxml()) diff --git a/python/namespace.py b/python/namespace.py index 2216578..2e19d7f 100644 --- a/python/namespace.py +++ b/python/namespace.py @@ -8,4 +8,4 @@ doc = xmltemplate.process_file("../xml/namespace.xml", { 'tagname': 'foo', '#moretest': 'bar' }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/namespace2.py b/python/namespace2.py index 5df48c5..3c0d0c3 100644 --- a/python/namespace2.py +++ b/python/namespace2.py @@ -5,4 +5,4 @@ doc = xmltemplate.process_file("../xml/namespace2.xml", { 'title': 'Namespace tests', '#hello': 'Replaced.' }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/passthru.py b/python/passthru.py index 1d7dd81..bd701d7 100644 --- a/python/passthru.py +++ b/python/passthru.py @@ -2,4 +2,4 @@ import xmltemplate doc = xmltemplate.process_file("../xml/passthru.xml", {}) -print doc.toxml() +print(doc.toxml()) diff --git a/python/simple.py b/python/simple.py index 7c78ed0..8df3e08 100644 --- a/python/simple.py +++ b/python/simple.py @@ -5,4 +5,4 @@ doc = xmltemplate.process_file("../xml/simple.xml", { 'title': 'A very basic example', '#hello': 'Hello world!' }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/structure.py b/python/structure.py index 519ec6f..3d72ee6 100644 --- a/python/structure.py +++ b/python/structure.py @@ -8,4 +8,4 @@ doc = xmltemplate.process_file("../xml/structure.xml", { { '#inner': 'Three' }, ] }) -print doc.toxml() +print(doc.toxml()) diff --git a/python/xmltemplate.py b/python/xmltemplate.py index 182b8e4..4348687 100644 --- a/python/xmltemplate.py +++ b/python/xmltemplate.py @@ -1,14 +1,21 @@ #! /usr/bin/python import re +import sys import xml.dom.minidom +if sys.version_info[0] > 2: + string = str +else: + string = basestring + + def process_file(filename, obj, clean = True): doc = xml.dom.minidom.parse(filename) process(doc, obj, clean) return doc def process(node, obj, clean = True): - if isinstance(obj, basestring): # overwrite + if isinstance(obj, string): # overwrite while not node.firstChild is None: node.removeChild(node.firstChild) doc = _get_document_element(node) diff --git a/tests/test.sh b/tests/test.sh index cb118be..1b66e17 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -4,7 +4,7 @@ if [ -z "$TESTS" ]; then TESTS="passthru simple clone include attribute attribute2 attribute3 attribute-empty namespace namespace2 structure" fi if [ -z "$LANGUAGES" ]; then - LANGUAGES="perl perl-sax php4 php5 python ruby c++11" + LANGUAGES="perl perl-sax php4 php5 python python3 ruby c++11" fi for L in $LANGUAGES; do @@ -28,7 +28,10 @@ for L in $LANGUAGES; do ( cd ../c++11 && make -s && cd ../php5-swig && make -s && sudo cp XML_Template_SWIG.so /usr/lib/php5/20090626+lfs ) && php5-cgi -q ../php5-swig/$T.php > $TEMPFILE fi if [ "$L" = "python" ]; then - python ../python/$T.py > $TEMPFILE + python2 ../python/$T.py > $TEMPFILE + fi + if [ "$L" = "python3" ]; then + python3 ../python/$T.py > $TEMPFILE fi if [ "$L" = "ruby" ]; then ruby -I../ruby ../ruby/$T.rb > $TEMPFILE