Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 0c84704

Browse files
authored
Merge pull request #457 from robszewczyk/bug/python3-support
Bug/python3 support
2 parents 5c933cd + cf36dd0 commit 0c84704

File tree

10 files changed

+539
-502
lines changed

10 files changed

+539
-502
lines changed

src/device-manager/python/build-openweave-wheel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# Builds a Python wheel package for OpenWeave.
2121
#
2222

23+
from __future__ import absolute_import
2324
import sys
2425
import os
2526
import stat

src/device-manager/python/openweave/WeaveBleBase.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
# This file is Weave BLE Base class file
2121
#
2222

23+
from __future__ import absolute_import
2324
import abc
2425
import shlex
2526
import optparse
2627
from optparse import OptionParser, Option, OptionValueError
28+
import six
2729

28-
class WeaveBleBase(object):
29-
__metaclass__ = abc.ABCMeta
30+
class WeaveBleBase(six.with_metaclass(abc.ABCMeta, object)):
3031
@abc.abstractmethod
3132
def scan(self, line):
3233
""" API to initiatae BLE scanning for -t user_timeout seconds."""

src/device-manager/python/openweave/WeaveBleUtility.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
# This file is uitilies for Weave BLE
2121
#
2222

23+
from __future__ import absolute_import
24+
from __future__ import print_function
2325
import binascii
2426
import logging
2527
from ctypes import *
28+
import six
2629

2730

2831
# Duplicates of BLE definitions in WeaveDeviceManager-ScriptBinding.cpp
@@ -54,7 +57,7 @@ def _VoidPtrToUUIDString(ptr, len):
5457
ptr = ptr[:8] + '-' + ptr[8:12] + '-' + ptr[12:16] + '-' + ptr[16:20] + '-' + ptr[20:]
5558
ptr = str(ptr)
5659
except:
57-
print "ERROR: failed to convert void * to UUID"
60+
print("ERROR: failed to convert void * to UUID")
5861
ptr = None
5962

6063
return ptr
@@ -74,7 +77,7 @@ def _ByteArrayToVoidPtr(array):
7477
return c_void_p(0)
7578

7679
def ParseBleEventType(val):
77-
if isinstance(val, (int, long)):
80+
if isinstance(val, six.integer_types):
7881
return val
7982
if (val.lower() == "rx"):
8083
return BleEventType_Rx
@@ -92,16 +95,16 @@ def __init__(self, svcId=None, charId=None, status=False):
9295
self.Status = status
9396

9497
def Print(self, prefix=""):
95-
print "%sBleEvent Type: %s" % (prefix, ("TX" if self.EventType == BleEventType_Tx else "ERROR"))
96-
print "%sStatus: %s" % (prefix, str(self.Status))
98+
print("%sBleEvent Type: %s" % (prefix, ("TX" if self.EventType == BleEventType_Tx else "ERROR")))
99+
print("%sStatus: %s" % (prefix, str(self.Status)))
97100

98101
if self.SvcId:
99-
print "%sSvcId:" % (prefix)
100-
print binascii.hexlify(self.SvcId)
102+
print("%sSvcId:" % (prefix))
103+
print(binascii.hexlify(self.SvcId))
101104

102105
if self.CharId:
103-
print "%sCharId:" % (prefix)
104-
print binascii.hexlify(self.CharId)
106+
print("%sCharId:" % (prefix))
107+
print(binascii.hexlify(self.CharId))
105108

106109
def SetField(self, name, val):
107110
name = name.lower()
@@ -123,8 +126,8 @@ def __init__(self, error=0):
123126
self.Error = error
124127

125128
def Print(self, prefix=""):
126-
print "%sBleEvent Type: %s" % (prefix, ("DC" if self.EventType == BleEventType_Disconnect else "ERROR"))
127-
print "%sError: %s" % (prefix, str(self.Error))
129+
print("%sBleEvent Type: %s" % (prefix, ("DC" if self.EventType == BleEventType_Disconnect else "ERROR")))
130+
print("%sError: %s" % (prefix, str(self.Error)))
128131

129132
def SetField(self, name, val):
130133
name = name.lower()
@@ -144,18 +147,18 @@ def __init__(self, svcId=None, charId=None, buffer=None):
144147
self.Buffer = buffer
145148

146149
def Print(self, prefix=""):
147-
print "%sBleEvent Type: %s" % (prefix, ("RX" if self.EventType == BleEventType_Rx else "ERROR"))
150+
print("%sBleEvent Type: %s" % (prefix, ("RX" if self.EventType == BleEventType_Rx else "ERROR")))
148151
if self.Buffer:
149-
print "%sBuffer:" % (prefix)
150-
print binascii.hexlify(self.Buffer)
152+
print("%sBuffer:" % (prefix))
153+
print(binascii.hexlify(self.Buffer))
151154

152155
if self.SvcId:
153-
print "%sSvcId:" % (prefix)
154-
print binascii.hexlify(self.SvcId)
156+
print("%sSvcId:" % (prefix))
157+
print(binascii.hexlify(self.SvcId))
155158

156159
if self.CharId:
157-
print "%sCharId:" % (prefix)
158-
print binascii.hexlify(self.CharId)
160+
print("%sCharId:" % (prefix))
161+
print(binascii.hexlify(self.CharId))
159162

160163
def SetField(self, name, val):
161164
name = name.lower()
@@ -180,17 +183,17 @@ def __init__(self, svcId=None, charId=None, status=True, operation=BleSubscribe
180183
self.Operation = operation
181184

182185
def Print(self, prefix=""):
183-
print "%sBleEvent Type: %s" % (prefix, ("SUBSCRIBE" if self.EventType == BleEventType_Subscribe else "ERROR"))
184-
print "%sStatus: %s" % (prefix, str(self.Status))
185-
print "%sOperation: %s" % (prefix, ("UNSUBSCRIBE" if self.Operation == BleSubscribeOperation_Unsubscribe else "SUBSCRIBE"))
186+
print("%sBleEvent Type: %s" % (prefix, ("SUBSCRIBE" if self.EventType == BleEventType_Subscribe else "ERROR")))
187+
print("%sStatus: %s" % (prefix, str(self.Status)))
188+
print("%sOperation: %s" % (prefix, ("UNSUBSCRIBE" if self.Operation == BleSubscribeOperation_Unsubscribe else "SUBSCRIBE")))
186189

187190
if self.SvcId:
188-
print "%sSvcId:" % (prefix)
189-
print binascii.hexlify(self.SvcId)
191+
print("%sSvcId:" % (prefix))
192+
print(binascii.hexlify(self.SvcId))
190193

191194
if self.CharId:
192-
print "%sCharId:" % (prefix)
193-
print binascii.hexlify(self.CharId)
195+
print("%sCharId:" % (prefix))
196+
print(binascii.hexlify(self.CharId))
194197

195198
def SetField(self, name, val):
196199
name = name.lower()

src/device-manager/python/openweave/WeaveBluezMgr.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# BLE Central support for Weave Device Manager via BlueZ APIs.
2121
#
2222

23+
from __future__ import absolute_import
24+
from __future__ import print_function
2325
import abc
2426
import dbus
2527
import dbus.service
@@ -34,21 +36,23 @@
3436
import time
3537
import traceback
3638
import uuid
37-
import Queue
39+
import six.moves.queue
3840

3941

4042
from ctypes import *
43+
import six
44+
from six.moves import range
4145

4246
try:
4347
from gi.repository import GObject
4448
except:
4549
from pgi.repository import GObject
4650

47-
from WeaveBleUtility import *
48-
from WeaveBleUtility import _VoidPtrToUUIDString
49-
from WeaveBleUtility import _VoidPtrToByteArray
51+
from .WeaveBleUtility import *
52+
from .WeaveBleUtility import _VoidPtrToUUIDString
53+
from .WeaveBleUtility import _VoidPtrToByteArray
5054

51-
from WeaveBleBase import WeaveBleBase
55+
from .WeaveBleBase import WeaveBleBase
5256

5357
weave_service = uuid.UUID('0000FEAF-0000-1000-8000-00805F9B34FB')
5458
weave_tx = uuid.UUID('18EE2EF5-263D-4559-959F-4F9C429F9D11')
@@ -81,7 +85,7 @@ def get_bluez_objects(bluez, bus, interface, prefix_path):
8185
results = []
8286
if bluez is None or bus is None or interface is None or prefix_path is None:
8387
return results
84-
for item in bluez.GetManagedObjects().iteritems():
88+
for item in six.iteritems(bluez.GetManagedObjects()):
8589
delegates = item[1].get(interface)
8690
if not delegates:
8791
continue
@@ -166,7 +170,7 @@ def adapter_bg_scan(self, enable):
166170
self.adapter.StopDiscovery()
167171
self.logger.info("scanning stopped")
168172
else:
169-
print "it has stopped scanning"
173+
print("it has stopped scanning")
170174
if action_flag:
171175
if not self.adapter_event.wait(bleStatusTransitionTimeoutSec):
172176
if enable:
@@ -697,7 +701,7 @@ def __init__(self, devMgr, logger=None):
697701
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
698702
self.scan_quiet= False
699703
self.peripheral_list = []
700-
self.weave_queue = Queue.Queue()
704+
self.weave_queue = six.moves.queue.Queue()
701705
self.Gmainloop = None
702706
self.daemon_thread = None
703707
self.adapter = None
@@ -790,7 +794,7 @@ def running_thread(self, target, kwargs):
790794
while not self.Gmainloop or not self.Gmainloop.is_running():
791795
time.sleep(0.00001)
792796
target(**kwargs)
793-
except Exception, err:
797+
except Exception as err:
794798
traceback.print_exc()
795799
finally:
796800
self.Gmainloop.quit()
@@ -954,7 +958,7 @@ def connect(self, identifier):
954958
else:
955959
return False
956960
else:
957-
print "device cannot be found"
961+
print("device cannot be found")
958962
return False
959963

960964
def disconnect(self):

src/device-manager/python/openweave/WeaveCoreBluetoothMgr.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
# BLE Central support for Weave Device Manager via OSX CoreBluetooth APIs.
2121
#
2222

23+
from __future__ import absolute_import
24+
from __future__ import print_function
2325
import abc
2426
import logging
2527
import select
2628
import socket
2729
import sys
28-
import Queue
30+
import six.moves.queue
2931
import subprocess
3032
import threading
3133
import time
@@ -37,10 +39,10 @@
3739
import objc
3840
from PyObjCTools import AppHelper
3941

40-
from WeaveBleUtility import *
41-
from WeaveBleUtility import _VoidPtrToByteArray
42+
from .WeaveBleUtility import *
43+
from .WeaveBleUtility import _VoidPtrToByteArray
4244

43-
from WeaveBleBase import WeaveBleBase
45+
from .WeaveBleBase import WeaveBleBase
4446

4547

4648
objc.loadBundle("CoreBluetooth", globals(),
@@ -60,7 +62,7 @@ def _VoidPtrToCBUUID(ptr, len):
6062
ptr = ptr[:8] + '-' + ptr[8:12] + '-' + ptr[12:16] + '-' + ptr[16:20] + '-' + ptr[20:]
6163
ptr = CBUUID.UUIDWithString_(ptr)
6264
except:
63-
print "ERROR: failed to convert void * to CBUUID"
65+
print("ERROR: failed to convert void * to CBUUID")
6466
ptr = None
6567

6668
return ptr
@@ -82,7 +84,7 @@ def __init__(self, devMgr, logger=None):
8284
self.characteristics = {}
8385
self.peripheral_list = []
8486
self.bg_peripheral_name = None
85-
self.weave_queue = Queue.Queue()
87+
self.weave_queue = six.moves.queue.Queue()
8688

8789
self.manager = CBCentralManager.alloc()
8890
self.manager.initWithDelegate_queue_options_(self, None, None)

0 commit comments

Comments
 (0)