changeset 15:60f59340b897

Remove argument ordering weirdness of QName. * emeraldtree/ElementTree.py: Redefine interface of QName. * emeraldtree/tests/test_ElementTree.py: Add testcases.
author Bastian Blank <bblank@thinkmo.de>
date Sat, 31 May 2008 17:46:46 +0200
parents 3d7555e0be1b
children 533331338691
files emeraldtree/ElementTree.py emeraldtree/tests/test_ElementTree.py
diffstat 2 files changed, 47 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/ElementTree.py	Sat May 31 16:34:35 2008 +0200
+++ b/emeraldtree/ElementTree.py	Sat May 31 17:46:46 2008 +0200
@@ -515,26 +515,40 @@
 # QName wrapper.  This can be used to wrap a QName attribute value, in
 # order to get proper namespace handling on output.
 #
-# @param text A string containing the QName value, in the form {uri}local,
-#     or, if the tag argument is given, the URI part of a QName.
-# @param tag Optional tag.  If given, the first argument is interpreted as
-#     an URI, and this argument is interpreted as a local name.
 # @return An opaque object, representing the QName.
 
 class QName(object):
-    def __init__(self, text_or_uri, tag=None):
-        if tag:
-            text_or_uri = "{%s}%s" % (text_or_uri, tag)
-        self.text = text_or_uri
+    def __init__(self, text, uri = None):
+        if text[0] == '{':
+            if uri is not None:
+                raise ValueError
+            i = text.find('}')
+            if i == -1:
+                raise ValueError
+            uri = text[1:i]
+            text = text[i + 1:]
+        self.name, self.uri = text, uri
+
     def __str__(self):
         return self.text
+
     def __hash__(self):
         return hash(self.text)
+
     def __cmp__(self, other):
         if isinstance(other, QName):
-            return cmp(self.text, other.text)
+            c = cmp(self.uri, other.uri)
+            if c:
+                return c
+            return cmp(self.name, other.name)
         return cmp(self.text, other)
 
+    @property
+    def text(self):
+        if self.uri is not None:
+            return '{' + self.uri + '}' + self.name
+        return self.name
+
 # --------------------------------------------------------------------
 
 ##
--- a/emeraldtree/tests/test_ElementTree.py	Sat May 31 16:34:35 2008 +0200
+++ b/emeraldtree/tests/test_ElementTree.py	Sat May 31 17:46:46 2008 +0200
@@ -1,3 +1,4 @@
+import py.test
 from emeraldtree.ElementTree import *
 
 def serialize(elem, **options):
@@ -7,7 +8,7 @@
     tree.write(file, **options)
     return file.getvalue()
 
-def test_Element___init__():
+def test_Element():
     elem = Element('a')
     assert serialize(elem) == '<a />'
     assert isinstance(elem, Node)
@@ -62,6 +63,28 @@
     elem = ProcessingInstruction('a', 'b')
     assert serialize(elem) == '<?a b?>'
 
+def test_QName():
+    qname = QName('a')
+    assert qname.uri is None
+    assert qname.name == 'a'
+    assert str(qname) == 'a'
+    assert qname.text == 'a'
+
+    qname = QName('{b}a')
+    assert qname.uri == 'b'
+    assert qname.name == 'a'
+    assert str(qname) == '{b}a'
+    assert qname.text == '{b}a'
+
+    qname = QName('a', 'b')
+    assert qname.uri == 'b'
+    assert qname.name == 'a'
+    assert str(qname) == '{b}a'
+    assert qname.text == '{b}a'
+
+    py.test.raises(ValueError, QName, '{ba')
+    py.test.raises(ValueError, QName, '{b}a', 'c')
+
 def test_XMLParser_simple1():
     elem = XML('<a />')
     assert elem.tag == 'a'