Updated error stanza to be PEP8 compliant and include documentation.

This commit is contained in:
Lance Stout 2010-07-29 11:06:10 -04:00
parent d148f633f3
commit 25f43bd219

View file

@ -5,58 +5,127 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
class Error(ElementBase): class Error(ElementBase):
namespace = 'jabber:client'
name = 'error' """
plugin_attrib = 'error' XMPP stanzas of type 'error' should include an <error> stanza that
conditions = set(('bad-request', 'conflict', 'feature-not-implemented', 'forbidden', 'gone', 'internal-server-error', 'item-not-found', 'jid-malformed', 'not-acceptable', 'not-allowed', 'not-authorized', 'payment-required', 'recipient-unavailable', 'redirect', 'registration-required', 'remote-server-not-found', 'remote-server-timeout', 'resource-constraint', 'service-unavailable', 'subscription-required', 'undefined-condition', 'unexpected-request')) describes the nature of the error and how it should be handled.
interfaces = set(('code', 'condition', 'text', 'type'))
types = set(('cancel', 'continue', 'modify', 'auth', 'wait')) Use the 'XEP-0086: Error Condition Mappings' plugin to include error
sub_interfaces = set(('text',)) codes used in older XMPP versions.
condition_ns = 'urn:ietf:params:xml:ns:xmpp-stanzas'
Example error stanza:
def setup(self, xml=None): <error type="cancel" code="404">
if ElementBase.setup(self, xml): #if we had to generate xml <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
self['type'] = 'cancel' <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
self['condition'] = 'feature-not-implemented' The item was not found.
if self.parent is not None: </text>
self.parent()['type'] = 'error' </error>
def getCondition(self): Stanza Interface:
for child in self.xml.getchildren(): code -- The error code used in older XMPP versions.
if "{%s}" % self.condition_ns in child.tag: condition -- The name of the condition element.
return child.tag.split('}', 1)[-1] text -- Human readable description of the error.
return '' type -- Error type indicating how the error should be handled.
def setCondition(self, value): Attributes:
if value in self.conditions: conditions -- The set of allowable error condition elements.
for child in self.xml.getchildren(): condition_ns -- The namespace for the condition element.
if "{%s}" % self.condition_ns in child.tag: types -- A set of values indicating how the error
self.xml.remove(child) should be treated.
condition = ET.Element("{%s}%s" % (self.condition_ns, value))
self.xml.append(condition) Methods:
return self setup -- Overrides ElementBase.setup.
getCondition -- Retrieve the name of the condition element.
def delCondition(self): setCondition -- Add a condition element.
return self delCondition -- Remove the condition element.
getText -- Retrieve the contents of the <text> element.
def getText(self): setText -- Set the contents of the <text> element.
text = '' delText -- Remove the <text> element.
textxml = self.xml.find("{urn:ietf:params:xml:ns:xmpp-stanzas}text") """
if textxml is not None:
text = textxml.text namespace = 'jabber:client'
return text name = 'error'
plugin_attrib = 'error'
def setText(self, value): interfaces = set(('code', 'condition', 'text', 'type'))
self.delText() sub_interfaces = set(('text',))
textxml = ET.Element('{urn:ietf:params:xml:ns:xmpp-stanzas}text') conditions = set(('bad-request', 'conflict', 'feature-not-implemented',
textxml.text = value 'forbidden', 'gone', 'internal-server-error',
self.xml.append(textxml) 'item-not-found', 'jid-malformed', 'not-acceptable',
return self 'not-allowed', 'not-authorized', 'payment-required',
'recipient-unavailable', 'redirect',
def delText(self): 'registration-required', 'remote-server-not-found',
textxml = self.xml.find("{urn:ietf:params:xml:ns:xmpp-stanzas}text") 'remote-server-timeout', 'resource-constraint',
if textxml is not None: 'service-unavailable', 'subscription-required',
self.xml.remove(textxml) 'undefined-condition', 'unexpected-request'))
condition_ns = 'urn:ietf:params:xml:ns:xmpp-stanzas'
types = set(('cancel', 'continue', 'modify', 'auth', 'wait'))
def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides ElementBase.setup.
Sets a default error type and condition, and changes the
parent stanza's type to 'error'.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
if ElementBase.setup(self, xml):
#If we had to generate XML then set default values.
self['type'] = 'cancel'
self['condition'] = 'feature-not-implemented'
if self.parent is not None:
self.parent()['type'] = 'error'
def getCondition(self):
"""Return the condition element's name."""
for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag:
return child.tag.split('}', 1)[-1]
return ''
def setCondition(self, value):
"""
Set the tag name of the condition element.
Arguments:
value -- The tag name of the condition element.
"""
if value in self.conditions:
del self['condition']
self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
return self
def delCondition(self):
"""Remove the condition element."""
for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag:
self.xml.remove(child)
return self
def getText(self):
"""Retrieve the contents of the <text> element."""
return self._getSubText('{%s}text' % self.condition_ns)
def setText(self, value):
"""
Set the contents of the <text> element.
Arguments:
value -- The new contents for the <text> element.
"""
self._setSubText('{%s}text' % self.condition_ns, text=value)
return self
def delText(self):
"""Remove the <text> element."""
self._delSub('{%s}text' % self.condition_ns)
return self