From fcdd57ce543b4797114868c1af8209e8cc92d0dd Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 1 Oct 2010 11:15:51 -0400 Subject: Moved add_handler, send, and sendXML to XMLStream. --- sleekxmpp/basexmpp.py | 24 -------------- sleekxmpp/xmlstream/xmlstream.py | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 24 deletions(-) (limited to 'sleekxmpp') diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index a522f822..51607d78 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -114,30 +114,6 @@ class basexmpp(object): for plugin in self.plugin: self.plugin[plugin].post_init() - def add_handler(self, mask, pointer, name=None, disposable=False, threaded=False, filter=False, instream=False): - # threaded is no longer needed, but leaving it for backwards compatibility for now - if name is None: - name = 'add_handler_%s' % self.getNewId() - self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer, once=disposable, instream=instream)) - - def sendXML(self, data, mask=None, timeout=10): - return self.send(tostring(data), mask, timeout) - - def send(self, data, mask=None, timeout=10): - #logging.warning("Deprecated send used for \"%s\"" % (data,)) - #if not type(data) == type(''): - # data = self.tostring(data) - if hasattr(mask, 'xml'): - mask = mask.xml - data = str(data) - if mask is not None: - logging.warning("Use of send mask waiters is deprecated") - waitfor = Waiter('SendWait_%s' % self.getNewId(), MatchXMLMask(mask)) - self.registerHandler(waitfor) - self.sendRaw(data) - if mask is not None: - return waitfor.wait(timeout) - def makeIq(self, id=0, ifrom=None): return self.Iq().setStanzaValues({'id': str(id), 'from': ifrom}) diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 5531b1d2..fc6a374c 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -148,6 +148,7 @@ class XMLStream(object): self.sendRaw = self.send_raw self.getId = self.get_id self.getNewId = self.new_id + self.sendXML = self.send_xml self.ssl_support = SSL_SUPPORT @@ -376,6 +377,30 @@ class XMLStream(object): """ del self.__root_stanza[stanza_class] + def add_handler(self, mask, pointer, name=None, disposable=False, + threaded=False, filter=False, instream=False): + """ + A shortcut method for registering a handler using XML masks. + + Arguments: + mask -- An XML snippet matching the structure of the + stanzas that will be passed to this handler. + pointer -- The handler function itself. + name -- A unique name for the handler. A name will + be generated if one is not provided. + disposable -- Indicates if the handler should be discarded + after one use. + threaded -- Deprecated. Remains for backwards compatibility. + filter -- Deprecated. Remains for backwards compatibility. + instream -- Indicates if the handler should execute during + stream processing and not during normal event + processing. + """ + if name is None: + name = 'add_handler_%s' % self.getNewId() + self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer, + once=disposable, instream=instream)) + def register_handler(self, handler, before=None, after=None): """ Add a stream event handler that will be executed when a matching @@ -432,6 +457,33 @@ class XMLStream(object): """ return xml + def send(self, data, mask, timeout=RESPONSE_TIMEOUT): + """ + A wrapper for send_raw for sending stanza objects. + + May optionally block until an expected response is received. + + Arguments: + data -- The stanza object to send on the stream. + mask -- Deprecated. An XML snippet matching the structure + of the expected response. Execution will block + in this thread until the response is received + or a timeout occurs. + timeout -- Time in seconds to wait for a response before + continuing. Defaults to RESPONSE_TIMEOUT. + """ + if hasattr(mask, 'xml'): + mask = mask.xml + data = str(data) + if mask is not None: + logging.warning("Use of send mask waiters is deprecated.") + wait_for = Waiter("SendWait_%s" % self.new_id(), + MatchXMLMask(mask)) + self.register_handler(wait_for) + self.send_raw(data) + if mask is not None: + return wait_for.wait(timeout) + def send_raw(self, data): """ Send raw data across the stream. @@ -442,6 +494,22 @@ class XMLStream(object): self.send_queue.put(data) return True + def send_xml(self, data, mask=None, timeout=RESPONSE_TIMEOUT): + """ + Send an XML object on the stream, and optionally wait + for a response. + + Arguments: + data -- The XML object to send on the stream. + mask -- Deprecated. An XML snippet matching the structure + of the expected response. Execution will block + in this thread until the response is received + or a timeout occurs. + timeout -- Time in seconds to wait for a response before + continuing. Defaults to RESPONSE_TIMEOUT. + """ + return self.send(tostring(data), mask, timeout) + def process(self, threaded=True): """ Initialize the XML streams and begin processing events. -- cgit v1.2.3