summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/stanzabase.py
diff options
context:
space:
mode:
authorLance Stout <lstout@kestrel.cs.clemson.edu>2010-08-24 09:37:42 -0400
committerLance Stout <lstout@kestrel.cs.clemson.edu>2010-08-24 09:37:42 -0400
commitc8f406d1b3e53aafdf549c93f3eeb44910c7f580 (patch)
tree25cdb85b3152a532595ef57d5c834fa80e9bb4b2 /sleekxmpp/xmlstream/stanzabase.py
parent203986dd7c485aa012fd5d84e58165374031ce87 (diff)
downloadslixmpp-c8f406d1b3e53aafdf549c93f3eeb44910c7f580.tar.gz
slixmpp-c8f406d1b3e53aafdf549c93f3eeb44910c7f580.tar.bz2
slixmpp-c8f406d1b3e53aafdf549c93f3eeb44910c7f580.tar.xz
slixmpp-c8f406d1b3e53aafdf549c93f3eeb44910c7f580.zip
Updated ElementBase._setSubText and added unit tests.
_setSubText can now handle elements specified by an XPath expression, and will build up the element tree as needed, reusing an existing elements in the path.
Diffstat (limited to 'sleekxmpp/xmlstream/stanzabase.py')
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py55
1 files changed, 43 insertions, 12 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 75b9b921..b95d837f 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -374,6 +374,49 @@ class ElementBase(object):
else:
return stanza.text
+ def _setSubText(self, name, text=None, keep=False):
+ """
+ Set the text contents of a sub element.
+
+ In case the element does not exist, a element will be created,
+ and its text contents will be set.
+
+ If the text is set to an empty string, or None, then the
+ element will be removed, unless keep is set to True.
+
+ Arguments:
+ name -- The name or XPath expression of the element.
+ text -- The new textual content of the element. If the text
+ is an empty string or None, the element will be removed
+ unless the parameter keep is True.
+ keep -- Indicates if the element should be kept if its text is
+ removed. Defaults to False.
+ """
+ name = self._fix_ns(name)
+ element = self.xml.find(name)
+
+ if not text and not keep:
+ return self.__delitem__(name)
+
+ if element is None:
+ # We need to add the element. If the provided name was
+ # an XPath expression, some of the intermediate elements
+ # may already exist. If so, we want to use those instead
+ # of generating new elements.
+ last_xml = self.xml
+ walked = []
+ for ename in name.split('/'):
+ walked.append(ename)
+ element = self.xml.find("/".join(walked))
+ if element is None:
+ element = ET.Element(ename)
+ last_xml.append(element)
+ last_xml = element
+ element = last_xml
+
+ element.text = text
+ return element
+
@property
def attrib(self): #backwards compatibility
return self
@@ -469,18 +512,6 @@ class ElementBase(object):
return False
return True
- def _setSubText(self, name, attrib={}, text=None):
- if '}' not in name:
- name = "{%s}%s" % (self.namespace, name)
- if text is None or text == '':
- return self.__delitem__(name)
- stanza = self.xml.find(name)
- if stanza is None:
- stanza = ET.Element(name)
- self.xml.append(stanza)
- stanza.text = text
- return stanza
-
def _delSub(self, name):
if '}' not in name:
name = "{%s}%s" % (self.namespace, name)