mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
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.
This commit is contained in:
parent
4e757c2b56
commit
0c8a8314b2
3 changed files with 48 additions and 37 deletions
|
@ -84,5 +84,5 @@ class xep_0092(base_plugin):
|
||||||
result = iq.send()
|
result = iq.send()
|
||||||
|
|
||||||
if result and result['type'] != 'error':
|
if result and result['type'] != 'error':
|
||||||
return result['software_version']._get_stanza_values()
|
return result['software_version'].values
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -157,8 +157,7 @@ class SleekTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Create and compare several stanza objects to a correct XML string.
|
Create and compare several stanza objects to a correct XML string.
|
||||||
|
|
||||||
If use_values is False, test using getStanzaValues() and
|
If use_values is False, tests using stanza.values will not be used.
|
||||||
setStanzaValues() will not be used.
|
|
||||||
|
|
||||||
Some stanzas provide default values for some interfaces, but
|
Some stanzas provide default values for some interfaces, but
|
||||||
these defaults can be problematic for testing since they can easily
|
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
|
values. These interfaces will be set to their
|
||||||
defaults for the given and generated stanzas to
|
defaults for the given and generated stanzas to
|
||||||
prevent unexpected test failures.
|
prevent unexpected test failures.
|
||||||
use_values -- Indicates if testing using getStanzaValues() and
|
use_values -- Indicates if testing using stanza.values should
|
||||||
setStanzaValues() should be used. Defaults to
|
be used. Defaults to True.
|
||||||
True.
|
|
||||||
"""
|
"""
|
||||||
if method is None and hasattr(self, 'match_method'):
|
if method is None and hasattr(self, 'match_method'):
|
||||||
method = getattr(self, 'match_method')
|
method = getattr(self, 'match_method')
|
||||||
|
@ -216,10 +214,10 @@ class SleekTest(unittest.TestCase):
|
||||||
stanza2 = stanza_class(xml=xml)
|
stanza2 = stanza_class(xml=xml)
|
||||||
|
|
||||||
if use_values:
|
if use_values:
|
||||||
# Using getStanzaValues() and setStanzaValues() will add
|
# Using stanza.values will add XML for any interface that
|
||||||
# XML for any interface that has a default value. We need
|
# has a default value. We need to set those defaults on
|
||||||
# to set those defaults on the existing stanzas and XML
|
# the existing stanzas and XML so that they will compare
|
||||||
# so that they will compare correctly.
|
# correctly.
|
||||||
default_stanza = stanza_class()
|
default_stanza = stanza_class()
|
||||||
if defaults is None:
|
if defaults is None:
|
||||||
known_defaults = {
|
known_defaults = {
|
||||||
|
@ -238,9 +236,9 @@ class SleekTest(unittest.TestCase):
|
||||||
value = default_stanza.xml.attrib[interface]
|
value = default_stanza.xml.attrib[interface]
|
||||||
xml.attrib[interface] = value
|
xml.attrib[interface] = value
|
||||||
|
|
||||||
values = stanza2.getStanzaValues()
|
values = stanza2.values
|
||||||
stanza3 = stanza_class()
|
stanza3 = stanza_class()
|
||||||
stanza3.setStanzaValues(values)
|
stanza3.values = values
|
||||||
|
|
||||||
debug = "Three methods for creating stanzas do not match.\n"
|
debug = "Three methods for creating stanzas do not match.\n"
|
||||||
debug += "Given XML:\n%s\n" % tostring(xml)
|
debug += "Given XML:\n%s\n" % tostring(xml)
|
||||||
|
@ -390,8 +388,7 @@ class SleekTest(unittest.TestCase):
|
||||||
'id', 'stanzapath', 'xpath', and 'mask'.
|
'id', 'stanzapath', 'xpath', and 'mask'.
|
||||||
Defaults to the value of self.match_method.
|
Defaults to the value of self.match_method.
|
||||||
use_values -- Indicates if stanza comparisons should test using
|
use_values -- Indicates if stanza comparisons should test using
|
||||||
getStanzaValues() and setStanzaValues().
|
stanza.values. Defaults to True.
|
||||||
Defaults to True.
|
|
||||||
timeout -- Time to wait in seconds for data to be received by
|
timeout -- Time to wait in seconds for data to be received by
|
||||||
a live connection.
|
a live connection.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -129,6 +129,8 @@ class ElementBase(object):
|
||||||
be added as substanzas. Deprecated version
|
be added as substanzas. Deprecated version
|
||||||
of plugin_iterables.
|
of plugin_iterables.
|
||||||
types -- A set of generic type attribute values.
|
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
|
plugin_attrib -- The interface name that the stanza uses to be
|
||||||
accessed as a plugin from another stanza.
|
accessed as a plugin from another stanza.
|
||||||
plugin_attrib_map -- A mapping of plugin attribute names with the
|
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
|
values -- A dictionary of the stanza's interfaces
|
||||||
and interface values, including plugins.
|
and interface values, including plugins.
|
||||||
|
|
||||||
|
Class Methods
|
||||||
|
tag_name -- Return the namespaced version of the stanza's
|
||||||
|
root element's name.
|
||||||
|
|
||||||
Methods:
|
Methods:
|
||||||
setup -- Initialize the stanza's XML contents.
|
setup -- Initialize the stanza's XML contents.
|
||||||
enable -- Instantiate a stanza plugin.
|
enable -- Instantiate a stanza plugin.
|
||||||
|
@ -185,6 +191,7 @@ class ElementBase(object):
|
||||||
appendxml -- Add XML content to the stanza.
|
appendxml -- Add XML content to the stanza.
|
||||||
pop -- Remove a substanza.
|
pop -- Remove a substanza.
|
||||||
next -- Return the next iterable substanza.
|
next -- Return the next iterable substanza.
|
||||||
|
clear -- Reset the stanza's XML contents.
|
||||||
_fix_ns -- Apply the stanza's namespace to non-namespaced
|
_fix_ns -- Apply the stanza's namespace to non-namespaced
|
||||||
elements in an XPath expression.
|
elements in an XPath expression.
|
||||||
"""
|
"""
|
||||||
|
@ -226,6 +233,7 @@ class ElementBase(object):
|
||||||
self.plugins = {}
|
self.plugins = {}
|
||||||
self.iterables = []
|
self.iterables = []
|
||||||
self._index = 0
|
self._index = 0
|
||||||
|
self.tag = self.tag_name()
|
||||||
if parent is None:
|
if parent is None:
|
||||||
self.parent = None
|
self.parent = None
|
||||||
else:
|
else:
|
||||||
|
@ -316,14 +324,12 @@ class ElementBase(object):
|
||||||
for interface in self.interfaces:
|
for interface in self.interfaces:
|
||||||
values[interface] = self[interface]
|
values[interface] = self[interface]
|
||||||
for plugin, stanza in self.plugins.items():
|
for plugin, stanza in self.plugins.items():
|
||||||
values[plugin] = stanza._get_stanza_values()
|
values[plugin] = stanza.values
|
||||||
if self.iterables:
|
if self.iterables:
|
||||||
iterables = []
|
iterables = []
|
||||||
for stanza in self.iterables:
|
for stanza in self.iterables:
|
||||||
iterables.append(stanza._get_stanza_values())
|
iterables.append(stanza.values)
|
||||||
iterables[-1].update({
|
iterables[-1]['__childtag__'] = stanza.tag
|
||||||
'__childtag__': "{%s}%s" % (stanza.namespace,
|
|
||||||
stanza.name)})
|
|
||||||
values['substanzas'] = iterables
|
values['substanzas'] = iterables
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
@ -347,7 +353,7 @@ class ElementBase(object):
|
||||||
subclass.name)
|
subclass.name)
|
||||||
if subdict['__childtag__'] == child_tag:
|
if subdict['__childtag__'] == child_tag:
|
||||||
sub = subclass(parent=self)
|
sub = subclass(parent=self)
|
||||||
sub._set_stanza_values(subdict)
|
sub.values = subdict
|
||||||
self.iterables.append(sub)
|
self.iterables.append(sub)
|
||||||
break
|
break
|
||||||
elif interface in self.interfaces:
|
elif interface in self.interfaces:
|
||||||
|
@ -355,7 +361,7 @@ class ElementBase(object):
|
||||||
elif interface in self.plugin_attrib_map:
|
elif interface in self.plugin_attrib_map:
|
||||||
if interface not in self.plugins:
|
if interface not in self.plugins:
|
||||||
self.init_plugin(interface)
|
self.init_plugin(interface)
|
||||||
self.plugins[interface]._set_stanza_values(value)
|
self.plugins[interface].values = value
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __getitem__(self, attrib):
|
def __getitem__(self, attrib):
|
||||||
|
@ -826,6 +832,28 @@ class ElementBase(object):
|
||||||
"""
|
"""
|
||||||
return self.__next__()
|
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
|
@property
|
||||||
def attrib(self):
|
def attrib(self):
|
||||||
"""
|
"""
|
||||||
|
@ -898,13 +926,13 @@ class ElementBase(object):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Check that this stanza is a superset of the other stanza.
|
# Check that this stanza is a superset of the other stanza.
|
||||||
values = self._get_stanza_values()
|
values = self.values
|
||||||
for key in other.keys():
|
for key in other.keys():
|
||||||
if key not in values or values[key] != other[key]:
|
if key not in values or values[key] != other[key]:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Check that the other stanza is a superset of this stanza.
|
# Check that the other stanza is a superset of this stanza.
|
||||||
values = other._get_stanza_values()
|
values = other.values
|
||||||
for key in self.keys():
|
for key in self.keys():
|
||||||
if key not in values or values[key] != self[key]:
|
if key not in values or values[key] != self[key]:
|
||||||
return False
|
return False
|
||||||
|
@ -1008,7 +1036,6 @@ class StanzaBase(ElementBase):
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
stream -- The XMLStream instance that will handle sending this stanza.
|
stream -- The XMLStream instance that will handle sending this stanza.
|
||||||
tag -- The namespaced version of the stanza's name.
|
|
||||||
|
|
||||||
Methods:
|
Methods:
|
||||||
set_type -- Set the type of the stanza.
|
set_type -- Set the type of the stanza.
|
||||||
|
@ -1019,7 +1046,6 @@ class StanzaBase(ElementBase):
|
||||||
get_payload -- Return the stanza's XML contents.
|
get_payload -- Return the stanza's XML contents.
|
||||||
set_payload -- Append to the stanza's XML contents.
|
set_payload -- Append to the stanza's XML contents.
|
||||||
del_payload -- Remove 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'
|
reply -- Reset the stanza and modify the 'to' and 'from'
|
||||||
attributes to prepare for sending a reply.
|
attributes to prepare for sending a reply.
|
||||||
error -- Set the stanza's type to 'error'.
|
error -- Set the stanza's type to 'error'.
|
||||||
|
@ -1134,18 +1160,6 @@ class StanzaBase(ElementBase):
|
||||||
self.clear()
|
self.clear()
|
||||||
return self
|
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):
|
def reply(self):
|
||||||
"""
|
"""
|
||||||
Reset the stanza and swap its 'from' and 'to' attributes to prepare
|
Reset the stanza and swap its 'from' and 'to' attributes to prepare
|
||||||
|
|
Loading…
Reference in a new issue