diff options
| author | Devendra | 2014-03-24 19:17:48 +0530 | 
|---|---|---|
| committer | Devendra | 2014-03-24 19:17:48 +0530 | 
| commit | 80edcffbfe140a6d19c65deca24e1ba1c0f49b99 (patch) | |
| tree | 4ece6bf2a480866f1028a5765a1fac0cabb68752 | |
| parent | 0874d7b862d8cff1c6d01f3d804a2509e181c959 (diff) | |
| download | pubnub-python-80edcffbfe140a6d19c65deca24e1ba1c0f49b99.tar.bz2 | |
support for python3
| -rw-r--r-- | common/PubnubBase.py | 27 | ||||
| -rw-r--r-- | python/Pubnub.py | 38 | ||||
| -rw-r--r-- | python/examples/here-now-example.py | 4 | ||||
| -rwxr-xr-x | python/examples/history-example.py | 1 | ||||
| -rwxr-xr-x | python/examples/publish-example.py | 3 | ||||
| -rwxr-xr-x | python/examples/subscribe-example.py | 5 | ||||
| -rwxr-xr-x | python/tests/detailed-history-unit-test.py | 24 | ||||
| -rwxr-xr-x | python/tests/unit-test.py | 3 | ||||
| -rw-r--r-- | python/unassembled/Platform.py | 11 | 
9 files changed, 59 insertions, 57 deletions
| diff --git a/common/PubnubBase.py b/common/PubnubBase.py index 5856921..b8817c1 100644 --- a/common/PubnubBase.py +++ b/common/PubnubBase.py @@ -3,7 +3,6 @@ except ImportError: import simplejson as json  import time  import hashlib -import urllib2  import uuid   class PubnubBase(object): @@ -51,7 +50,7 @@ class PubnubBase(object):          self.uuid = UUID or str(uuid.uuid4()) -        if not isinstance(self.uuid, basestring): +        if not isinstance(self.uuid, str):              raise AttributeError("pres_uuid must be a string")      def sign(self, channel, message): @@ -112,7 +111,7 @@ class PubnubBase(object):          channel = str(args['channel'])          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -196,7 +195,7 @@ class PubnubBase(object):          channel = str(args['channel'])          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None @@ -234,7 +233,7 @@ class PubnubBase(object):          """          ## Capture User Input -        limit   = args.has_key('limit') and int(args['limit']) or 10 +        limit   = 'limit' in args and int(args['limit']) or 10          channel = str(args['channel'])          ## Fail if bad input. @@ -243,7 +242,7 @@ class PubnubBase(object):              return False          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None @@ -282,18 +281,18 @@ class PubnubBase(object):          params = dict()           count = 100     -        if args.has_key('count'): +        if 'count' in args:              count = int(args['count'])          params['count'] = str(count)     -        if args.has_key('reverse'): +        if 'reverse' in args:              params['reverse'] = str(args['reverse']).lower() -        if args.has_key('start'): +        if 'start' in args:              params['start'] = str(args['start']) -        if args.has_key('end'): +        if 'end' in args:              params['end'] = str(args['end'])          ## Fail if bad input. @@ -302,7 +301,7 @@ class PubnubBase(object):              return False          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -333,7 +332,7 @@ class PubnubBase(object):          """          ## Capture Callback -        if args and args.has_key('callback') : +        if args and 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -359,6 +358,6 @@ class PubnubBase(object):                  hex(ord(ch)).replace( '0x', '%' ).upper() or                  ch for ch in list(bit)              ]) for bit in request["urlcomponents"]]) -        if (request.has_key("urlparams")): -            url = url + '?' + "&".join([ x + "=" + y  for x,y in request["urlparams"].iteritems()]) +        if ("urlparams" in request): +            url = url + '?' + "&".join([ x + "=" + y  for x,y in request["urlparams"].items()])          return url diff --git a/python/Pubnub.py b/python/Pubnub.py index 59a38af..5dd4c7d 100644 --- a/python/Pubnub.py +++ b/python/Pubnub.py @@ -98,7 +98,6 @@ except ImportError: import simplejson as json  import time  import hashlib -import urllib2  import uuid   class PubnubBase(object): @@ -146,7 +145,7 @@ class PubnubBase(object):          self.uuid = UUID or str(uuid.uuid4()) -        if not isinstance(self.uuid, basestring): +        if not isinstance(self.uuid, str):              raise AttributeError("pres_uuid must be a string")      def sign(self, channel, message): @@ -207,7 +206,7 @@ class PubnubBase(object):          channel = str(args['channel'])          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -291,7 +290,7 @@ class PubnubBase(object):          channel = str(args['channel'])          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None @@ -329,7 +328,7 @@ class PubnubBase(object):          """          ## Capture User Input -        limit   = args.has_key('limit') and int(args['limit']) or 10 +        limit   = 'limit' in args and int(args['limit']) or 10          channel = str(args['channel'])          ## Fail if bad input. @@ -338,7 +337,7 @@ class PubnubBase(object):              return False          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None @@ -377,18 +376,18 @@ class PubnubBase(object):          params = dict()           count = 100     -        if args.has_key('count'): +        if 'count' in args:              count = int(args['count'])          params['count'] = str(count)     -        if args.has_key('reverse'): +        if 'reverse' in args:              params['reverse'] = str(args['reverse']).lower() -        if args.has_key('start'): +        if 'start' in args:              params['start'] = str(args['start']) -        if args.has_key('end'): +        if 'end' in args:              params['end'] = str(args['end'])          ## Fail if bad input. @@ -397,7 +396,7 @@ class PubnubBase(object):              return False          ## Capture Callback -        if args.has_key('callback') : +        if 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -428,7 +427,7 @@ class PubnubBase(object):          """          ## Capture Callback -        if args and args.has_key('callback') : +        if args and 'callback' in args :              callback = args['callback']          else :              callback = None  @@ -454,8 +453,8 @@ class PubnubBase(object):                  hex(ord(ch)).replace( '0x', '%' ).upper() or                  ch for ch in list(bit)              ]) for bit in request["urlcomponents"]]) -        if (request.has_key("urlparams")): -            url = url + '?' + "&".join([ x + "=" + y  for x,y in request["urlparams"].iteritems()]) +        if ("urlparams" in request): +            url = url + '?' + "&".join([ x + "=" + y  for x,y in request["urlparams"].items()])          return url @@ -576,6 +575,7 @@ class PubnubCore(PubnubBase):          return True +import urllib3  class Pubnub(PubnubCore):      def __init__( @@ -596,7 +596,8 @@ class Pubnub(PubnubCore):              ssl_on = ssl_on,              origin = origin,              uuid = pres_uuid -        )         +        ) +        self.http = urllib3.PoolManager(timeout=310)           def _request( self, request, callback = None ) :          ## Build URL @@ -604,11 +605,8 @@ class Pubnub(PubnubCore):          ## Send Request Expecting JSONP Response          try: -            try: usock = urllib2.urlopen( url, None, 310 ) -            except TypeError: usock = urllib2.urlopen( url, None ) -            response = usock.read() -            usock.close() -            resp_json = json.loads(response) +            response = self.http.request('GET', url) +            resp_json = json.loads(response.data.decode("utf-8"))          except:              return None diff --git a/python/examples/here-now-example.py b/python/examples/here-now-example.py index d2ca9bd..b9f0b02 100644 --- a/python/examples/here-now-example.py +++ b/python/examples/here-now-example.py @@ -11,7 +11,7 @@  import sys  sys.path.append('../') -from twisted.internet import reactor +sys.path.append('./')  from Pubnub import Pubnub  publish_key   = len(sys.argv) > 1 and sys.argv[1] or 'demo' @@ -28,7 +28,7 @@ pubnub = Pubnub( publish_key=publish_key, subscribe_key=subscribe_key,  crazy  = 'hello_world'  def print_cb(message): -	print message +	print(message)  pubnub.here_now( {      'channel'  : crazy, diff --git a/python/examples/history-example.py b/python/examples/history-example.py index c7c9547..bf78c7d 100755 --- a/python/examples/history-example.py +++ b/python/examples/history-example.py @@ -1,5 +1,6 @@  import sys  sys.path.append('../') +sys.path.append('./')  from Pubnub import Pubnub  ## Initiat Class diff --git a/python/examples/publish-example.py b/python/examples/publish-example.py index c97034b..55f19ea 100755 --- a/python/examples/publish-example.py +++ b/python/examples/publish-example.py @@ -1,3 +1,6 @@ +import sys +sys.path.append('.') +sys.path.append('..')  from Pubnub import Pubnub  ## Initiate Class diff --git a/python/examples/subscribe-example.py b/python/examples/subscribe-example.py index 14a43d9..a67a08f 100755 --- a/python/examples/subscribe-example.py +++ b/python/examples/subscribe-example.py @@ -1,5 +1,6 @@  import sys -sys.path.append('../') +sys.path.append('..') +sys.path.append('.')  import threading  import time  import random @@ -59,7 +60,7 @@ publish()  print("waiting for subscribes and presence")  pres_thread.join() -print pubnub.here_now({'channel':channel}) +print(pubnub.here_now({'channel':channel}))  sub_thread.join() diff --git a/python/tests/detailed-history-unit-test.py b/python/tests/detailed-history-unit-test.py index 31bdef8..b1500b4 100755 --- a/python/tests/detailed-history-unit-test.py +++ b/python/tests/detailed-history-unit-test.py @@ -14,7 +14,7 @@ sys.path.append('.')  sys.path.append('..')  sys.path.append('../common')  from Pubnub import Pubnub -import unittest2 as unittest +import unittest as unittest  publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' @@ -37,7 +37,7 @@ class TestDetailedHistory(unittest.TestCase):      @classmethod      def publish_msg(cls, start, end, offset): -        print 'Publishing messages' +        print('Publishing messages')          inputs = []          for i in range(start + offset, end + offset):              message = str(i) + " " + crazy @@ -47,19 +47,19 @@ class TestDetailedHistory(unittest.TestCase):                                       })              t = pubnub.time()              inputs.append({'timestamp': t, 'message': message}) -            print 'Message # ', i, ' published' +            print(('Message # ' + i + ' published'))          return inputs      @classmethod -    def setUpClass(cls): -        print 'Setting up context for Detailed History tests. Please wait ...' -        cls.starttime = pubnub.time() -        cls.inputs = cls.inputs + cls.publish_msg(0, cls.total_msg / 2, 0) -        cls.midtime = pubnub.time() -        cls.inputs = cls.inputs + cls.publish_msg( -            0, cls.total_msg / 2, cls.total_msg / 2) -        cls.endtime = pubnub.time() -        print 'Context setup for Detailed History tests. Now running tests' +    def setUpClass(c): +        print('Setting up context for Detailed History tests. Please wait ...') +        c.starttime = pubnub.time() +        c.inputs = c.inputs + c.publish_msg(0, c.total_msg / 2, 0) +        c.midtime = pubnub.time() +        c.inputs = c.inputs + c.publish_msg( +            0, c.total_msg / 2, c.total_msg / 2) +        c.endtime = pubnub.time() +        print('Context setup for Detailed History tests. Now running tests')      def test_begin_to_end_count(self):          count = 5 diff --git a/python/tests/unit-test.py b/python/tests/unit-test.py index 1737ace..762959e 100755 --- a/python/tests/unit-test.py +++ b/python/tests/unit-test.py @@ -53,8 +53,9 @@ history = pubnub.history({      'channel' : crazy,      'limit'   : 1  }) +  test( -    history[0].encode('utf-8') == crazy, +    history[0] == crazy,      'History Message: ' + history[0]  )  test( len(history) == 1, 'History Message Count' ) diff --git a/python/unassembled/Platform.py b/python/unassembled/Platform.py index f598a98..627a350 100644 --- a/python/unassembled/Platform.py +++ b/python/unassembled/Platform.py @@ -1,3 +1,4 @@ +import urllib3  class Pubnub(PubnubCore):      def __init__( @@ -18,7 +19,8 @@ class Pubnub(PubnubCore):              ssl_on = ssl_on,              origin = origin,              uuid = pres_uuid -        )         +        ) +        self.http = urllib3.PoolManager(timeout=310)           def _request( self, request, callback = None ) :          ## Build URL @@ -26,11 +28,8 @@ class Pubnub(PubnubCore):          ## Send Request Expecting JSONP Response          try: -            try: usock = urllib2.urlopen( url, None, 310 ) -            except TypeError: usock = urllib2.urlopen( url, None ) -            response = usock.read() -            usock.close() -            resp_json = json.loads(response) +            response = self.http.request('GET', url) +            resp_json = json.loads(response.data.decode("utf-8"))          except:              return None | 
