2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-08-13 03:24:09 +00:00
|
|
|
|
|
|
|
import copy
|
2009-12-17 01:54:22 +00:00
|
|
|
import logging
|
2010-01-29 10:04:15 +00:00
|
|
|
import sys
|
2010-05-12 20:45:36 +00:00
|
|
|
import weakref
|
2010-08-13 03:24:09 +00:00
|
|
|
from xml.etree import cElementTree as ET
|
2010-01-29 10:04:15 +00:00
|
|
|
|
2010-08-13 03:24:09 +00:00
|
|
|
from sleekxmpp.xmlstream import JID
|
2010-08-06 00:26:41 +00:00
|
|
|
from sleekxmpp.xmlstream.tostring import tostring
|
2011-01-27 21:01:35 +00:00
|
|
|
from sleekxmpp.thirdparty import OrderedDict
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-08-13 14:12:51 +00:00
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-08-13 14:12:51 +00:00
|
|
|
# Used to check if an argument is an XML object.
|
|
|
|
XML_TYPE = type(ET.Element('xml'))
|
2010-01-14 15:37:44 +00:00
|
|
|
|
2010-07-19 17:58:53 +00:00
|
|
|
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
def register_stanza_plugin(stanza, plugin, iterable=False, overrides=False):
|
2010-08-13 14:12:51 +00:00
|
|
|
"""
|
|
|
|
Associate a stanza object as a plugin for another stanza.
|
|
|
|
|
|
|
|
Arguments:
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
stanza -- The class of the parent stanza.
|
|
|
|
plugin -- The class of the plugin stanza.
|
|
|
|
iterable -- Indicates if the plugin stanza should be
|
|
|
|
included in the parent stanza's iterable
|
|
|
|
'substanzas' interface results.
|
|
|
|
overrides -- Indicates if the plugin should be allowed
|
|
|
|
to override the interface handlers for
|
|
|
|
the parent stanza.
|
2010-08-13 14:12:51 +00:00
|
|
|
"""
|
|
|
|
tag = "{%s}%s" % (plugin.namespace, plugin.name)
|
2011-09-01 07:50:45 +00:00
|
|
|
|
|
|
|
# Prevent weird memory reference gotchas by ensuring
|
|
|
|
# that the parent stanza class has its own set of
|
|
|
|
# plugin info maps and is not using the mappings from
|
|
|
|
# an ancestor class (like ElementBase).
|
|
|
|
plugin_info = ('plugin_attrib_map', 'plugin_tag_map',
|
|
|
|
'plugin_iterables', 'plugin_overrides')
|
|
|
|
for attr in plugin_info:
|
|
|
|
info = getattr(stanza, attr)
|
|
|
|
setattr(stanza, attr, info.copy())
|
|
|
|
|
2010-08-13 14:12:51 +00:00
|
|
|
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
|
|
|
|
stanza.plugin_tag_map[tag] = plugin
|
2011-09-01 07:50:45 +00:00
|
|
|
|
2011-01-26 15:04:36 +00:00
|
|
|
if iterable:
|
|
|
|
stanza.plugin_iterables.add(plugin)
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
if overrides:
|
|
|
|
for interface in plugin.overrides:
|
|
|
|
stanza.plugin_overrides[interface] = plugin.plugin_attrib
|
2010-07-19 17:58:53 +00:00
|
|
|
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
# To maintain backwards compatibility for now, preserve the camel case name.
|
|
|
|
registerStanzaPlugin = register_stanza_plugin
|
|
|
|
|
|
|
|
|
2010-08-06 00:26:41 +00:00
|
|
|
class ElementBase(object):
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
The core of SleekXMPP's stanza XML manipulation and handling is provided
|
|
|
|
by ElementBase. ElementBase wraps XML cElementTree objects and enables
|
|
|
|
access to the XML contents through dictionary syntax, similar in style
|
|
|
|
to the Ruby XMPP library Blather's stanza implementation.
|
|
|
|
|
|
|
|
Stanzas are defined by their name, namespace, and interfaces. For
|
|
|
|
example, a simplistic Message stanza could be defined as:
|
|
|
|
|
|
|
|
>>> class Message(ElementBase):
|
|
|
|
... name = "message"
|
|
|
|
... namespace = "jabber:client"
|
|
|
|
... interfaces = set(('to', 'from', 'type', 'body'))
|
|
|
|
... sub_interfaces = set(('body',))
|
|
|
|
|
|
|
|
The resulting Message stanza's contents may be accessed as so:
|
|
|
|
|
|
|
|
>>> message['to'] = "user@example.com"
|
|
|
|
>>> message['body'] = "Hi!"
|
2010-08-26 18:07:09 +00:00
|
|
|
>>> message['body']
|
|
|
|
"Hi!"
|
|
|
|
>>> del message['body']
|
|
|
|
>>> message['body']
|
|
|
|
""
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
The interface values map to either custom access methods, stanza
|
|
|
|
XML attributes, or (if the interface is also in sub_interfaces) the
|
|
|
|
text contents of a stanza's subelement.
|
|
|
|
|
|
|
|
Custom access methods may be created by adding methods of the
|
|
|
|
form "getInterface", "setInterface", or "delInterface", where
|
2010-08-26 18:07:09 +00:00
|
|
|
"Interface" is the titlecase version of the interface name.
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
Stanzas may be extended through the use of plugins. A plugin
|
|
|
|
is simply a stanza that has a plugin_attrib value. For example:
|
|
|
|
|
|
|
|
>>> class MessagePlugin(ElementBase):
|
|
|
|
... name = "custom_plugin"
|
|
|
|
... namespace = "custom"
|
|
|
|
... interfaces = set(('useful_thing', 'custom'))
|
|
|
|
... plugin_attrib = "custom"
|
|
|
|
|
|
|
|
The plugin stanza class must be associated with its intended
|
2010-10-18 01:38:22 +00:00
|
|
|
container stanza by using register_stanza_plugin as so:
|
2010-08-26 17:49:36 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
>>> register_stanza_plugin(Message, MessagePlugin)
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
The plugin may then be accessed as if it were built-in to the parent
|
|
|
|
stanza.
|
|
|
|
|
|
|
|
>>> message['custom']['useful_thing'] = 'foo'
|
|
|
|
|
|
|
|
If a plugin provides an interface that is the same as the plugin's
|
2011-01-20 00:49:13 +00:00
|
|
|
plugin_attrib value, then the plugin's interface may be assigned
|
|
|
|
directly from the parent stanza, as shown below, but retrieving
|
|
|
|
information will require all interfaces to be used, as so:
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
>>> message['custom'] = 'bar' # Same as using message['custom']['custom']
|
2011-01-20 00:49:13 +00:00
|
|
|
>>> message['custom']['custom'] # Must use all interfaces
|
|
|
|
'bar'
|
|
|
|
|
|
|
|
If the plugin sets the value is_extension = True, then both setting
|
|
|
|
and getting an interface value that is the same as the plugin's
|
|
|
|
plugin_attrib value will work, as so:
|
|
|
|
|
|
|
|
>>> message['custom'] = 'bar' # Using is_extension=True
|
|
|
|
>>> message['custom']
|
|
|
|
'bar'
|
|
|
|
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
Class Attributes:
|
|
|
|
name -- The name of the stanza's main element.
|
|
|
|
namespace -- The namespace of the stanza's main element.
|
|
|
|
interfaces -- A set of attribute and element names that may
|
|
|
|
be accessed using dictionary syntax.
|
|
|
|
sub_interfaces -- A subset of the set of interfaces which map
|
|
|
|
to subelements instead of attributes.
|
|
|
|
subitem -- A set of stanza classes which are allowed to
|
2011-01-26 15:04:36 +00:00
|
|
|
be added as substanzas. Deprecated version
|
|
|
|
of plugin_iterables.
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
overrides -- A list of interfaces prepended with 'get_',
|
|
|
|
'set_', or 'del_'. If the stanza is registered
|
|
|
|
as a plugin with overrides=True, then the
|
|
|
|
parent's interface handlers will be
|
|
|
|
overridden by the plugin's matching handler.
|
2010-08-26 17:49:36 +00:00
|
|
|
types -- A set of generic type attribute values.
|
2011-01-26 16:27:41 +00:00
|
|
|
tag -- The namespaced name of the stanza's root
|
|
|
|
element. Example: "{foo_ns}bar"
|
2010-08-26 17:49:36 +00:00
|
|
|
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
|
|
|
|
associated plugin stanza classes.
|
2011-01-26 15:04:36 +00:00
|
|
|
plugin_iterables -- A set of stanza classes which are allowed to
|
|
|
|
be added as substanzas.
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
plugin_overrides -- A mapping of interfaces prepended with 'get_',
|
|
|
|
'set_' or 'del_' to plugin attrib names. Allows
|
|
|
|
a plugin to override the behaviour of a parent
|
|
|
|
stanza's interface handlers.
|
2010-08-26 17:49:36 +00:00
|
|
|
plugin_tag_map -- A mapping of plugin stanza tag names with
|
|
|
|
the associated plugin stanza classes.
|
2011-01-20 00:49:13 +00:00
|
|
|
is_extension -- When True, allows the stanza to provide one
|
|
|
|
additional interface to the parent stanza,
|
|
|
|
extending the interfaces supported by the
|
|
|
|
parent. Defaults to False.
|
2010-12-08 04:07:40 +00:00
|
|
|
xml_ns -- The XML namespace,
|
|
|
|
http://www.w3.org/XML/1998/namespace,
|
|
|
|
for use with xml:lang values.
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
Instance Attributes:
|
|
|
|
xml -- The stanza's XML contents.
|
|
|
|
parent -- The parent stanza of this stanza.
|
|
|
|
plugins -- A map of enabled plugin names with the
|
|
|
|
initialized plugin stanza objects.
|
2010-10-18 01:38:22 +00:00
|
|
|
values -- A dictionary of the stanza's interfaces
|
|
|
|
and interface values, including plugins.
|
2010-08-26 17:49:36 +00:00
|
|
|
|
2011-01-26 16:27:41 +00:00
|
|
|
Class Methods
|
|
|
|
tag_name -- Return the namespaced version of the stanza's
|
|
|
|
root element's name.
|
|
|
|
|
2010-08-26 17:49:36 +00:00
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
setup -- Initialize the stanza's XML contents.
|
|
|
|
enable -- Instantiate a stanza plugin.
|
|
|
|
Alias for init_plugin.
|
|
|
|
init_plugin -- Instantiate a stanza plugin.
|
|
|
|
_get_stanza_values -- Return a dictionary of stanza interfaces and
|
|
|
|
their values.
|
|
|
|
_set_stanza_values -- Set stanza interface values given a dictionary
|
|
|
|
of interfaces and values.
|
|
|
|
__getitem__ -- Return the value of a stanza interface.
|
|
|
|
__setitem__ -- Set the value of a stanza interface.
|
|
|
|
__delitem__ -- Remove the value of a stanza interface.
|
|
|
|
_set_attr -- Set an attribute value of the main
|
|
|
|
stanza element.
|
|
|
|
_del_attr -- Remove an attribute from the main
|
|
|
|
stanza element.
|
|
|
|
_get_attr -- Return an attribute's value from the main
|
|
|
|
stanza element.
|
|
|
|
_get_sub_text -- Return the text contents of a subelement.
|
2010-12-08 04:07:40 +00:00
|
|
|
_set_sub_text -- Set the text contents of a subelement.
|
2010-10-18 01:38:22 +00:00
|
|
|
_del_sub -- Remove a subelement.
|
|
|
|
match -- Compare the stanza against an XPath expression.
|
|
|
|
find -- Return subelement matching an XPath expression.
|
|
|
|
findall -- Return subelements matching an XPath expression.
|
|
|
|
get -- Return the value of a stanza interface, with an
|
|
|
|
optional default value.
|
|
|
|
keys -- Return the set of interface names accepted by
|
|
|
|
the stanza.
|
|
|
|
append -- Add XML content or a substanza to the stanza.
|
|
|
|
appendxml -- Add XML content to the stanza.
|
|
|
|
pop -- Remove a substanza.
|
|
|
|
next -- Return the next iterable substanza.
|
2011-01-26 16:27:41 +00:00
|
|
|
clear -- Reset the stanza's XML contents.
|
2010-10-18 01:38:22 +00:00
|
|
|
_fix_ns -- Apply the stanza's namespace to non-namespaced
|
|
|
|
elements in an XPath expression.
|
2010-08-26 17:49:36 +00:00
|
|
|
"""
|
|
|
|
|
2010-08-13 14:15:52 +00:00
|
|
|
name = 'stanza'
|
|
|
|
plugin_attrib = 'plugin'
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
|
|
|
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
|
|
|
sub_interfaces = tuple()
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
overrides = {}
|
2010-08-13 14:15:52 +00:00
|
|
|
plugin_attrib_map = {}
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
plugin_overrides = {}
|
2011-01-26 15:04:36 +00:00
|
|
|
plugin_iterables = set()
|
2010-08-13 14:15:52 +00:00
|
|
|
plugin_tag_map = {}
|
2011-03-23 00:42:43 +00:00
|
|
|
subitem = set()
|
2011-01-20 00:49:13 +00:00
|
|
|
is_extension = False
|
2010-12-08 04:07:40 +00:00
|
|
|
xml_ns = 'http://www.w3.org/XML/1998/namespace'
|
2010-08-13 14:15:52 +00:00
|
|
|
|
|
|
|
def __init__(self, xml=None, parent=None):
|
2010-08-13 14:26:33 +00:00
|
|
|
"""
|
|
|
|
Create a new stanza object.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- Initialize the stanza with optional existing XML.
|
|
|
|
parent -- Optional stanza object that contains this stanza.
|
|
|
|
"""
|
|
|
|
self.xml = xml
|
2011-01-27 21:01:35 +00:00
|
|
|
self.plugins = OrderedDict()
|
2010-08-13 14:26:33 +00:00
|
|
|
self.iterables = []
|
2010-08-26 14:08:22 +00:00
|
|
|
self._index = 0
|
2011-01-26 16:27:41 +00:00
|
|
|
self.tag = self.tag_name()
|
2010-08-13 14:26:33 +00:00
|
|
|
if parent is None:
|
|
|
|
self.parent = None
|
|
|
|
else:
|
|
|
|
self.parent = weakref.ref(parent)
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
ElementBase.values = property(ElementBase._get_stanza_values,
|
|
|
|
ElementBase._set_stanza_values)
|
|
|
|
|
2011-03-23 00:42:43 +00:00
|
|
|
if self.subitem is not None:
|
|
|
|
for sub in self.subitem:
|
|
|
|
self.plugin_iterables.add(sub)
|
|
|
|
|
2010-08-13 14:26:33 +00:00
|
|
|
if self.setup(xml):
|
|
|
|
# If we generated our own XML, then everything is ready.
|
|
|
|
return
|
|
|
|
|
|
|
|
# Initialize values using provided XML
|
|
|
|
for child in self.xml.getchildren():
|
|
|
|
if child.tag in self.plugin_tag_map:
|
|
|
|
plugin = self.plugin_tag_map[child.tag]
|
|
|
|
self.plugins[plugin.plugin_attrib] = plugin(child, self)
|
2011-01-26 15:04:36 +00:00
|
|
|
for sub in self.plugin_iterables:
|
|
|
|
if child.tag == "{%s}%s" % (sub.namespace, sub.name):
|
|
|
|
self.iterables.append(sub(child, self))
|
|
|
|
break
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-13 16:24:47 +00:00
|
|
|
def setup(self, xml=None):
|
|
|
|
"""
|
|
|
|
Initialize the stanza's XML contents.
|
|
|
|
|
|
|
|
Will return True if XML was generated according to the stanza's
|
|
|
|
definition.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- Optional XML object to use for the stanza's content
|
|
|
|
instead of generating XML.
|
|
|
|
"""
|
|
|
|
if self.xml is None:
|
|
|
|
self.xml = xml
|
|
|
|
|
|
|
|
if self.xml is None:
|
|
|
|
# Generate XML from the stanza definition
|
|
|
|
for ename in self.name.split('/'):
|
|
|
|
new = ET.Element("{%s}%s" % (self.namespace, ename))
|
|
|
|
if self.xml is None:
|
|
|
|
self.xml = new
|
|
|
|
else:
|
|
|
|
last_xml.append(new)
|
|
|
|
last_xml = new
|
|
|
|
if self.parent is not None:
|
|
|
|
self.parent().xml.append(self.xml)
|
|
|
|
|
|
|
|
# We had to generate XML
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# We did not generate XML
|
|
|
|
return False
|
|
|
|
|
2010-08-13 16:51:07 +00:00
|
|
|
def enable(self, attrib):
|
|
|
|
"""
|
|
|
|
Enable and initialize a stanza plugin.
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
Alias for init_plugin.
|
2010-08-13 16:51:07 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
attrib -- The stanza interface for the plugin.
|
|
|
|
"""
|
2010-10-18 01:38:22 +00:00
|
|
|
return self.init_plugin(attrib)
|
2010-08-13 16:51:07 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def init_plugin(self, attrib):
|
2010-08-13 16:51:07 +00:00
|
|
|
"""
|
|
|
|
Enable and initialize a stanza plugin.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
attrib -- The stanza interface for the plugin.
|
|
|
|
"""
|
|
|
|
if attrib not in self.plugins:
|
|
|
|
plugin_class = self.plugin_attrib_map[attrib]
|
|
|
|
self.plugins[attrib] = plugin_class(parent=self)
|
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _get_stanza_values(self):
|
2010-08-14 00:05:24 +00:00
|
|
|
"""
|
|
|
|
Return a dictionary of the stanza's interface values.
|
|
|
|
|
|
|
|
Stanza plugin values are included as nested dictionaries.
|
|
|
|
"""
|
|
|
|
values = {}
|
|
|
|
for interface in self.interfaces:
|
|
|
|
values[interface] = self[interface]
|
|
|
|
for plugin, stanza in self.plugins.items():
|
2011-01-26 16:27:41 +00:00
|
|
|
values[plugin] = stanza.values
|
2010-08-14 00:05:24 +00:00
|
|
|
if self.iterables:
|
|
|
|
iterables = []
|
|
|
|
for stanza in self.iterables:
|
2011-01-26 16:27:41 +00:00
|
|
|
iterables.append(stanza.values)
|
|
|
|
iterables[-1]['__childtag__'] = stanza.tag
|
2010-08-14 00:05:24 +00:00
|
|
|
values['substanzas'] = iterables
|
|
|
|
return values
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _set_stanza_values(self, values):
|
2010-08-14 00:05:24 +00:00
|
|
|
"""
|
|
|
|
Set multiple stanza interface values using a dictionary.
|
|
|
|
|
|
|
|
Stanza plugin values may be set using nested dictionaries.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
values -- A dictionary mapping stanza interface with values.
|
|
|
|
Plugin interfaces may accept a nested dictionary that
|
|
|
|
will be used recursively.
|
|
|
|
"""
|
2011-03-23 00:42:43 +00:00
|
|
|
iterable_interfaces = [p.plugin_attrib for \
|
|
|
|
p in self.plugin_iterables]
|
|
|
|
|
2010-08-14 00:05:24 +00:00
|
|
|
for interface, value in values.items():
|
|
|
|
if interface == 'substanzas':
|
2011-03-23 00:42:43 +00:00
|
|
|
# Remove existing substanzas
|
|
|
|
for stanza in self.iterables:
|
|
|
|
self.xml.remove(stanza.xml)
|
|
|
|
self.iterables = []
|
|
|
|
|
|
|
|
# Add new substanzas
|
2010-08-14 00:05:24 +00:00
|
|
|
for subdict in value:
|
|
|
|
if '__childtag__' in subdict:
|
2011-03-23 00:42:43 +00:00
|
|
|
for subclass in self.plugin_iterables:
|
2010-08-14 00:05:24 +00:00
|
|
|
child_tag = "{%s}%s" % (subclass.namespace,
|
|
|
|
subclass.name)
|
|
|
|
if subdict['__childtag__'] == child_tag:
|
|
|
|
sub = subclass(parent=self)
|
2011-01-26 16:27:41 +00:00
|
|
|
sub.values = subdict
|
2010-08-14 00:05:24 +00:00
|
|
|
self.iterables.append(sub)
|
|
|
|
break
|
|
|
|
elif interface in self.interfaces:
|
|
|
|
self[interface] = value
|
|
|
|
elif interface in self.plugin_attrib_map:
|
2011-03-23 00:42:43 +00:00
|
|
|
if interface not in iterable_interfaces:
|
|
|
|
if interface not in self.plugins:
|
|
|
|
self.init_plugin(interface)
|
|
|
|
self.plugins[interface].values = value
|
2010-08-14 00:05:24 +00:00
|
|
|
return self
|
|
|
|
|
2010-08-14 01:33:11 +00:00
|
|
|
def __getitem__(self, attrib):
|
|
|
|
"""
|
|
|
|
Return the value of a stanza interface using dictionary-like syntax.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> msg['body']
|
|
|
|
'Message contents'
|
|
|
|
|
|
|
|
Stanza interfaces are typically mapped directly to the underlying XML
|
2010-10-18 01:38:22 +00:00
|
|
|
object, but can be overridden by the presence of a get_attrib method
|
|
|
|
(or get_foo where the interface is named foo, etc).
|
2010-08-14 01:33:11 +00:00
|
|
|
|
|
|
|
The search order for interface value retrieval for an interface
|
|
|
|
named 'foo' is:
|
|
|
|
1. The list of substanzas.
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
2. The result of calling the get_foo override handler.
|
|
|
|
3. The result of calling get_foo.
|
|
|
|
4. The result of calling getFoo.
|
|
|
|
5. The contents of the foo subelement, if foo is a sub interface.
|
|
|
|
6. The value of the foo attribute of the XML object.
|
|
|
|
7. The plugin named 'foo'
|
|
|
|
8. An empty string.
|
2010-08-14 01:33:11 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
attrib -- The name of the requested stanza interface.
|
|
|
|
"""
|
|
|
|
if attrib == 'substanzas':
|
|
|
|
return self.iterables
|
|
|
|
elif attrib in self.interfaces:
|
2010-10-18 01:38:22 +00:00
|
|
|
get_method = "get_%s" % attrib.lower()
|
|
|
|
get_method2 = "get%s" % attrib.title()
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
if self.plugin_overrides:
|
|
|
|
plugin = self.plugin_overrides.get(get_method, None)
|
|
|
|
if plugin:
|
|
|
|
if plugin not in self.plugins:
|
|
|
|
self.init_plugin(plugin)
|
|
|
|
handler = getattr(self.plugins[plugin], get_method, None)
|
|
|
|
if handler:
|
|
|
|
return handler()
|
|
|
|
|
2010-08-14 01:33:11 +00:00
|
|
|
if hasattr(self, get_method):
|
|
|
|
return getattr(self, get_method)()
|
2010-10-18 01:38:22 +00:00
|
|
|
elif hasattr(self, get_method2):
|
|
|
|
return getattr(self, get_method2)()
|
2010-08-14 01:33:11 +00:00
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_sub_text(attrib)
|
2010-08-14 01:33:11 +00:00
|
|
|
else:
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_attr(attrib)
|
2010-08-14 01:33:11 +00:00
|
|
|
elif attrib in self.plugin_attrib_map:
|
|
|
|
if attrib not in self.plugins:
|
2010-10-18 01:38:22 +00:00
|
|
|
self.init_plugin(attrib)
|
2011-01-20 00:49:13 +00:00
|
|
|
if self.plugins[attrib].is_extension:
|
|
|
|
return self.plugins[attrib][attrib]
|
2010-08-14 01:33:11 +00:00
|
|
|
return self.plugins[attrib]
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
2010-08-19 18:21:58 +00:00
|
|
|
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
|
2010-10-18 01:38:22 +00:00
|
|
|
object, but can be overridden by the presence of a set_attrib method
|
|
|
|
(or set_foo where the interface is named foo, etc).
|
2010-08-19 18:21:58 +00:00
|
|
|
|
|
|
|
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.
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
2. Call the set_foo override handler, if it exists.
|
|
|
|
3. Call set_foo, if it exists.
|
|
|
|
4. Call setFoo, if it exists.
|
|
|
|
5. Set the text of a foo element, if foo is in sub_interfaces.
|
|
|
|
6. Set the value of a top level XML attribute name foo.
|
|
|
|
7. Attempt to pass value to a plugin named foo using the plugin's
|
2010-08-19 18:21:58 +00:00
|
|
|
foo interface.
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
8. Do nothing.
|
2010-08-19 18:21:58 +00:00
|
|
|
|
|
|
|
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:
|
2010-10-18 01:38:22 +00:00
|
|
|
set_method = "set_%s" % attrib.lower()
|
|
|
|
set_method2 = "set%s" % attrib.title()
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
if self.plugin_overrides:
|
|
|
|
plugin = self.plugin_overrides.get(set_method, None)
|
|
|
|
if plugin:
|
|
|
|
if plugin not in self.plugins:
|
|
|
|
self.init_plugin(plugin)
|
2011-08-05 05:37:22 +00:00
|
|
|
handler = getattr(self.plugins[plugin],
|
|
|
|
set_method, None)
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
if handler:
|
|
|
|
return handler(value)
|
|
|
|
|
2010-08-19 23:14:18 +00:00
|
|
|
if hasattr(self, set_method):
|
|
|
|
getattr(self, set_method)(value,)
|
2010-10-18 01:38:22 +00:00
|
|
|
elif hasattr(self, set_method2):
|
|
|
|
getattr(self, set_method2)(value,)
|
2010-08-19 18:21:58 +00:00
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._set_sub_text(attrib, text=value)
|
2010-08-19 18:21:58 +00:00
|
|
|
else:
|
2010-10-18 01:38:22 +00:00
|
|
|
self._set_attr(attrib, value)
|
2010-08-19 18:21:58 +00:00
|
|
|
else:
|
|
|
|
self.__delitem__(attrib)
|
|
|
|
elif attrib in self.plugin_attrib_map:
|
|
|
|
if attrib not in self.plugins:
|
2010-10-18 01:38:22 +00:00
|
|
|
self.init_plugin(attrib)
|
2010-08-19 18:21:58 +00:00
|
|
|
self.plugins[attrib][attrib] = value
|
|
|
|
return self
|
|
|
|
|
2010-08-19 23:11:12 +00:00
|
|
|
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
|
2010-10-18 01:38:22 +00:00
|
|
|
object, but can be overridden by the presence of a del_attrib method
|
|
|
|
(or del_foo where the interface is named foo, etc).
|
2010-08-19 23:11:12 +00:00
|
|
|
|
|
|
|
The effect of deleting a stanza interface value named foo will be
|
|
|
|
one of:
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
1. Call del_foo override handler, if it exists.
|
|
|
|
2. Call del_foo, if it exists.
|
|
|
|
3. Call delFoo, if it exists.
|
|
|
|
4. Delete foo element, if foo is in sub_interfaces.
|
|
|
|
5. Delete top level XML attribute named foo.
|
|
|
|
6. Remove the foo plugin, if it was loaded.
|
|
|
|
7. Do nothing.
|
2010-08-20 00:41:26 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
attrib -- The name of the affected stanza interface.
|
2010-08-19 23:11:12 +00:00
|
|
|
"""
|
|
|
|
if attrib in self.interfaces:
|
2010-10-18 01:38:22 +00:00
|
|
|
del_method = "del_%s" % attrib.lower()
|
|
|
|
del_method2 = "del%s" % attrib.title()
|
Allow a stanza plugin to override a parent's interfaces.
Each interface, say foo, may be overridden in three ways:
set_foo
get_foo
del_foo
To declare an override in a plugin, add the class field
overrides as so:
overrides = ['set_foo', 'del_foo']
Each override must have a matching set_foo(), etc method
for implementing the new behaviour.
To enable the overrides for a particular parent stanza,
pass the option overrides=True to register_stanza_plugin.
register_stanza_plugin(Stanza, Plugin, overrides=True)
Example code:
class Test(ElementBase):
name = 'test'
namespace = 'testing'
interfaces = set(('foo', 'bar'))
sub_interfaces = set(('bar',))
class TestOverride(ElementBase):
name = 'test-override'
namespace = 'testing'
plugin_attrib = 'override'
interfaces = set(('foo',))
overrides = ['set_foo']
def setup(self, xml):
# Don't include an XML element in the parent stanza
# since we're adding just an attribute.
# If adding a regular subelement, no need to do this.
self.xml = ET.Element('')
def set_foo(self, value):
print("overrides!")
self.parent()._set_attr('foo', 'override-%s' % value)
register_stanza_plugin(Test, TestOverride, overrides=True)
Example usage:
>>> t = TestStanza()
>>> t['foo'] = 'bar'
>>> t['foo']
'override-bar'
2011-03-24 16:25:17 +00:00
|
|
|
|
|
|
|
if self.plugin_overrides:
|
|
|
|
plugin = self.plugin_overrides.get(del_method, None)
|
|
|
|
if plugin:
|
|
|
|
if plugin not in self.plugins:
|
|
|
|
self.init_plugin(plugin)
|
|
|
|
handler = getattr(self.plugins[plugin], del_method, None)
|
|
|
|
if handler:
|
|
|
|
return handler()
|
|
|
|
|
2010-08-19 23:11:12 +00:00
|
|
|
if hasattr(self, del_method):
|
|
|
|
getattr(self, del_method)()
|
2010-10-18 01:38:22 +00:00
|
|
|
elif hasattr(self, del_method2):
|
|
|
|
getattr(self, del_method2)()
|
2010-08-19 23:11:12 +00:00
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._del_sub(attrib)
|
2010-08-19 23:11:12 +00:00
|
|
|
else:
|
2010-10-18 01:38:22 +00:00
|
|
|
self._del_attr(attrib)
|
2010-08-19 23:11:12 +00:00
|
|
|
elif attrib in self.plugin_attrib_map:
|
|
|
|
if attrib in self.plugins:
|
2010-10-07 23:42:28 +00:00
|
|
|
xml = self.plugins[attrib].xml
|
2011-01-20 00:49:13 +00:00
|
|
|
if self.plugins[attrib].is_extension:
|
|
|
|
del self.plugins[attrib][attrib]
|
2010-08-19 23:11:12 +00:00
|
|
|
del self.plugins[attrib]
|
2011-01-20 00:49:13 +00:00
|
|
|
try:
|
|
|
|
self.xml.remove(xml)
|
|
|
|
except:
|
|
|
|
pass
|
2010-08-19 23:11:12 +00:00
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _set_attr(self, name, value):
|
2010-08-20 00:41:26 +00:00
|
|
|
"""
|
|
|
|
Set the value of a top level attribute of the underlying XML object.
|
|
|
|
|
|
|
|
If the new value is None or an empty string, then the attribute will
|
|
|
|
be removed.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the attribute.
|
|
|
|
value -- The new value of the attribute, or None or '' to
|
|
|
|
remove it.
|
|
|
|
"""
|
|
|
|
if value is None or value == '':
|
|
|
|
self.__delitem__(name)
|
|
|
|
else:
|
|
|
|
self.xml.attrib[name] = value
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _del_attr(self, name):
|
2010-08-20 00:41:26 +00:00
|
|
|
"""
|
|
|
|
Remove a top level attribute of the underlying XML object.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the attribute.
|
|
|
|
"""
|
|
|
|
if name in self.xml.attrib:
|
|
|
|
del self.xml.attrib[name]
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _get_attr(self, name, default=''):
|
2010-08-20 00:41:26 +00:00
|
|
|
"""
|
|
|
|
Return the value of a top level attribute of the underlying
|
|
|
|
XML object.
|
|
|
|
|
|
|
|
In case the attribute has not been set, a default value can be
|
|
|
|
returned instead. An empty string is returned if no other default
|
|
|
|
is supplied.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name of the attribute.
|
|
|
|
default -- Optional value to return if the attribute has not
|
|
|
|
been set. An empty string is returned otherwise.
|
|
|
|
"""
|
|
|
|
return self.xml.attrib.get(name, default)
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _get_sub_text(self, name, default=''):
|
2010-08-24 13:44:09 +00:00
|
|
|
"""
|
|
|
|
Return the text contents of a sub element.
|
|
|
|
|
|
|
|
In case the element does not exist, or it has no textual content,
|
2010-08-26 14:08:22 +00:00
|
|
|
a default value can be returned instead. An empty string is returned
|
2010-08-24 13:44:09 +00:00
|
|
|
if no other default is supplied.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name or XPath expression of the element.
|
|
|
|
default -- Optional default to return if the element does
|
|
|
|
not exists. An empty string is returned otherwise.
|
|
|
|
"""
|
|
|
|
name = self._fix_ns(name)
|
|
|
|
stanza = self.xml.find(name)
|
|
|
|
if stanza is None or stanza.text is None:
|
|
|
|
return default
|
|
|
|
else:
|
|
|
|
return stanza.text
|
2010-08-24 12:55:37 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _set_sub_text(self, name, text=None, keep=False):
|
2010-08-24 13:37:42 +00:00
|
|
|
"""
|
|
|
|
Set the text contents of a sub element.
|
|
|
|
|
|
|
|
In case the element does not exist, a element will be created,
|
|
|
|
and its text contents will be set.
|
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
If the text is set to an empty string, or None, then the
|
2010-08-24 13:37:42 +00:00
|
|
|
element will be removed, unless keep is set to True.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name or XPath expression of the element.
|
|
|
|
text -- The new textual content of the element. If the text
|
|
|
|
is an empty string or None, the element will be removed
|
|
|
|
unless the parameter keep is True.
|
|
|
|
keep -- Indicates if the element should be kept if its text is
|
|
|
|
removed. Defaults to False.
|
|
|
|
"""
|
2010-08-26 22:18:00 +00:00
|
|
|
path = self._fix_ns(name, split=True)
|
2010-08-24 13:37:42 +00:00
|
|
|
element = self.xml.find(name)
|
|
|
|
|
|
|
|
if not text and not keep:
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._del_sub(name)
|
2010-08-24 13:37:42 +00:00
|
|
|
|
|
|
|
if element is None:
|
|
|
|
# We need to add the element. If the provided name was
|
|
|
|
# an XPath expression, some of the intermediate elements
|
|
|
|
# may already exist. If so, we want to use those instead
|
|
|
|
# of generating new elements.
|
|
|
|
last_xml = self.xml
|
|
|
|
walked = []
|
2010-08-26 22:18:00 +00:00
|
|
|
for ename in path:
|
2010-08-24 13:37:42 +00:00
|
|
|
walked.append(ename)
|
|
|
|
element = self.xml.find("/".join(walked))
|
|
|
|
if element is None:
|
|
|
|
element = ET.Element(ename)
|
|
|
|
last_xml.append(element)
|
|
|
|
last_xml = element
|
|
|
|
element = last_xml
|
|
|
|
|
|
|
|
element.text = text
|
2010-08-26 14:08:22 +00:00
|
|
|
return element
|
2010-08-24 13:37:42 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def _del_sub(self, name, all=False):
|
2010-08-25 14:52:07 +00:00
|
|
|
"""
|
|
|
|
Remove sub elements that match the given name or XPath.
|
|
|
|
|
|
|
|
If the element is in a path, then any parent elements that become
|
|
|
|
empty after deleting the element may also be deleted if requested
|
|
|
|
by setting all=True.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
name -- The name or XPath expression for the element(s) to remove.
|
2010-08-26 14:08:22 +00:00
|
|
|
all -- If True, remove all empty elements in the path to the
|
2010-08-25 14:52:07 +00:00
|
|
|
deleted element. Defaults to False.
|
|
|
|
"""
|
2010-08-26 22:18:00 +00:00
|
|
|
path = self._fix_ns(name, split=True)
|
2010-08-25 14:52:07 +00:00
|
|
|
original_target = path[-1]
|
|
|
|
|
|
|
|
for level, _ in enumerate(path):
|
|
|
|
# Generate the paths to the target elements and their parent.
|
|
|
|
element_path = "/".join(path[:len(path) - level])
|
|
|
|
parent_path = "/".join(path[:len(path) - level - 1])
|
|
|
|
|
|
|
|
elements = self.xml.findall(element_path)
|
|
|
|
parent = self.xml.find(parent_path)
|
|
|
|
|
|
|
|
if elements:
|
|
|
|
if parent is None:
|
|
|
|
parent = self.xml
|
|
|
|
for element in elements:
|
2010-08-26 17:55:23 +00:00
|
|
|
if element.tag == original_target or \
|
|
|
|
not element.getchildren():
|
|
|
|
# Only delete the originally requested elements, and
|
|
|
|
# any parent elements that have become empty.
|
2010-08-25 14:52:07 +00:00
|
|
|
parent.remove(element)
|
|
|
|
if not all:
|
|
|
|
# If we don't want to delete elements up the tree, stop
|
|
|
|
# after deleting the first level of elements.
|
|
|
|
return
|
2010-08-26 14:08:22 +00:00
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
def match(self, xpath):
|
|
|
|
"""
|
|
|
|
Compare a stanza object with an XPath expression. If the XPath matches
|
|
|
|
the contents of the stanza object, the match is successful.
|
|
|
|
|
|
|
|
The XPath expression may include checks for stanza attributes.
|
|
|
|
For example:
|
|
|
|
presence@show=xa@priority=2/status
|
2010-08-26 14:08:22 +00:00
|
|
|
Would match a presence stanza whose show value is set to 'xa', has a
|
2010-08-25 18:40:16 +00:00
|
|
|
priority value of '2', and has a status element.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xpath -- The XPath expression to check against. It may be either a
|
|
|
|
string or a list of element names with attribute checks.
|
|
|
|
"""
|
|
|
|
if isinstance(xpath, str):
|
2010-08-30 18:55:30 +00:00
|
|
|
xpath = self._fix_ns(xpath, split=True, propagate_ns=False)
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
# Extract the tag name and attribute checks for the first XPath node.
|
|
|
|
components = xpath[0].split('@')
|
|
|
|
tag = components[0]
|
|
|
|
attributes = components[1:]
|
2010-08-26 14:08:22 +00:00
|
|
|
|
2010-09-01 18:20:34 +00:00
|
|
|
if tag not in (self.name, "{%s}%s" % (self.namespace, self.name)) and \
|
|
|
|
tag not in self.plugins and tag not in self.plugin_attrib:
|
2010-08-25 18:40:16 +00:00
|
|
|
# The requested tag is not in this stanza, so no match.
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check the rest of the XPath against any substanzas.
|
|
|
|
matched_substanzas = False
|
|
|
|
for substanza in self.iterables:
|
|
|
|
if xpath[1:] == []:
|
|
|
|
break
|
|
|
|
matched_substanzas = substanza.match(xpath[1:])
|
2010-08-26 14:08:22 +00:00
|
|
|
if matched_substanzas:
|
2010-08-25 18:40:16 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Check attribute values.
|
|
|
|
for attribute in attributes:
|
|
|
|
name, value = attribute.split('=')
|
|
|
|
if self[name] != value:
|
|
|
|
return False
|
|
|
|
|
2010-08-30 21:12:10 +00:00
|
|
|
# Check sub interfaces.
|
|
|
|
if len(xpath) > 1:
|
|
|
|
next_tag = xpath[1]
|
|
|
|
if next_tag in self.sub_interfaces and self[next_tag]:
|
|
|
|
return True
|
|
|
|
|
2010-08-25 18:40:16 +00:00
|
|
|
# Attempt to continue matching the XPath using the stanza's plugins.
|
|
|
|
if not matched_substanzas and len(xpath) > 1:
|
2010-08-25 18:54:09 +00:00
|
|
|
# Convert {namespace}tag@attribs to just tag
|
|
|
|
next_tag = xpath[1].split('@')[0].split('}')[-1]
|
2010-08-25 18:40:16 +00:00
|
|
|
if next_tag in self.plugins:
|
|
|
|
return self.plugins[next_tag].match(xpath[1:])
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Everything matched.
|
|
|
|
return True
|
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def find(self, xpath):
|
|
|
|
"""
|
|
|
|
Find an XML object in this stanza given an XPath expression.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Exposes ElementTree interface for backwards compatibility.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Note that matching on attribute values is not supported in Python 2.6
|
|
|
|
or Python 3.1
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Arguments:
|
|
|
|
xpath -- An XPath expression matching a single desired element.
|
|
|
|
"""
|
|
|
|
return self.xml.find(xpath)
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def findall(self, xpath):
|
|
|
|
"""
|
|
|
|
Find multiple XML objects in this stanza given an XPath expression.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Exposes ElementTree interface for backwards compatibility.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Note that matching on attribute values is not supported in Python 2.6
|
|
|
|
or Python 3.1.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xpath -- An XPath expression matching multiple desired elements.
|
|
|
|
"""
|
|
|
|
return self.xml.findall(xpath)
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
"""
|
|
|
|
Return the value of a stanza interface. If the found value is None
|
|
|
|
or an empty string, return the supplied default value.
|
|
|
|
|
|
|
|
Allows stanza objects to be used like dictionaries.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
key -- The name of the stanza interface to check.
|
|
|
|
default -- Value to return if the stanza interface has a value
|
|
|
|
of None or "". Will default to returning None.
|
|
|
|
"""
|
|
|
|
value = self[key]
|
|
|
|
if value is None or value == '':
|
|
|
|
return default
|
|
|
|
return value
|
2010-08-13 14:15:52 +00:00
|
|
|
|
|
|
|
def keys(self):
|
2010-08-26 14:08:22 +00:00
|
|
|
"""
|
|
|
|
Return the names of all stanza interfaces provided by the
|
|
|
|
stanza object.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Allows stanza objects to be used like dictionaries.
|
|
|
|
"""
|
|
|
|
out = []
|
|
|
|
out += [x for x in self.interfaces]
|
|
|
|
out += [x for x in self.plugins]
|
|
|
|
if self.iterables:
|
|
|
|
out.append('substanzas')
|
2010-08-26 17:49:36 +00:00
|
|
|
return out
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def append(self, item):
|
|
|
|
"""
|
|
|
|
Append either an XML object or a substanza to this stanza object.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
If a substanza object is appended, it will be added to the list
|
|
|
|
of iterable stanzas.
|
|
|
|
|
|
|
|
Allows stanza objects to be used like lists.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
item -- Either an XML object or a stanza object to add to
|
|
|
|
this stanza's contents.
|
|
|
|
"""
|
|
|
|
if not isinstance(item, ElementBase):
|
|
|
|
if type(item) == XML_TYPE:
|
|
|
|
return self.appendxml(item)
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
self.xml.append(item.xml)
|
|
|
|
self.iterables.append(item)
|
|
|
|
return self
|
2010-08-13 14:15:52 +00:00
|
|
|
|
|
|
|
def appendxml(self, xml):
|
2010-08-26 14:08:22 +00:00
|
|
|
"""
|
|
|
|
Append an XML object to the stanza's XML.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
The added XML will not be included in the list of
|
|
|
|
iterable substanzas.
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
Arguments:
|
|
|
|
xml -- The XML object to add to the stanza.
|
|
|
|
"""
|
|
|
|
self.xml.append(xml)
|
|
|
|
return self
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def pop(self, index=0):
|
|
|
|
"""
|
|
|
|
Remove and return the last substanza in the list of
|
|
|
|
iterable substanzas.
|
|
|
|
|
|
|
|
Allows stanza objects to be used like lists.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
index -- The index of the substanza to remove.
|
|
|
|
"""
|
|
|
|
substanza = self.iterables.pop(index)
|
|
|
|
self.xml.remove(substanza.xml)
|
|
|
|
return substanza
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
"""
|
|
|
|
Return the next iterable substanza.
|
|
|
|
"""
|
|
|
|
return self.__next__()
|
|
|
|
|
2011-01-26 16:27:41 +00:00
|
|
|
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)
|
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
@property
|
|
|
|
def attrib(self):
|
|
|
|
"""
|
|
|
|
DEPRECATED
|
|
|
|
|
|
|
|
For backwards compatibility, stanza.attrib returns the stanza itself.
|
|
|
|
|
|
|
|
Older implementations of stanza objects used XML objects directly,
|
|
|
|
requiring the use of .attrib to access attribute values.
|
|
|
|
|
|
|
|
Use of the dictionary syntax with the stanza object itself for
|
|
|
|
accessing stanza interfaces is preferred.
|
|
|
|
"""
|
|
|
|
return self
|
2010-08-13 14:15:52 +00:00
|
|
|
|
2010-08-30 18:55:30 +00:00
|
|
|
def _fix_ns(self, xpath, split=False, propagate_ns=True):
|
2010-08-24 12:55:37 +00:00
|
|
|
"""
|
|
|
|
Apply the stanza's namespace to elements in an XPath expression.
|
|
|
|
|
|
|
|
Arguments:
|
2010-08-30 18:55:30 +00:00
|
|
|
xpath -- The XPath expression to fix with namespaces.
|
|
|
|
split -- Indicates if the fixed XPath should be left as a
|
|
|
|
list of element names with namespaces. Defaults to
|
|
|
|
False, which returns a flat string path.
|
|
|
|
propagate_ns -- Overrides propagating parent element namespaces
|
|
|
|
to child elements. Useful if you wish to simply
|
|
|
|
split an XPath that has non-specified namespaces,
|
|
|
|
and child and parent namespaces are known not to
|
|
|
|
always match. Defaults to True.
|
2010-08-26 22:18:00 +00:00
|
|
|
"""
|
|
|
|
fixed = []
|
2010-08-26 22:40:58 +00:00
|
|
|
# Split the XPath into a series of blocks, where a block
|
|
|
|
# is started by an element with a namespace.
|
2010-08-26 22:18:00 +00:00
|
|
|
ns_blocks = xpath.split('{')
|
|
|
|
for ns_block in ns_blocks:
|
|
|
|
if '}' in ns_block:
|
2010-08-26 22:40:58 +00:00
|
|
|
# Apply the found namespace to following elements
|
|
|
|
# that do not have namespaces.
|
2010-08-26 22:18:00 +00:00
|
|
|
namespace = ns_block.split('}')[0]
|
|
|
|
elements = ns_block.split('}')[1].split('/')
|
2010-09-01 18:20:34 +00:00
|
|
|
else:
|
2010-08-26 22:40:58 +00:00
|
|
|
# Apply the stanza's namespace to the following
|
|
|
|
# elements since no namespace was provided.
|
2010-08-26 22:18:00 +00:00
|
|
|
namespace = self.namespace
|
|
|
|
elements = ns_block.split('/')
|
|
|
|
|
|
|
|
for element in elements:
|
|
|
|
if element:
|
2010-08-26 22:40:58 +00:00
|
|
|
# Skip empty entry artifacts from splitting.
|
2010-08-30 19:25:59 +00:00
|
|
|
if propagate_ns:
|
2010-08-30 18:55:30 +00:00
|
|
|
tag = '{%s}%s' % (namespace, element)
|
|
|
|
else:
|
|
|
|
tag = element
|
|
|
|
fixed.append(tag)
|
2010-08-26 22:18:00 +00:00
|
|
|
if split:
|
|
|
|
return fixed
|
|
|
|
return '/'.join(fixed)
|
2010-08-24 12:55:37 +00:00
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
"""
|
|
|
|
Compare the stanza object with another to test for equality.
|
|
|
|
|
|
|
|
Stanzas are equal if their interfaces return the same values,
|
|
|
|
and if they are both instances of ElementBase.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
other -- The stanza object to compare against.
|
|
|
|
"""
|
|
|
|
if not isinstance(other, ElementBase):
|
|
|
|
return False
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
# Check that this stanza is a superset of the other stanza.
|
2011-01-26 16:27:41 +00:00
|
|
|
values = self.values
|
2010-08-26 17:49:36 +00:00
|
|
|
for key in other.keys():
|
2010-08-26 14:08:22 +00:00
|
|
|
if key not in values or values[key] != other[key]:
|
|
|
|
return False
|
2010-08-26 17:49:36 +00:00
|
|
|
|
|
|
|
# Check that the other stanza is a superset of this stanza.
|
2011-01-26 16:27:41 +00:00
|
|
|
values = other.values
|
2010-08-26 17:49:36 +00:00
|
|
|
for key in self.keys():
|
|
|
|
if key not in values or values[key] != self[key]:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Both stanzas are supersets of each other, therefore they
|
|
|
|
# must be equal.
|
2010-08-26 14:08:22 +00:00
|
|
|
return True
|
|
|
|
|
2010-08-26 17:49:36 +00:00
|
|
|
def __ne__(self, other):
|
|
|
|
"""
|
|
|
|
Compare the stanza object with another to test for inequality.
|
|
|
|
|
|
|
|
Stanzas are not equal if their interfaces return different values,
|
|
|
|
or if they are not both instances of ElementBase.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
other -- The stanza object to compare against.
|
|
|
|
"""
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
2010-08-26 14:08:22 +00:00
|
|
|
def __bool__(self):
|
|
|
|
"""
|
|
|
|
Stanza objects should be treated as True in boolean contexts.
|
|
|
|
|
|
|
|
Python 3.x version.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
"""
|
|
|
|
Stanza objects should be treated as True in boolean contexts.
|
|
|
|
|
|
|
|
Python 2.x version.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
"""
|
|
|
|
Return the number of iterable substanzas contained in this stanza.
|
|
|
|
"""
|
|
|
|
return len(self.iterables)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
Return an iterator object for iterating over the stanza's substanzas.
|
|
|
|
|
|
|
|
The iterator is the stanza object itself. Attempting to use two
|
|
|
|
iterators on the same stanza at the same time is discouraged.
|
|
|
|
"""
|
|
|
|
self._index = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
"""
|
|
|
|
Return the next iterable substanza.
|
|
|
|
"""
|
|
|
|
self._index += 1
|
|
|
|
if self._index > len(self.iterables):
|
|
|
|
self._index = 0
|
|
|
|
raise StopIteration
|
|
|
|
return self.iterables[self._index - 1]
|
|
|
|
|
|
|
|
def __copy__(self):
|
|
|
|
"""
|
|
|
|
Return a copy of the stanza object that does not share the same
|
|
|
|
underlying XML object.
|
|
|
|
"""
|
|
|
|
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
|
|
|
|
2011-03-18 21:34:07 +00:00
|
|
|
def __str__(self, top_level_ns=True):
|
2010-08-26 14:08:22 +00:00
|
|
|
"""
|
|
|
|
Return a string serialization of the underlying XML object.
|
2011-03-18 21:34:07 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
top_level_ns -- Display the top-most namespace.
|
|
|
|
Defaults to True.
|
2010-08-26 14:08:22 +00:00
|
|
|
"""
|
2011-03-18 21:34:07 +00:00
|
|
|
stanza_ns = '' if top_level_ns else self.namespace
|
2011-01-27 23:05:05 +00:00
|
|
|
return tostring(self.xml, xmlns='',
|
2011-03-18 21:39:43 +00:00
|
|
|
stanza_ns=stanza_ns,
|
2011-08-05 05:37:22 +00:00
|
|
|
top_level=not top_level_ns)
|
2010-08-26 14:08:22 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
"""
|
|
|
|
Use the stanza's serialized XML as its representation.
|
|
|
|
"""
|
|
|
|
return self.__str__()
|
|
|
|
|
2009-12-11 01:29:46 +00:00
|
|
|
|
|
|
|
class StanzaBase(ElementBase):
|
2010-08-27 15:07:20 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
StanzaBase provides the foundation for all other stanza objects used by
|
|
|
|
SleekXMPP, and defines a basic set of interfaces common to nearly
|
|
|
|
all stanzas. These interfaces are the 'id', 'type', 'to', and 'from'
|
|
|
|
attributes. An additional interface, 'payload', is available to access
|
|
|
|
the XML contents of the stanza. Most stanza objects will provided more
|
|
|
|
specific interfaces, however.
|
|
|
|
|
|
|
|
Stanza Interface:
|
|
|
|
from -- A JID object representing the sender's JID.
|
|
|
|
id -- An optional id value that can be used to associate stanzas
|
|
|
|
with their replies.
|
|
|
|
payload -- The XML contents of the stanza.
|
|
|
|
to -- A JID object representing the recipient's JID.
|
|
|
|
type -- The type of stanza, typically will be 'normal', 'error',
|
|
|
|
'get', or 'set', etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
stream -- The XMLStream instance that will handle sending this stanza.
|
|
|
|
|
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
set_type -- Set the type of the stanza.
|
|
|
|
get_to -- Return the stanza recipients JID.
|
|
|
|
set_to -- Set the stanza recipient's JID.
|
|
|
|
get_from -- Return the stanza sender's JID.
|
|
|
|
set_from -- Set the stanza sender's JID.
|
|
|
|
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.
|
|
|
|
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'.
|
|
|
|
unhandled -- Callback for when the stanza is not handled by a
|
|
|
|
stream handler.
|
|
|
|
exception -- Callback for if an exception is raised while
|
|
|
|
handling the stanza.
|
|
|
|
send -- Send the stanza using the stanza's stream.
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
|
2010-08-26 18:19:36 +00:00
|
|
|
name = 'stanza'
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
|
|
|
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
|
|
|
sub_interfaces = tuple()
|
|
|
|
|
|
|
|
def __init__(self, stream=None, xml=None, stype=None,
|
|
|
|
sto=None, sfrom=None, sid=None):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Create a new stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
stream -- Optional XMLStream responsible for sending this stanza.
|
|
|
|
xml -- Optional XML contents to initialize stanza values.
|
|
|
|
stype -- Optional stanza type value.
|
|
|
|
sto -- Optional string or JID object of the recipient's JID.
|
|
|
|
sfrom -- Optional string or JID object of the sender's JID.
|
|
|
|
sid -- Optional ID value for the stanza.
|
|
|
|
"""
|
2010-08-26 18:19:36 +00:00
|
|
|
self.stream = stream
|
|
|
|
if stream is not None:
|
|
|
|
self.namespace = stream.default_ns
|
|
|
|
ElementBase.__init__(self, xml)
|
|
|
|
if stype is not None:
|
|
|
|
self['type'] = stype
|
|
|
|
if sto is not None:
|
|
|
|
self['to'] = sto
|
|
|
|
if sfrom is not None:
|
|
|
|
self['from'] = sfrom
|
|
|
|
self.tag = "{%s}%s" % (self.namespace, self.name)
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_type(self, value):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Set the stanza's 'type' attribute.
|
|
|
|
|
|
|
|
Only type values contained in StanzaBase.types are accepted.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- One of the values contained in StanzaBase.types
|
|
|
|
"""
|
2010-08-26 18:19:36 +00:00
|
|
|
if value in self.types:
|
|
|
|
self.xml.attrib['type'] = value
|
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_to(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""Return the value of the stanza's 'to' attribute."""
|
2010-10-18 01:38:22 +00:00
|
|
|
return JID(self._get_attr('to'))
|
2010-08-27 15:07:20 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_to(self, value):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Set the 'to' attribute of the stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value -- A string or JID object representing the recipient's JID.
|
|
|
|
"""
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._set_attr('to', str(value))
|
2010-08-27 15:07:20 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_from(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""Return the value of the stanza's 'from' attribute."""
|
2010-10-18 01:38:22 +00:00
|
|
|
return JID(self._get_attr('from'))
|
2010-08-27 15:07:20 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_from(self, value):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Set the 'from' attribute of the stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
from -- A string or JID object representing the sender's JID.
|
|
|
|
"""
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._set_attr('from', str(value))
|
2010-08-27 15:07:20 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_payload(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""Return a list of XML objects contained in the stanza."""
|
2010-08-26 18:19:36 +00:00
|
|
|
return self.xml.getchildren()
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_payload(self, value):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Add XML content to the stanza.
|
|
|
|
|
|
|
|
Arguments:
|
2010-08-27 16:22:35 +00:00
|
|
|
value -- Either an XML or a stanza object, or a list
|
|
|
|
of XML or stanza objects.
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
2010-08-27 16:22:35 +00:00
|
|
|
if not isinstance(value, list):
|
|
|
|
value = [value]
|
|
|
|
for val in value:
|
|
|
|
self.append(val)
|
2010-08-26 18:19:36 +00:00
|
|
|
return self
|
2010-08-06 00:26:41 +00:00
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_payload(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""Remove the XML contents of the stanza."""
|
2010-08-26 18:19:36 +00:00
|
|
|
self.clear()
|
|
|
|
return self
|
2010-08-06 00:26:41 +00:00
|
|
|
|
2011-02-11 20:20:26 +00:00
|
|
|
def reply(self, clear=True):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
2011-02-11 20:20:26 +00:00
|
|
|
Swap the 'from' and 'to' attributes to prepare the stanza for
|
|
|
|
sending a reply. If clear=True, then also remove the stanza's
|
|
|
|
contents to make room for the reply content.
|
2010-08-27 15:07:20 +00:00
|
|
|
|
|
|
|
For client streams, the 'from' attribute is removed.
|
2011-02-11 20:20:26 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
clear -- Indicates if the stanza's contents should be
|
|
|
|
removed. Defaults to True
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
2010-08-26 18:19:36 +00:00
|
|
|
# if it's a component, use from
|
|
|
|
if self.stream and hasattr(self.stream, "is_component") and \
|
|
|
|
self.stream.is_component:
|
|
|
|
self['from'], self['to'] = self['to'], self['from']
|
|
|
|
else:
|
|
|
|
self['to'] = self['from']
|
|
|
|
del self['from']
|
2011-02-11 20:20:26 +00:00
|
|
|
if clear:
|
|
|
|
self.clear()
|
2010-08-26 18:19:36 +00:00
|
|
|
return self
|
2010-08-06 00:26:41 +00:00
|
|
|
|
2010-08-26 18:19:36 +00:00
|
|
|
def error(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""Set the stanza's type to 'error'."""
|
2010-08-26 18:19:36 +00:00
|
|
|
self['type'] = 'error'
|
|
|
|
return self
|
2010-08-06 00:26:41 +00:00
|
|
|
|
2010-08-26 18:19:36 +00:00
|
|
|
def unhandled(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Called when no handlers have been registered to process this
|
|
|
|
stanza.
|
|
|
|
|
|
|
|
Meant to be overridden.
|
|
|
|
"""
|
2010-08-26 18:19:36 +00:00
|
|
|
pass
|
2010-08-06 00:26:41 +00:00
|
|
|
|
2010-08-26 18:19:36 +00:00
|
|
|
def exception(self, e):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Handle exceptions raised during stanza processing.
|
|
|
|
|
|
|
|
Meant to be overridden.
|
|
|
|
"""
|
2011-11-19 20:07:57 +00:00
|
|
|
log.exception('Error handling {%s}%s stanza', self.namespace,
|
|
|
|
self.name)
|
2010-08-26 18:19:36 +00:00
|
|
|
|
2011-05-27 23:59:52 +00:00
|
|
|
def send(self, now=False):
|
|
|
|
"""
|
|
|
|
Queue the stanza to be sent on the XML stream.
|
|
|
|
Arguments:
|
|
|
|
now -- Indicates if the queue should be skipped and the
|
|
|
|
stanza sent immediately. Useful for stream
|
|
|
|
initialization. Defaults to False.
|
|
|
|
"""
|
|
|
|
self.stream.send_raw(self.__str__(), now=now)
|
2010-08-26 18:19:36 +00:00
|
|
|
|
|
|
|
def __copy__(self):
|
2010-08-27 15:07:20 +00:00
|
|
|
"""
|
|
|
|
Return a copy of the stanza object that does not share the
|
|
|
|
same underlying XML object, but does share the same XML stream.
|
|
|
|
"""
|
|
|
|
return self.__class__(xml=copy.deepcopy(self.xml),
|
|
|
|
stream=self.stream)
|
2010-08-26 18:19:36 +00:00
|
|
|
|
2011-03-18 21:34:07 +00:00
|
|
|
def __str__(self, top_level_ns=False):
|
|
|
|
"""
|
|
|
|
Serialize the stanza's XML to a string.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
top_level_ns -- Display the top-most namespace.
|
|
|
|
Defaults to False.
|
|
|
|
"""
|
|
|
|
stanza_ns = '' if top_level_ns else self.namespace
|
2010-08-26 18:19:36 +00:00
|
|
|
return tostring(self.xml, xmlns='',
|
2011-03-18 21:34:07 +00:00
|
|
|
stanza_ns=stanza_ns,
|
2011-01-27 23:05:05 +00:00
|
|
|
stream=self.stream,
|
2011-08-05 05:37:22 +00:00
|
|
|
top_level=not top_level_ns)
|
2011-02-14 18:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
ElementBase.initPlugin = ElementBase.init_plugin
|
|
|
|
ElementBase._getAttr = ElementBase._get_attr
|
|
|
|
ElementBase._setAttr = ElementBase._set_attr
|
|
|
|
ElementBase._delAttr = ElementBase._del_attr
|
|
|
|
ElementBase._getSubText = ElementBase._get_sub_text
|
|
|
|
ElementBase._setSubText = ElementBase._set_sub_text
|
|
|
|
ElementBase._delSub = ElementBase._del_sub
|
|
|
|
ElementBase.getStanzaValues = ElementBase._get_stanza_values
|
|
|
|
ElementBase.setStanzaValues = ElementBase._set_stanza_values
|
|
|
|
|
|
|
|
StanzaBase.setType = StanzaBase.set_type
|
|
|
|
StanzaBase.getTo = StanzaBase.get_to
|
|
|
|
StanzaBase.setTo = StanzaBase.set_to
|
|
|
|
StanzaBase.getFrom = StanzaBase.get_from
|
|
|
|
StanzaBase.setFrom = StanzaBase.set_from
|
|
|
|
StanzaBase.getPayload = StanzaBase.get_payload
|
|
|
|
StanzaBase.setPayload = StanzaBase.set_payload
|
|
|
|
StanzaBase.delPayload = StanzaBase.del_payload
|