summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py48
-rw-r--r--tests/test_elementbase.py23
2 files changed, 55 insertions, 16 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 6762792f..b11d59ee 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -76,6 +76,38 @@ class ElementBase(object):
self.iterables.append(sub(child, self))
break
+ def setup(self, xml=None):
+ """
+ Initialize the stanza's XML contents.
+
+ Will return True if XML was generated according to the stanza's
+ definition.
+
+ Arguments:
+ xml -- Optional XML object to use for the stanza's content
+ instead of generating XML.
+ """
+ if self.xml is None:
+ self.xml = xml
+
+ if self.xml is None:
+ # Generate XML from the stanza definition
+ for ename in self.name.split('/'):
+ new = ET.Element("{%s}%s" % (self.namespace, ename))
+ if self.xml is None:
+ self.xml = new
+ else:
+ last_xml.append(new)
+ last_xml = new
+ if self.parent is not None:
+ self.parent().xml.append(self.xml)
+
+ # We had to generate XML
+ return True
+ else:
+ # We did not generate XML
+ return False
+
@property
def attrib(self): #backwards compatibility
return self
@@ -159,22 +191,6 @@ class ElementBase(object):
def findall(self, xpath):
return self.xml.findall(xpath)
- def setup(self, xml=None):
- if self.xml is None:
- self.xml = xml
- if self.xml is None:
- for ename in self.name.split('/'):
- new = ET.Element("{%(namespace)s}%(name)s" % {'name': self.name, 'namespace': self.namespace})
- if self.xml is None:
- self.xml = new
- else:
- self.xml.append(new)
- if self.parent is not None:
- self.parent().xml.append(self.xml)
- return True #had to generate XML
- else:
- return False
-
def enable(self, attrib):
self.initPlugin(attrib)
return self
diff --git a/tests/test_elementbase.py b/tests/test_elementbase.py
new file mode 100644
index 00000000..1b018b44
--- /dev/null
+++ b/tests/test_elementbase.py
@@ -0,0 +1,23 @@
+from . sleektest import *
+from sleekxmpp.xmlstream.stanzabase import ElementBase
+
+class TestElementBase(SleekTest):
+
+ def testExtendedName(self):
+ """Test element names of the form tag1/tag2/tag3."""
+
+ class TestStanza(ElementBase):
+ name = "foo/bar/baz"
+ namespace = "test"
+
+ stanza = TestStanza()
+ self.checkStanza(TestStanza, stanza, """
+ <foo xmlns="test">
+ <bar>
+ <baz />
+ </bar>
+ </foo>
+ """)
+
+
+suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase)