Updated RootStanza to use registerStanzaPlugin, and be PEP8 compliant.

This commit is contained in:
Lance Stout 2010-07-28 13:14:41 -04:00
parent aa02ecd154
commit bd92ef6acf
2 changed files with 56 additions and 25 deletions

View file

@ -5,4 +5,9 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
__all__ = ['presence']
from sleekxmpp.stanza.error import Error
from sleekxmpp.stanza.iq import Iq
from sleekxmpp.stanza.message import Message
from sleekxmpp.stanza.presence import Presence

View file

@ -5,26 +5,53 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from .. xmlstream.stanzabase import StanzaBase
from xml.etree import cElementTree as ET
from . error import Error
from .. exceptions import XMPPError
import logging import logging
import traceback import traceback
import sys import sys
from sleekxmpp.exceptions import XMPPError
from sleekxmpp.stanza import Error
from sleekxmpp.xmlstream.stanzabase import ET, StanzaBase, registerStanzaPlugin
class RootStanza(StanzaBase): class RootStanza(StanzaBase):
def exception(self, e): #called when a handler raises an exception """
A top-level XMPP stanza in an XMLStream.
The RootStanza class provides a more XMPP specific exception
handler than provided by the generic StanzaBase class.
Methods:
exception -- Overrides StanzaBase.exception
"""
def exception(self, e):
"""
Create and send an error reply.
Typically called when an event handler raises an exception.
The error's type and text content are based on the exception
object's type and content.
Overrides StanzaBase.exception.
Arguments:
e -- Exception object
"""
self.reply() self.reply()
if isinstance(e, XMPPError): # we raised this deliberately if isinstance(e, XMPPError):
# We raised this deliberately
self['error']['condition'] = e.condition self['error']['condition'] = e.condition
self['error']['text'] = e.text self['error']['text'] = e.text
if e.extension is not None: # extended error tag if e.extension is not None:
# Extended error tag
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension), e.extension_args) extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension), e.extension_args)
self['error'].xml.append(extxml) self['error'].append(extxml)
self['error']['type'] = e.etype self['error']['type'] = e.etype
else: # we probably didn't raise this on purpose, so send back a traceback else:
# We probably didn't raise this on purpose, so send back a traceback
self['error']['condition'] = 'undefined-condition' self['error']['condition'] = 'undefined-condition'
if sys.version_info < (3,0): if sys.version_info < (3,0):
self['error']['text'] = "SleekXMPP got into trouble." self['error']['text'] = "SleekXMPP got into trouble."
@ -33,6 +60,5 @@ class RootStanza(StanzaBase):
logging.exception('Error handling {%s}%s stanza' % (self.namespace, self.name)) logging.exception('Error handling {%s}%s stanza' % (self.namespace, self.name))
self.send() self.send()
# all jabber:client root stanzas should have the error plugin
RootStanza.plugin_attrib_map['error'] = Error registerStanzaPlugin(RootStanza, Error)
RootStanza.plugin_tag_map["{%s}%s" % (Error.namespace, Error.name)] = Error