diff options
author | Erik Larsson <who+github@cnackers.org> | 2012-04-30 22:19:17 +0200 |
---|---|---|
committer | Erik Larsson <who+github@cnackers.org> | 2012-04-30 22:19:17 +0200 |
commit | fa86f956efadd006a6d54f1b5f3b83c23b8eeebf (patch) | |
tree | fa1f9d56e2843361add8d264f1725138c56878ba /sleekxmpp/xmlstream/stanzabase.py | |
parent | a9acff5294fa6c349d113236de07d0e8dae186a1 (diff) | |
download | slixmpp-fa86f956efadd006a6d54f1b5f3b83c23b8eeebf.tar.gz slixmpp-fa86f956efadd006a6d54f1b5f3b83c23b8eeebf.tar.bz2 slixmpp-fa86f956efadd006a6d54f1b5f3b83c23b8eeebf.tar.xz slixmpp-fa86f956efadd006a6d54f1b5f3b83c23b8eeebf.zip |
added multifactory and support for it to register_stanza_plugin
Diffstat (limited to 'sleekxmpp/xmlstream/stanzabase.py')
-rw-r--r-- | sleekxmpp/xmlstream/stanzabase.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 8a4f07ae..3b148446 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -67,6 +67,9 @@ def register_stanza_plugin(stanza, plugin, iterable=False, overrides=False): if iterable: stanza.plugin_iterables.add(plugin) + if iterable and hasattr(plugin, 'multi_attrib'): + multiplugin = multifactory(plugin, plugin.multi_attrib) + register_stanza_plugin(stanza, multiplugin) if overrides: for interface in plugin.overrides: stanza.plugin_overrides[interface] = plugin.plugin_attrib @@ -76,6 +79,45 @@ def register_stanza_plugin(stanza, plugin, iterable=False, overrides=False): registerStanzaPlugin = register_stanza_plugin +def multifactory(stanza, plugin_attrib): + """ + Returns a ElementBase class for handling reoccuring child stanzas + """ + class Multi(ElementBase): + """ + Template class for multifactory + """ + def setup(self, xml=None): + self.xml = ET.Element('') + + def get_multi(self): + parent = self.parent() + res = filter(lambda sub: isinstance(sub, self._multistanza), parent) + return tuple(res) + + def set_multi(self, val): + parent = self.parent() + del parent[self.plugin_attrib] + for sub in val: + parent.append(sub) + + def del_multi(self): + parent = self.parent() + res = filter(lambda sub: isinstance(sub, self._multistanza), parent) + for stanza in res: + parent.iterables.remove(stanza) + parent.xml.remove(stanza.xml) + + Multi.is_extension = True + Multi.plugin_attrib = plugin_attrib + Multi._multistanza = stanza + Multi.interfaces = (plugin_attrib,) + setattr(Multi, "get_%s" % plugin_attrib, get_multi) + setattr(Multi, "set_%s" % plugin_attrib, set_multi) + setattr(Multi, "del_%s" % plugin_attrib, del_multi) + return Multi + + def fix_ns(xpath, split=False, propagate_ns=True, default_ns=''): """Apply the stanza's namespace to elements in an XPath expression. |