aboutsummaryrefslogtreecommitdiff
path: root/myrpki.rototill/myrpki.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2010-03-05 01:21:58 +0000
committerRob Austein <sra@hactrn.net>2010-03-05 01:21:58 +0000
commite4f7b7ebba7ac820a080aeab8c1e91cf63f8edd3 (patch)
tree209a40ce53b3b6dda93901d6b0c0d0fbd5c8ed21 /myrpki.rototill/myrpki.py
parentdac9c84f9f8484fe1b8dc3533a1b5a26d301e785 (diff)
FINALLY figured out a (relatively) sane way to deal with XML
namespaces in xml.etree API: check and strip the one and only legal namespace on read, add the one and only legal namespace on write, keep all namespace glorp out of the rest of the code entirely. svn path=/myrpki.rototill/myirbe.py; revision=3026
Diffstat (limited to 'myrpki.rototill/myrpki.py')
-rw-r--r--myrpki.rototill/myrpki.py35
1 files changed, 18 insertions, 17 deletions
diff --git a/myrpki.rototill/myrpki.py b/myrpki.rototill/myrpki.py
index 434266de..bf466038 100644
--- a/myrpki.rototill/myrpki.py
+++ b/myrpki.rototill/myrpki.py
@@ -51,11 +51,13 @@ PERFORMANCE OF THIS SOFTWARE.
import subprocess, csv, re, os, getopt, sys, ConfigParser, base64
-from xml.etree.ElementTree import Element, SubElement, ElementTree, QName
+from xml.etree.ElementTree import Element, SubElement, ElementTree
-# Our XML namespace.
+# Our XML namespace and protocol version.
-namespace = "http://www.hactrn.net/uris/rpki/myrpki/"
+namespace = "http://www.hactrn.net/uris/rpki/myrpki/"
+version = "1"
+namespaceQName = "{" + namespace + "}"
# Dialect for our use of CSV files, here to make it easy to change if
# your site needs to do something different. See doc for the csv
@@ -589,32 +591,31 @@ def etree_write(e, filename, verbose = True):
I still miss SYSCAL(RENMWO).
"""
-
assert isinstance(filename, str)
if verbose:
print "Writing", filename
- ElementTree(e).write(filename + ".tmp")
+ tmp = Element(e.tag, e.attrib, xmlns = namespace, version = version)
+ tmp[:] = e[:]
+ ElementTree(tmp).write(filename + ".tmp")
os.rename(filename + ".tmp", filename)
def etree_read(filename, verbose = False):
"""
- Read an etree from a file.
+ Read an etree from a file, verifying then stripping XML namespace
+ cruft.
"""
if verbose:
print "Reading", filename
try:
- return ElementTree(file = filename).getroot()
+ e = ElementTree(file = filename).getroot()
except IOError:
return None
-
-def tag(t):
- """
- Wrap an element name in the right XML namespace goop. We probably
- should be using a QName, but it doesn't work correctly with the
- etree search functions
- """
- #return QName(namespace, t)
- return "{" + namespace + "}" + t
+ for i in e.getiterator():
+ if i.tag.startswith(namespaceQName):
+ i.tag = i.tag[len(namespaceQName):]
+ else:
+ raise RuntimeError, "XML tag %r is not in namespace %r" % (i.tag, namespace)
+ return e
def main(argv = ()):
"""
@@ -656,7 +657,7 @@ def main(argv = ()):
e = etree_read(xml_filename)
if e:
- bsc_req, bsc_cer = bpki.bsc(e.findtext(tag("bpki_bsc_pkcs10")))
+ bsc_req, bsc_cer = bpki.bsc(e.findtext("bpki_bsc_pkcs10"))
else:
bsc_req, bsc_cer = None, None