summaryrefslogtreecommitdiff
path: root/sleekxmpp
diff options
context:
space:
mode:
authorLance Stout <lstout@kestrel.cs.clemson.edu>2010-08-24 08:55:37 -0400
committerLance Stout <lstout@kestrel.cs.clemson.edu>2010-08-24 08:55:37 -0400
commit203986dd7c485aa012fd5d84e58165374031ce87 (patch)
tree8e149e61cefd11b76c9c5e2eeddfb998b1db76b6 /sleekxmpp
parentf4ecf0bac4089771a20ea6117f0a505c0b0b6e37 (diff)
downloadslixmpp-203986dd7c485aa012fd5d84e58165374031ce87.tar.gz
slixmpp-203986dd7c485aa012fd5d84e58165374031ce87.tar.bz2
slixmpp-203986dd7c485aa012fd5d84e58165374031ce87.tar.xz
slixmpp-203986dd7c485aa012fd5d84e58165374031ce87.zip
Updated ElementBase._getSubText and added unit tests.
Also added ElementBase._fix_ns() to apply the stanza namespace to elements that don't have a namespace.
Diffstat (limited to 'sleekxmpp')
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py46
1 files changed, 37 insertions, 9 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 7458f3c7..75b9b921 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -354,6 +354,26 @@ class ElementBase(object):
"""
return self.xml.attrib.get(name, default)
+ def _getSubText(self, name, default=''):
+ """
+ Return the text contents of a sub element.
+
+ In case the element does not exist, or it has no textual content,
+ a default value can be returned instead. An empty string is returned
+ if no other default is supplied.
+
+ Arguments:
+ name -- The name or XPath expression of the element.
+ default -- Optional default to return if the element does
+ not exists. An empty string is returned otherwise.
+ """
+ name = self._fix_ns(name)
+ stanza = self.xml.find(name)
+ if stanza is None or stanza.text is None:
+ return default
+ else:
+ return stanza.text
+
@property
def attrib(self): #backwards compatibility
return self
@@ -449,15 +469,6 @@ class ElementBase(object):
return False
return True
- def _getSubText(self, name):
- if '}' not in name:
- name = "{%s}%s" % (self.namespace, name)
- stanza = self.xml.find(name)
- if stanza is None or stanza.text is None:
- return ''
- else:
- return stanza.text
-
def _setSubText(self, name, attrib={}, text=None):
if '}' not in name:
name = "{%s}%s" % (self.namespace, name)
@@ -490,6 +501,23 @@ class ElementBase(object):
def __repr__(self):
return self.__str__()
+ def _fix_ns(self, xpath):
+ """
+ Apply the stanza's namespace to elements in an XPath expression.
+
+ Arguments:
+ xpath -- The XPath expression to fix with namespaces.
+ """
+
+ def fix_ns(name):
+ """Apply namespace to an element if needed."""
+ if "}" in name:
+ return name
+ return "{%s}%s" % (self.namespace, name)
+
+ return "/".join(map(fix_ns, xpath.split("/")))
+
+
#def __del__(self): #prevents garbage collection of reference cycle
# if self.parent is not None:
# self.parent.xml.remove(self.xml)