diff options
-rw-r--r-- | nbxmpp/auth_nb.py | 14 | ||||
-rw-r--r-- | nbxmpp/dispatcher_nb.py | 5 | ||||
-rw-r--r-- | nbxmpp/idlequeue.py | 20 | ||||
-rw-r--r-- | nbxmpp/simplexml.py | 48 | ||||
-rw-r--r-- | nbxmpp/tls_nb.py | 2 | ||||
-rw-r--r-- | nbxmpp/transports_nb.py | 8 |
6 files changed, 71 insertions, 26 deletions
diff --git a/nbxmpp/auth_nb.py b/nbxmpp/auth_nb.py index 55b4c76..2c3aa3a 100644 --- a/nbxmpp/auth_nb.py +++ b/nbxmpp/auth_nb.py @@ -21,10 +21,13 @@ Can be used both for client and transport authentication See client_nb.py """ +from __future__ import unicode_literals + from .protocol import NS_SASL, NS_SESSION, NS_STREAMS, NS_BIND, NS_AUTH from .protocol import NS_STREAM_MGMT from .protocol import Node, NodeProcessed, isResultNode, Iq, Protocol, JID from .plugin import PlugIn +import sys import re import base64 from . import dispatcher_nb @@ -373,9 +376,14 @@ class SASL(PlugIn): def HMAC(k, s): return hmac.new(key=k, msg=s, digestmod=hashfn).digest() - def XOR(x, y): - r = [px ^ py for px, py in zip(x, y)] - return bytes(r) + if sys.version_info[0] == 2: + def XOR(x, y): + r = (chr(ord(px) ^ ord(py)) for px, py in zip(x, y)) + return bytes(b''.join(r)) + else: + def XOR(x, y): + r = [px ^ py for px, py in zip(x, y)] + return bytes(r) def Hi(s, salt, iters): ii = 1 diff --git a/nbxmpp/dispatcher_nb.py b/nbxmpp/dispatcher_nb.py index e572a73..b916e31 100644 --- a/nbxmpp/dispatcher_nb.py +++ b/nbxmpp/dispatcher_nb.py @@ -20,6 +20,7 @@ Main xmpp decision making logic. Provides library with methods to assign different handlers to different XMPP stanzas and namespaces """ +from __future__ import unicode_literals from . import simplexml import sys import locale @@ -29,6 +30,10 @@ from .plugin import PlugIn from .protocol import (NS_STREAMS, NS_XMPP_STREAMS, NS_HTTP_BIND, Iq, Presence, Message, Protocol, Node, Error, ERR_FEATURE_NOT_IMPLEMENTED, StreamError) import logging + +if sys.version_info[0] == 2: + chr = unichr + log = logging.getLogger('nbxmpp.dispatcher_nb') #: default timeout to wait for response for our id diff --git a/nbxmpp/idlequeue.py b/nbxmpp/idlequeue.py index 08005a8..c6b2a0d 100644 --- a/nbxmpp/idlequeue.py +++ b/nbxmpp/idlequeue.py @@ -18,13 +18,18 @@ Idlequeues are Gajim's network heartbeat. Transports can be plugged as idle objects and be informed about possible IO """ +from __future__ import unicode_literals + import os +import sys import select import logging log = logging.getLogger('nbxmpp.idlequeue') # needed for get_idleqeue try: + if sys.version_info[0] == 2: + raise ImportError from gi.repository import GLib HAVE_GLIB = True except ImportError: @@ -36,11 +41,10 @@ if os.name == 'nt': elif os.name == 'posix': import fcntl -FLAG_WRITE = GLib.IOCondition.OUT | GLib.IOCondition.HUP -FLAG_READ = GLib.IOCondition.IN | GLib.IOCondition.PRI | GLib.IOCondition.HUP -FLAG_READ_WRITE = GLib.IOCondition.OUT | GLib.IOCondition.IN | \ - GLib.IOCondition.PRI | GLib.IOCondition.HUP -FLAG_CLOSE = GLib.IOCondition.HUP +FLAG_WRITE = 20 # write only +FLAG_READ = 19 # read only +FLAG_READ_WRITE = 23 # read and write +FLAG_CLOSE = 16 # wait for close PENDING_READ = 3 # waiting read event PENDING_WRITE = 4 # waiting write event @@ -309,11 +313,11 @@ class IdleQueue: """ current_time = self.current_time() - for fd, timeouts in list(self.read_timeouts.items()): + for fd, timeouts in self.read_timeouts.items(): if fd not in self.queue: self.remove_timeout(fd) continue - for timeout, func in list(timeouts.items()): + for timeout, func in timeouts.items(): if timeout > current_time: continue if func: @@ -324,7 +328,7 @@ class IdleQueue: self.queue[fd].read_timeout() self.remove_timeout(fd, timeout) - times = list(self.alarms.keys()) + times = self.alarms.keys() for alarm_time in times: if alarm_time > current_time: continue diff --git a/nbxmpp/simplexml.py b/nbxmpp/simplexml.py index 69f078e..24f1b46 100644 --- a/nbxmpp/simplexml.py +++ b/nbxmpp/simplexml.py @@ -20,6 +20,9 @@ nodes and XML streams. I'm personally using it in many other separate projects. It is designed to be as standalone as possible """ +from __future__ import unicode_literals + +import sys import xml.parsers.expat import logging log = logging.getLogger('nbxmpp.simplexml') @@ -34,20 +37,37 @@ def XMLescape(txt): ENCODING='utf-8' -def ustr(what): - """ - Converts object "what" to unicode string using it's own __str__ method if - accessible or unicode method otherwise - """ - if isinstance(what, str): - return what - try: - r = what.__str__() - except AttributeError: - r = str(what) - if not isinstance(r, str): - return str(r, ENCODING) - return r +if sys.version_info[0] == 2: + def ustr(what): + """ + Converts object "what" to unicode string using it's own __str__ method if + accessible or unicode method otherwise + """ + if isinstance(what, unicode): + return what + try: + r = what.__str__() + except AttributeError: + r = unicode(what) + if not isinstance(r, unicode): + return unicode(r, ENCODING) + return r + +else: + def ustr(what): + """ + Converts object "what" to unicode string using it's own __str__ method if + accessible or unicode method otherwise + """ + if isinstance(what, str): + return what + try: + r = what.__str__() + except AttributeError: + r = str(what) + if not isinstance(r, str): + return str(r, ENCODING) + return r class Node(object): """ diff --git a/nbxmpp/tls_nb.py b/nbxmpp/tls_nb.py index b979d08..365db4e 100644 --- a/nbxmpp/tls_nb.py +++ b/nbxmpp/tls_nb.py @@ -15,6 +15,8 @@ ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. +from __future__ import print_function + import socket import ssl from .plugin import PlugIn diff --git a/nbxmpp/transports_nb.py b/nbxmpp/transports_nb.py index 0a7b5eb..e9e8889 100644 --- a/nbxmpp/transports_nb.py +++ b/nbxmpp/transports_nb.py @@ -24,6 +24,8 @@ Transports are not aware of XMPP stanzas and only responsible for low-level connection handling. """ +from __future__ import unicode_literals + from .simplexml import ustr from .plugin import PlugIn from .idlequeue import IdleObject @@ -35,7 +37,11 @@ import errno import time import traceback import base64 -from urllib.parse import urlparse + +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse import logging log = logging.getLogger('nbxmpp.transports_nb') |