diff options
author | Lance Stout <lancestout@gmail.com> | 2011-01-26 11:27:41 -0500 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2011-01-26 11:27:41 -0500 |
commit | 0c8a8314b2e70d4a82b1c199b7e5bc16b494e275 (patch) | |
tree | 2f8b2c35afe1882a8f0b5011d76980c6099b2fed | |
parent | 4e757c2b56e7c4ded0a3de9e49c550db36da72d2 (diff) | |
download | slixmpp-0c8a8314b2e70d4a82b1c199b7e5bc16b494e275.tar.gz slixmpp-0c8a8314b2e70d4a82b1c199b7e5bc16b494e275.tar.bz2 slixmpp-0c8a8314b2e70d4a82b1c199b7e5bc16b494e275.tar.xz slixmpp-0c8a8314b2e70d4a82b1c199b7e5bc16b494e275.zip |
Cleanup for stanzabase.
Use stanza.values instead of _get/set_stanza_values where used.
ElementBase stanzas can now use .tag
May use class method tag_name() for stanza classes.
ElementBase now has .clear() method.
-rw-r--r-- | sleekxmpp/plugins/xep_0092/version.py | 2 | ||||
-rw-r--r-- | sleekxmpp/test/sleektest.py | 23 | ||||
-rw-r--r-- | sleekxmpp/xmlstream/stanzabase.py | 60 |
3 files changed, 48 insertions, 37 deletions
diff --git a/sleekxmpp/plugins/xep_0092/version.py b/sleekxmpp/plugins/xep_0092/version.py index f59f8819..fb3671e4 100644 --- a/sleekxmpp/plugins/xep_0092/version.py +++ b/sleekxmpp/plugins/xep_0092/version.py @@ -84,5 +84,5 @@ class xep_0092(base_plugin): result = iq.send() if result and result['type'] != 'error': - return result['software_version']._get_stanza_values() + return result['software_version'].values return False diff --git a/sleekxmpp/test/sleektest.py b/sleekxmpp/test/sleektest.py index aa411cd7..b5c28fde 100644 --- a/sleekxmpp/test/sleektest.py +++ b/sleekxmpp/test/sleektest.py @@ -157,8 +157,7 @@ class SleekTest(unittest.TestCase): """ Create and compare several stanza objects to a correct XML string. - If use_values is False, test using getStanzaValues() and - setStanzaValues() will not be used. + If use_values is False, tests using stanza.values will not be used. Some stanzas provide default values for some interfaces, but these defaults can be problematic for testing since they can easily @@ -181,9 +180,8 @@ class SleekTest(unittest.TestCase): values. These interfaces will be set to their defaults for the given and generated stanzas to prevent unexpected test failures. - use_values -- Indicates if testing using getStanzaValues() and - setStanzaValues() should be used. Defaults to - True. + use_values -- Indicates if testing using stanza.values should + be used. Defaults to True. """ if method is None and hasattr(self, 'match_method'): method = getattr(self, 'match_method') @@ -216,10 +214,10 @@ class SleekTest(unittest.TestCase): stanza2 = stanza_class(xml=xml) if use_values: - # Using getStanzaValues() and setStanzaValues() will add - # XML for any interface that has a default value. We need - # to set those defaults on the existing stanzas and XML - # so that they will compare correctly. + # Using stanza.values will add XML for any interface that + # has a default value. We need to set those defaults on + # the existing stanzas and XML so that they will compare + # correctly. default_stanza = stanza_class() if defaults is None: known_defaults = { @@ -238,9 +236,9 @@ class SleekTest(unittest.TestCase): value = default_stanza.xml.attrib[interface] xml.attrib[interface] = value - values = stanza2.getStanzaValues() + values = stanza2.values stanza3 = stanza_class() - stanza3.setStanzaValues(values) + stanza3.values = values debug = "Three methods for creating stanzas do not match.\n" debug += "Given XML:\n%s\n" % tostring(xml) @@ -390,8 +388,7 @@ class SleekTest(unittest.TestCase): 'id', 'stanzapath', 'xpath', and 'mask'. Defaults to the value of self.match_method. use_values -- Indicates if stanza comparisons should test using - getStanzaValues() and setStanzaValues(). - Defaults to True. + stanza.values. Defaults to True. timeout -- Time to wait in seconds for data to be received by a live connection. """ diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 764aaca7..558ab743 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -129,6 +129,8 @@ class ElementBase(object): be added as substanzas. Deprecated version of plugin_iterables. types -- A set of generic type attribute values. + tag -- The namespaced name of the stanza's root + element. Example: "{foo_ns}bar" plugin_attrib -- The interface name that the stanza uses to be accessed as a plugin from another stanza. plugin_attrib_map -- A mapping of plugin attribute names with the @@ -153,6 +155,10 @@ class ElementBase(object): values -- A dictionary of the stanza's interfaces and interface values, including plugins. + Class Methods + tag_name -- Return the namespaced version of the stanza's + root element's name. + Methods: setup -- Initialize the stanza's XML contents. enable -- Instantiate a stanza plugin. @@ -185,6 +191,7 @@ class ElementBase(object): appendxml -- Add XML content to the stanza. pop -- Remove a substanza. next -- Return the next iterable substanza. + clear -- Reset the stanza's XML contents. _fix_ns -- Apply the stanza's namespace to non-namespaced elements in an XPath expression. """ @@ -226,6 +233,7 @@ class ElementBase(object): self.plugins = {} self.iterables = [] self._index = 0 + self.tag = self.tag_name() if parent is None: self.parent = None else: @@ -316,14 +324,12 @@ class ElementBase(object): for interface in self.interfaces: values[interface] = self[interface] for plugin, stanza in self.plugins.items(): - values[plugin] = stanza._get_stanza_values() + values[plugin] = stanza.values if self.iterables: iterables = [] for stanza in self.iterables: - iterables.append(stanza._get_stanza_values()) - iterables[-1].update({ - '__childtag__': "{%s}%s" % (stanza.namespace, - stanza.name)}) + iterables.append(stanza.values) + iterables[-1]['__childtag__'] = stanza.tag values['substanzas'] = iterables return values @@ -347,7 +353,7 @@ class ElementBase(object): subclass.name) if subdict['__childtag__'] == child_tag: sub = subclass(parent=self) - sub._set_stanza_values(subdict) + sub.values = subdict self.iterables.append(sub) break elif interface in self.interfaces: @@ -355,7 +361,7 @@ class ElementBase(object): elif interface in self.plugin_attrib_map: if interface not in self.plugins: self.init_plugin(interface) - self.plugins[interface]._set_stanza_values(value) + self.plugins[interface].values = value return self def __getitem__(self, attrib): @@ -826,6 +832,28 @@ class ElementBase(object): """ return self.__next__() + def clear(self): + """ + Remove all XML element contents and plugins. + + Any attribute values will be preserved. + """ + for child in self.xml.getchildren(): + self.xml.remove(child) + for plugin in list(self.plugins.keys()): + del self.plugins[plugin] + return self + + @classmethod + def tag_name(cls): + """ + Return the namespaced name of the stanza's root element. + + For example, for the stanza <foo xmlns="bar" />, + stanza.tag would return "{bar}foo". + """ + return "{%s}%s" % (cls.namespace, cls.name) + @property def attrib(self): """ @@ -898,13 +926,13 @@ class ElementBase(object): return False # Check that this stanza is a superset of the other stanza. - values = self._get_stanza_values() + values = self.values for key in other.keys(): if key not in values or values[key] != other[key]: return False # Check that the other stanza is a superset of this stanza. - values = other._get_stanza_values() + values = other.values for key in self.keys(): if key not in values or values[key] != self[key]: return False @@ -1008,7 +1036,6 @@ class StanzaBase(ElementBase): Attributes: stream -- The XMLStream instance that will handle sending this stanza. - tag -- The namespaced version of the stanza's name. Methods: set_type -- Set the type of the stanza. @@ -1019,7 +1046,6 @@ class StanzaBase(ElementBase): get_payload -- Return the stanza's XML contents. set_payload -- Append to the stanza's XML contents. del_payload -- Remove the stanza's XML contents. - clear -- Reset the stanza's XML contents. reply -- Reset the stanza and modify the 'to' and 'from' attributes to prepare for sending a reply. error -- Set the stanza's type to 'error'. @@ -1134,18 +1160,6 @@ class StanzaBase(ElementBase): self.clear() return self - def clear(self): - """ - Remove all XML element contents and plugins. - - Any attribute values will be preserved. - """ - for child in self.xml.getchildren(): - self.xml.remove(child) - for plugin in list(self.plugins.keys()): - del self.plugins[plugin] - return self - def reply(self): """ Reset the stanza and swap its 'from' and 'to' attributes to prepare |