From 0d326383799a7d7bb69fec9dcd1eaf9e1a64eab8 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 11 Feb 2011 15:20:26 -0500 Subject: XMPPError exceptions can keep a stanza's contents. This allows exceptions to include the original content of a stanza in the error response by including the parameter clear=False when raising the exception. --- sleekxmpp/stanza/iq.py | 8 ++++++-- sleekxmpp/stanza/message.py | 6 ++++-- sleekxmpp/stanza/presence.py | 8 ++++++-- sleekxmpp/stanza/rootstanza.py | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) (limited to 'sleekxmpp/stanza') diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py index c6aa64d0..841d282c 100644 --- a/sleekxmpp/stanza/iq.py +++ b/sleekxmpp/stanza/iq.py @@ -144,7 +144,7 @@ class Iq(RootStanza): self.xml.remove(child) return self - def reply(self): + def reply(self, clear=True): """ Send a reply stanza. @@ -152,9 +152,13 @@ class Iq(RootStanza): Sets the 'type' to 'result' in addition to the default StanzaBase.reply behavior. + + Arguments: + clear -- Indicates if existing content should be + removed before replying. Defaults to True. """ self['type'] = 'result' - StanzaBase.reply(self) + StanzaBase.reply(self, clear) return self def send(self, block=True, timeout=None, callback=None): diff --git a/sleekxmpp/stanza/message.py b/sleekxmpp/stanza/message.py index 66c74d8a..6f0cf212 100644 --- a/sleekxmpp/stanza/message.py +++ b/sleekxmpp/stanza/message.py @@ -104,7 +104,7 @@ class Message(RootStanza): self['type'] = 'normal' return self - def reply(self, body=None): + def reply(self, body=None, clear=True): """ Create a message reply. @@ -114,7 +114,9 @@ class Message(RootStanza): adds a message body if one is given. Arguments: - body -- Optional text content for the message. + body -- Optional text content for the message. + clear -- Indicates if existing content should be removed + before replying. Defaults to True. """ StanzaBase.reply(self) if self['type'] == 'groupchat': diff --git a/sleekxmpp/stanza/presence.py b/sleekxmpp/stanza/presence.py index 7dcd8f90..60dddf64 100644 --- a/sleekxmpp/stanza/presence.py +++ b/sleekxmpp/stanza/presence.py @@ -173,14 +173,18 @@ class Presence(RootStanza): # The priority is not a number: we consider it 0 as a default return 0 - def reply(self): + def reply(self, clear=True): """ Set the appropriate presence reply type. Overrides StanzaBase.reply. + + Arguments: + clear -- Indicates if the stanza contents should be removed + before replying. Defaults to True. """ if self['type'] == 'unsubscribe': self['type'] = 'unsubscribed' elif self['type'] == 'subscribe': self['type'] = 'subscribed' - return StanzaBase.reply(self) + return StanzaBase.reply(self, clear) diff --git a/sleekxmpp/stanza/rootstanza.py b/sleekxmpp/stanza/rootstanza.py index 8123c5f8..bc11476e 100644 --- a/sleekxmpp/stanza/rootstanza.py +++ b/sleekxmpp/stanza/rootstanza.py @@ -43,8 +43,8 @@ class RootStanza(StanzaBase): Arguments: e -- Exception object """ - self.reply() if isinstance(e, XMPPError): + self.reply(clear=e.clear) # We raised this deliberately self['error']['condition'] = e.condition self['error']['text'] = e.text @@ -56,6 +56,7 @@ class RootStanza(StanzaBase): self['error']['type'] = e.etype self.send() else: + self.reply() # We probably didn't raise this on purpose, so send an error stanza self['error']['condition'] = 'undefined-condition' self['error']['text'] = "SleekXMPP got into trouble." -- cgit v1.2.3 From ca2b4a188ac31d1bdf45ec244c950f7675414b38 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 12 Feb 2011 11:01:43 -0500 Subject: Return the registered callback when using iq.send(callback=foo). Allows for a callback to be canceled by unregistering the returned handler. --- sleekxmpp/stanza/iq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/stanza') diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py index 841d282c..7a8b997b 100644 --- a/sleekxmpp/stanza/iq.py +++ b/sleekxmpp/stanza/iq.py @@ -195,7 +195,7 @@ class Iq(RootStanza): once=True) self.stream.register_handler(handler) StanzaBase.send(self) - return None + return handler elif block and self['type'] in ('get', 'set'): waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id'])) self.stream.register_handler(waitfor) -- cgit v1.2.3 From 34f6195ca5a042ac61aa20af0b339a6654e2ffdd Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 13 Feb 2011 16:30:57 -0500 Subject: Return the name of the registered callback. Instead of the actual callback object, return just the name of the callback object created when using iq.send(callback=..). This will help prevent memory leaks by not keeping an additional reference to the object, but still allows for the callback to be canceled by using self.remove_handler("handler_name"). --- sleekxmpp/stanza/iq.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'sleekxmpp/stanza') diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py index 7a8b997b..330df6c2 100644 --- a/sleekxmpp/stanza/iq.py +++ b/sleekxmpp/stanza/iq.py @@ -189,13 +189,14 @@ class Iq(RootStanza): if timeout is None: timeout = self.stream.response_timeout if callback is not None and self['type'] in ('get', 'set'): - handler = Callback('IqCallback_%s' % self['id'], + handler_name = 'IqCallback_%s' % self['id'] + handler = Callback(handler_name, MatcherId(self['id']), callback, once=True) self.stream.register_handler(handler) StanzaBase.send(self) - return handler + return handler_name elif block and self['type'] in ('get', 'set'): waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id'])) self.stream.register_handler(waitfor) -- cgit v1.2.3 From 75584d7ad74b284d30164cde0b5efec2c845d207 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 14 Feb 2011 13:49:43 -0500 Subject: Remap old method names in a better way. This should prevent some reference cycles that will cause garbage collection issues. --- sleekxmpp/stanza/error.py | 19 ++++++++++--------- sleekxmpp/stanza/htmlim.py | 23 ++++++----------------- sleekxmpp/stanza/iq.py | 15 ++++++++------- sleekxmpp/stanza/message.py | 32 +++++++++++--------------------- sleekxmpp/stanza/nick.py | 23 ++++++----------------- sleekxmpp/stanza/presence.py | 30 ++++++++++-------------------- sleekxmpp/stanza/roster.py | 23 ++++++----------------- 7 files changed, 57 insertions(+), 108 deletions(-) (limited to 'sleekxmpp/stanza') diff --git a/sleekxmpp/stanza/error.py b/sleekxmpp/stanza/error.py index 09229bc6..5d1ce50d 100644 --- a/sleekxmpp/stanza/error.py +++ b/sleekxmpp/stanza/error.py @@ -77,15 +77,6 @@ class Error(ElementBase): Arguments: xml -- Use an existing XML object for the stanza's values. """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.getCondition = self.get_condition - self.setCondition = self.set_condition - self.delCondition = self.del_condition - self.getText = self.get_text - self.setText = self.set_text - self.delText = self.del_text - if ElementBase.setup(self, xml): #If we had to generate XML then set default values. self['type'] = 'cancel' @@ -139,3 +130,13 @@ class Error(ElementBase): """Remove the element.""" self._del_sub('{%s}text' % self.condition_ns) return self + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Error.getCondition = Error.get_condition +Error.setCondition = Error.set_condition +Error.delCondition = Error.del_condition +Error.getText = Error.get_text +Error.setText = Error.set_text +Error.delText = Error.del_text diff --git a/sleekxmpp/stanza/htmlim.py b/sleekxmpp/stanza/htmlim.py index 45868287..d21a74e1 100644 --- a/sleekxmpp/stanza/htmlim.py +++ b/sleekxmpp/stanza/htmlim.py @@ -46,23 +46,6 @@ class HTMLIM(ElementBase): interfaces = set(('body',)) plugin_attrib = name - def setup(self, xml=None): - """ - Populate the stanza object using an optional XML object. - - Overrides StanzaBase.setup. - - Arguments: - xml -- Use an existing XML object for the stanza's values. - """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setBody = self.set_body - self.getBody = self.get_body - self.delBody = self.del_body - - return ElementBase.setup(self, xml) - def set_body(self, html): """ Set the contents of the HTML body. @@ -95,3 +78,9 @@ class HTMLIM(ElementBase): register_stanza_plugin(Message, HTMLIM) + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +HTMLIM.setBody = HTMLIM.set_body +HTMLIM.getBody = HTMLIM.get_body +HTMLIM.delBody = HTMLIM.del_body diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py index 330df6c2..2bfbc7b1 100644 --- a/sleekxmpp/stanza/iq.py +++ b/sleekxmpp/stanza/iq.py @@ -75,13 +75,6 @@ class Iq(RootStanza): Overrides StanzaBase.__init__. """ StanzaBase.__init__(self, *args, **kwargs) - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setPayload = self.set_payload - self.getQuery = self.get_query - self.setQuery = self.set_query - self.delQuery = self.del_query - if self['id'] == '': if self.stream is not None: self['id'] = self.stream.getNewId() @@ -229,3 +222,11 @@ class Iq(RootStanza): else: StanzaBase._set_stanza_values(self, values) return self + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Iq.setPayload = Iq.set_payload +Iq.getQuery = Iq.get_query +Iq.setQuery = Iq.set_query +Iq.delQuery = Iq.del_query diff --git a/sleekxmpp/stanza/message.py b/sleekxmpp/stanza/message.py index 6f0cf212..cb3d344c 100644 --- a/sleekxmpp/stanza/message.py +++ b/sleekxmpp/stanza/message.py @@ -63,27 +63,6 @@ class Message(RootStanza): plugin_attrib = name types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat')) - def setup(self, xml=None): - """ - Populate the stanza object using an optional XML object. - - Overrides StanzaBase.setup. - - Arguments: - xml -- Use an existing XML object for the stanza's values. - """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.getType = self.get_type - self.getMucroom = self.get_mucroom - self.setMucroom = self.set_mucroom - self.delMucroom = self.del_mucroom - self.getMucnick = self.get_mucnick - self.setMucnick = self.set_mucnick - self.delMucnick = self.del_mucnick - - return StanzaBase.setup(self, xml) - def get_type(self): """ Return the message type. @@ -165,3 +144,14 @@ class Message(RootStanza): def del_mucnick(self): """Dummy method to prevent deletion.""" pass + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Message.getType = Message.get_type +Message.getMucroom = Message.get_mucroom +Message.setMucroom = Message.set_mucroom +Message.delMucroom = Message.del_mucroom +Message.getMucnick = Message.get_mucnick +Message.setMucnick = Message.set_mucnick +Message.delMucnick = Message.del_mucnick diff --git a/sleekxmpp/stanza/nick.py b/sleekxmpp/stanza/nick.py index dce41d14..1e23d34f 100644 --- a/sleekxmpp/stanza/nick.py +++ b/sleekxmpp/stanza/nick.py @@ -49,23 +49,6 @@ class Nick(ElementBase): plugin_attrib = name interfaces = set(('nick',)) - def setup(self, xml=None): - """ - Populate the stanza object using an optional XML object. - - Overrides StanzaBase.setup. - - Arguments: - xml -- Use an existing XML object for the stanza's values. - """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setNick = self.set_nick - self.getNick = self.get_nick - self.delNick = self.del_nick - - return ElementBase.setup(self, xml) - def set_nick(self, nick): """ Add a element with the given nickname. @@ -87,3 +70,9 @@ class Nick(ElementBase): register_stanza_plugin(Message, Nick) register_stanza_plugin(Presence, Nick) + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Nick.setNick = Nick.set_nick +Nick.getNick = Nick.get_nick +Nick.delNick = Nick.del_nick diff --git a/sleekxmpp/stanza/presence.py b/sleekxmpp/stanza/presence.py index 60dddf64..c8706233 100644 --- a/sleekxmpp/stanza/presence.py +++ b/sleekxmpp/stanza/presence.py @@ -72,26 +72,6 @@ class Presence(RootStanza): 'subscribed', 'unsubscribe', 'unsubscribed')) showtypes = set(('dnd', 'chat', 'xa', 'away')) - def setup(self, xml=None): - """ - Populate the stanza object using an optional XML object. - - Overrides ElementBase.setup. - - Arguments: - xml -- Use an existing XML object for the stanza's values. - """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setShow = self.set_show - self.getType = self.get_type - self.setType = self.set_type - self.delType = self.get_type - self.getPriority = self.get_priority - self.setPriority = self.set_priority - - return StanzaBase.setup(self, xml) - def exception(self, e): """ Override exception passback for presence. @@ -188,3 +168,13 @@ class Presence(RootStanza): elif self['type'] == 'subscribe': self['type'] = 'subscribed' return StanzaBase.reply(self, clear) + + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Presence.setShow = Presence.set_show +Presence.getType = Presence.get_type +Presence.setType = Presence.set_type +Presence.delType = Presence.get_type +Presence.getPriority = Presence.get_priority +Presence.setPriority = Presence.set_priority diff --git a/sleekxmpp/stanza/roster.py b/sleekxmpp/stanza/roster.py index 8f154a22..afe75516 100644 --- a/sleekxmpp/stanza/roster.py +++ b/sleekxmpp/stanza/roster.py @@ -38,23 +38,6 @@ class Roster(ElementBase): plugin_attrib = 'roster' interfaces = set(('items',)) - def setup(self, xml=None): - """ - Populate the stanza object using an optional XML object. - - Overrides StanzaBase.setup. - - Arguments: - xml -- Use an existing XML object for the stanza's values. - """ - # To comply with PEP8, method names now use underscores. - # Deprecated method names are re-mapped for backwards compatibility. - self.setItems = self.set_items - self.getItems = self.get_items - self.delItems = self.del_items - - return ElementBase.setup(self, xml) - def set_items(self, items): """ Set the roster entries in the stanza. @@ -123,3 +106,9 @@ class Roster(ElementBase): register_stanza_plugin(Iq, Roster) + +# To comply with PEP8, method names now use underscores. +# Deprecated method names are re-mapped for backwards compatibility. +Roster.setItems = Roster.set_items +Roster.getItems = Roster.get_items +Roster.delItems = Roster.del_items -- cgit v1.2.3