From 253de8518cfb2461f2172fbffd9b2a252924cb00 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 3 Jun 2010 22:42:11 -0400 Subject: Modified xmlstream.py to pass a clean stanza object to each stream handler. The previous version passed the same stanza object to each registered handler, which can cause issues when the stanza object is modified by one handler. The next handler receives the stanza with the modifications, not the original stanza. --- sleekxmpp/xmlstream/xmlstream.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 6b92abca..003ead15 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -21,6 +21,7 @@ import threading import time import traceback import types +import copy import xml.sax.saxutils from . import scheduler @@ -305,19 +306,18 @@ class XMLStream(object): #convert XML into Stanza logging.debug("RECV: %s" % cElementTree.tostring(xmlobj)) xmlobj = self.incoming_filter(xmlobj) - stanza = None + stanza_type = StanzaBase for stanza_class in self.__root_stanza: if xmlobj.tag == "{%s}%s" % (self.default_ns, stanza_class.name): - #if self.__root_stanza[stanza_class].match(xmlobj): - stanza = stanza_class(self, xmlobj) + stanza_type = stanza_class break - if stanza is None: - stanza = StanzaBase(self, xmlobj) unhandled = True + stanza = stanza_type(self, xmlobj) for handler in self.__handlers: if handler.match(stanza): - handler.prerun(stanza) - self.eventqueue.put(('stanza', handler, stanza)) + stanza_copy = stanza_type(self, copy.deepcopy(xmlobj)) + handler.prerun(stanza_copy) + self.eventqueue.put(('stanza', handler, stanza_copy)) if handler.checkDelete(): self.__handlers.pop(self.__handlers.index(handler)) unhandled = False if unhandled: -- cgit v1.2.3 From 690eaf8d3c3856c6242612da22e6c6d323f193ed Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 20 Jul 2010 11:19:49 -0400 Subject: Updated license notices to use the correct MIT format. Also corrected references to nonexistant license.txt to LICENSE. --- sleekxmpp/xmlstream/xmlstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 003ead15..cea204b9 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -3,7 +3,7 @@ Copyright (C) 2010 Nathanael C. Fritz This file is part of SleekXMPP. - See the file license.txt for copying permission. + See the file LICENSE for copying permission. """ from __future__ import with_statement, unicode_literals -- cgit v1.2.3 From c8989c04f3675235e3ae730cb240e2154b5d9e76 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 26 Jul 2010 21:02:25 -0400 Subject: Replaced traceback calls to use logging.exception where applicable. --- sleekxmpp/xmlstream/xmlstream.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index cea204b9..6ce89c25 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -19,7 +19,6 @@ import logging import socket import threading import time -import traceback import types import copy import xml.sax.saxutils @@ -195,14 +194,14 @@ class XMLStream(object): return else: self.state.set('processing', False) - traceback.print_exc() + logging.exception('Socket Error') self.disconnect(reconnect=True) except: if not self.state.reconnect: return else: self.state.set('processing', False) - traceback.print_exc() + logging.exception('Connection error. Reconnecting.') self.disconnect(reconnect=True) if self.state['reconnect']: self.reconnect() @@ -258,8 +257,7 @@ class XMLStream(object): logging.warning("Failed to send %s" % data) self.state.set('connected', False) if self.state.reconnect: - logging.error("Disconnected. Socket Error.") - traceback.print_exc() + logging.exception("Disconnected. Socket Error.") self.disconnect(reconnect=True) def sendRaw(self, data): @@ -344,14 +342,14 @@ class XMLStream(object): try: handler.run(args[0]) except Exception as e: - traceback.print_exc() + logging.exception('Error processing event handler: %s' % handler.name) args[0].exception(e) elif etype == 'schedule': try: logging.debug(args) handler(*args[0]) except: - logging.error(traceback.format_exc()) + logging.exception('Error processing scheduled task') elif etype == 'quit': logging.debug("Quitting eventRunner thread") return False -- cgit v1.2.3 From a49f511a2f844bbab261b5ce84257f026e1eb37c Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 29 Jul 2010 20:16:57 -0400 Subject: Added RESPONSE_TIMEOUT constant to sleekxmpp.xmlstream to serve as a single place to specify a default timeout value when waiting for a stanza response. --- sleekxmpp/xmlstream/xmlstream.py | 1 + 1 file changed, 1 insertion(+) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 6ce89c25..94fed64a 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -24,6 +24,7 @@ import copy import xml.sax.saxutils from . import scheduler +RESPONSE_TIMEOUT = 10 HANDLER_THREADS = 1 ssl_support = True -- cgit v1.2.3 From e077204a16c76df4af90ba067e94c31af3d9e372 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 5 Aug 2010 20:26:41 -0400 Subject: Replaced the ToString class with a tostring function. The sleekxmpp.xmlstream.tostring and sleekxmpp.xmlstream.tostring26 packages have been merged to sleekxmpp.xmlstream.tostring. The __init__.py file will import the appropriate tostring function depending on the Python version. The setup.py file has been updated with the package changes. ElementBase is now a direct descendent of object and does not subclass ToString. Stanza objects now return their XML contents for __repr__. --- sleekxmpp/xmlstream/xmlstream.py | 101 ++++++++++----------------------------- 1 file changed, 24 insertions(+), 77 deletions(-) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 94fed64a..ffaa6514 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -23,6 +23,7 @@ import types import copy import xml.sax.saxutils from . import scheduler +from sleekxmpp.xmlstream.tostring import tostring RESPONSE_TIMEOUT = 10 HANDLER_THREADS = 1 @@ -37,7 +38,7 @@ if sys.version_info < (3, 0): #monkey patch broken filesocket object from . import filesocket #socket._fileobject = filesocket.filesocket - + class RestartStream(Exception): pass @@ -82,7 +83,7 @@ class XMLStream(object): self.namespace_map = {} self.run = True - + def setSocket(self, socket): "Set the socket" self.socket = socket @@ -90,10 +91,10 @@ class XMLStream(object): self.filesocket = socket.makefile('rb', 0) # ElementTree.iterparse requires a file. 0 buffer files have to be binary self.state.set('connected', True) - + def setFileSocket(self, filesocket): self.filesocket = filesocket - + def connect(self, host='', port=0, use_ssl=False, use_tls=True): "Link to connectTCP" return self.connectTCP(host, port, use_ssl, use_tls) @@ -125,7 +126,7 @@ class XMLStream(object): except socket.error as serr: logging.error("Could not connect. Socket Error #%s: %s" % (serr.errno, serr.strerror)) time.sleep(1) - + def connectUnix(self, filepath): "Connect to Unix file and create socket" @@ -146,7 +147,7 @@ class XMLStream(object): logging.warning("Tried to enable TLS, but ssl module not found.") return False raise RestartStream() - + def process(self, threaded=True): self.scheduler.process(threaded=True) for t in range(0, HANDLER_THREADS): @@ -160,10 +161,10 @@ class XMLStream(object): self.__thread['process'].start() else: self._process() - + def schedule(self, name, seconds, callback, args=None, kwargs=None, repeat=False): self.scheduler.add(name, seconds, callback, args, kwargs, repeat, qpointer=self.eventqueue) - + def _process(self): "Start processing the socket." firstrun = True @@ -212,7 +213,7 @@ class XMLStream(object): #self.__thread['readXML'].start() #self.__thread['spawnEvents'] = threading.Thread(name='spawnEvents', target=self.__spawnEvents) #self.__thread['spawnEvents'].start() - + def __readXML(self): "Parses the incoming stream, adding to xmlin queue as it goes" #build cElementTree object from expat was we go @@ -245,7 +246,7 @@ class XMLStream(object): if event == b'start': edepth += 1 logging.debug("Ending readXML loop") - + def _sendThread(self): while self.run: data = self.sendqueue.get(True) @@ -260,11 +261,11 @@ class XMLStream(object): if self.state.reconnect: logging.exception("Disconnected. Socket Error.") self.disconnect(reconnect=True) - + def sendRaw(self, data): self.sendqueue.put(data) return True - + def disconnect(self, reconnect=False): self.state.set('reconnect', reconnect) if self.state['disconnecting']: @@ -290,20 +291,20 @@ class XMLStream(object): if self.state['processing']: #raise CloseStream pass - + def reconnect(self): self.state.set('tls',False) self.state.set('ssl',False) time.sleep(1) self.connect() - + def incoming_filter(self, xmlobj): return xmlobj - + def __spawnEvent(self, xmlobj): "watching xmlOut and processes handlers" #convert XML into Stanza - logging.debug("RECV: %s" % cElementTree.tostring(xmlobj)) + logging.debug("RECV: %s" % tostring(xmlobj)) xmlobj = self.incoming_filter(xmlobj) stanza_type = StanzaBase for stanza_class in self.__root_stanza: @@ -323,7 +324,7 @@ class XMLStream(object): stanza.unhandled() #loop through handlers and test match #spawn threads as necessary, call handlers, sending Stanza - + def _eventRunner(self): logging.debug("Loading event runner") while self.run: @@ -354,11 +355,11 @@ class XMLStream(object): elif etype == 'quit': logging.debug("Quitting eventRunner thread") return False - + def registerHandler(self, handler, before=None, after=None): "Add handler with matcher class and parameters." self.__handlers.append(handler) - + def removeHandler(self, name): "Removes the handler." idx = 0 @@ -367,81 +368,27 @@ class XMLStream(object): self.__handlers.pop(idx) return idx += 1 - + def registerStanza(self, stanza_class): "Adds stanza. If root stanzas build stanzas sent in events while non-root stanzas build substanza objects." self.__root_stanza.append(stanza_class) - + def registerStanzaExtension(self, stanza_class, stanza_extension): if stanza_class not in stanza_extensions: stanza_extensions[stanza_class] = [stanza_extension] else: stanza_extensions[stanza_class].append(stanza_extension) - + def removeStanza(self, stanza_class, root=False): "Removes the stanza's registration." if root: del self.__root_stanza[stanza_class] else: del self.__stanza[stanza_class] - + def removeStanzaExtension(self, stanza_class, stanza_extension): stanza_extension[stanza_class].pop(stanza_extension) - def tostring(self, xml, xmlns='', stringbuffer=''): - newoutput = [stringbuffer] - #TODO respect ET mapped namespaces - itag = xml.tag.split('}', 1)[-1] - if '}' in xml.tag: - ixmlns = xml.tag.split('}', 1)[0][1:] - else: - ixmlns = '' - nsbuffer = '' - if xmlns != ixmlns and ixmlns != '': - if ixmlns in self.namespace_map: - if self.namespace_map[ixmlns] != '': - itag = "%s:%s" % (self.namespace_map[ixmlns], itag) - else: - nsbuffer = """ xmlns="%s\"""" % ixmlns - newoutput.append("<%s" % itag) - newoutput.append(nsbuffer) - for attrib in xml.attrib: - newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) - if len(xml) or xml.text or xml.tail: - newoutput.append(">") - if xml.text: - newoutput.append(self.xmlesc(xml.text)) - if len(xml): - for child in xml.getchildren(): - newoutput.append(self.tostring(child, ixmlns)) - newoutput.append("" % (itag, )) - if xml.tail: - newoutput.append(self.xmlesc(xml.tail)) - elif xml.text: - newoutput.append(">%s" % (self.xmlesc(xml.text), itag)) - else: - newoutput.append(" />") - return ''.join(newoutput) - - def xmlesc(self, text): - text = list(text) - cc = 0 - matches = ('&', '<', '"', '>', "'") - for c in text: - if c in matches: - if c == '&': - text[cc] = '&' - elif c == '<': - text[cc] = '<' - elif c == '>': - text[cc] = '>' - elif c == "'": - text[cc] = ''' - elif self.escape_quotes: - text[cc] = '"' - cc += 1 - return ''.join(text) - def start_stream_handler(self, xml): """Meant to be overridden""" pass -- cgit v1.2.3 From 4d1f071f831183568147870302722e492d71d051 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 5 Aug 2010 23:11:22 -0400 Subject: Updated the use of tostring in xmlstream.py Now uses the xmlns and stream parameters to reduce the number of extra xmlns attributes used in the logging output. Added self.default_ns to XMLStream just to be safe. --- sleekxmpp/xmlstream/xmlstream.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sleekxmpp/xmlstream/xmlstream.py') diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index ffaa6514..bf39bb33 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -73,6 +73,7 @@ class XMLStream(object): self.use_ssl = False self.use_tls = False + self.default_ns = '' self.stream_header = "" self.stream_footer = "" @@ -304,7 +305,7 @@ class XMLStream(object): def __spawnEvent(self, xmlobj): "watching xmlOut and processes handlers" #convert XML into Stanza - logging.debug("RECV: %s" % tostring(xmlobj)) + logging.debug("RECV: %s" % tostring(xmlobj, xmlns=self.default_ns, stream=self)) xmlobj = self.incoming_filter(xmlobj) stanza_type = StanzaBase for stanza_class in self.__root_stanza: -- cgit v1.2.3