diff options
author | Lance Stout <lancestout@gmail.com> | 2010-08-19 19:11:12 -0400 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2010-08-19 19:11:12 -0400 |
commit | fac3bca1f6fd2412b52c3e7ce9b0971d8a290083 (patch) | |
tree | c6f47b2998c01799aea3df34b3824f3eda3f913d /sleekxmpp | |
parent | e4240dd593207a5912de996c42451b3946f113b2 (diff) | |
download | slixmpp-fac3bca1f6fd2412b52c3e7ce9b0971d8a290083.tar.gz slixmpp-fac3bca1f6fd2412b52c3e7ce9b0971d8a290083.tar.bz2 slixmpp-fac3bca1f6fd2412b52c3e7ce9b0971d8a290083.tar.xz slixmpp-fac3bca1f6fd2412b52c3e7ce9b0971d8a290083.zip |
Updated ElementBase.__delitem__ and added unit tests.
Diffstat (limited to 'sleekxmpp')
-rw-r--r-- | sleekxmpp/xmlstream/stanzabase.py | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 83a8ddfc..fb8e6bee 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -269,6 +269,44 @@ class ElementBase(object): self.plugins[attrib][attrib] = value return self + def __delitem__(self, attrib): + """ + Delete the value of a stanza interface using dictionary-like syntax. + + Example: + >>> msg['body'] = "Hi!" + >>> msg['body'] + 'Hi!' + >>> del msg['body'] + >>> msg['body'] + '' + + Stanza interfaces are typically mapped directly to the underlyig XML + object, but can be overridden by the presence of a delAttrib method + (or delFoo where the interface is named foo, etc). + + The effect of deleting a stanza interface value named foo will be + one of: + 1. Call delFoo, if it exists. + 2. Delete foo element, if foo is in sub_interfaces. + 3. Delete top level XML attribute named foo. + 4. Remove the foo plugin, if it was loaded. + 5. Do nothing. + """ + if attrib in self.interfaces: + del_method = "del%s" % attrib.title() + if hasattr(self, del_method): + getattr(self, del_method)() + else: + if attrib in self.sub_interfaces: + return self._delSub(attrib) + else: + self._delAttr(attrib) + elif attrib in self.plugin_attrib_map: + if attrib in self.plugins: + del self.plugins[attrib] + return self + @property def attrib(self): #backwards compatibility return self @@ -352,20 +390,6 @@ class ElementBase(object): def findall(self, xpath): return self.xml.findall(xpath) - def __delitem__(self, attrib): - if attrib.lower() in self.interfaces: - if hasattr(self, "del%s" % attrib.title()): - getattr(self, "del%s" % attrib.title())() - else: - if attrib in self.sub_interfaces: - return self._delSub(attrib) - else: - self._delAttr(attrib) - elif attrib in self.plugin_attrib_map: - if attrib in self.plugins: - del self.plugins[attrib] - return self - def __eq__(self, other): if not isinstance(other, ElementBase): return False |