summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2010-08-19 14:21:58 -0400
committerLance Stout <lancestout@gmail.com>2010-08-19 14:21:58 -0400
commite4240dd593207a5912de996c42451b3946f113b2 (patch)
tree5f7832829a3449eef226429f849fec78aae2144a /sleekxmpp/xmlstream
parent2f6f4fc16d81fd01bc5a4569e0a1bc4ede4a3af8 (diff)
downloadslixmpp-e4240dd593207a5912de996c42451b3946f113b2.tar.gz
slixmpp-e4240dd593207a5912de996c42451b3946f113b2.tar.bz2
slixmpp-e4240dd593207a5912de996c42451b3946f113b2.tar.xz
slixmpp-e4240dd593207a5912de996c42451b3946f113b2.zip
Updated ElementBase.__setitem__ and added unit tests.
Diffstat (limited to 'sleekxmpp/xmlstream')
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py62
1 files changed, 44 insertions, 18 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 962cf8ed..83a8ddfc 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -225,6 +225,50 @@ class ElementBase(object):
else:
return ''
+ def __setitem__(self, attrib, value):
+ """
+ Set the value of a stanza interface using dictionary-like syntax.
+
+ Example:
+ >>> msg['body'] = "Hi!"
+ >>> msg['body']
+ 'Hi!'
+
+ Stanza interfaces are typically mapped directly to the underlying XML
+ object, but can be overridden by the presence of a setAttrib method
+ (or setFoo where the interface is named foo, etc).
+
+ The effect of interface value assignment for an interface
+ named 'foo' will be one of:
+ 1. Delete the interface's contents if the value is None.
+ 2. Call setFoo, if it exists.
+ 3. Set the text of a foo element, if foo is in sub_interfaces.
+ 4. Set the value of a top level XML attribute name foo.
+ 5. Attempt to pass value to a plugin named foo using the plugin's
+ foo interface.
+ 6. Do nothing.
+
+ Arguments:
+ attrib -- The name of the stanza interface to modify.
+ value -- The new value of the stanza interface.
+ """
+ if attrib in self.interfaces:
+ if value is not None:
+ if hasattr(self, "set%s" % attrib.title()):
+ getattr(self, "set%s" % attrib.title())(value,)
+ else:
+ if attrib in self.sub_interfaces:
+ return self._setSubText(attrib, text=value)
+ else:
+ self._setAttr(attrib, value)
+ else:
+ self.__delitem__(attrib)
+ elif attrib in self.plugin_attrib_map:
+ if attrib not in self.plugins:
+ self.initPlugin(attrib)
+ self.plugins[attrib][attrib] = value
+ return self
+
@property
def attrib(self): #backwards compatibility
return self
@@ -308,24 +352,6 @@ class ElementBase(object):
def findall(self, xpath):
return self.xml.findall(xpath)
- def __setitem__(self, attrib, value):
- if attrib in self.interfaces:
- if value is not None:
- if hasattr(self, "set%s" % attrib.title()):
- getattr(self, "set%s" % attrib.title())(value,)
- else:
- if attrib in self.sub_interfaces:
- return self._setSubText(attrib, text=value)
- else:
- self._setAttr(attrib, value)
- else:
- self.__delitem__(attrib)
- elif attrib in self.plugin_attrib_map:
- if attrib not in self.plugins: self.initPlugin(attrib)
- self.initPlugin(attrib)
- self.plugins[attrib][attrib] = value
- return self
-
def __delitem__(self, attrib):
if attrib.lower() in self.interfaces:
if hasattr(self, "del%s" % attrib.title()):