From ffa76dd958282735c1b39793351a075a3d1a64ac Mon Sep 17 00:00:00 2001
From: Marek Siarkowicz <mareksiarkowicz@gmail.com>
Date: Mon, 15 Jun 2015 01:10:40 +0200
Subject: Refactor.

---
 BluetoothPairedDevices.py   | 45 ----------------------
 ParrotZik.py                | 52 -------------------------
 ParrotZikTray               |  6 +--
 bluetooth_paired_devices.py | 92 +++++++++++++++++++++++++++++++++++++++++++++
 resource_manager.py         |  3 +-
 5 files changed, 97 insertions(+), 101 deletions(-)
 delete mode 100644 BluetoothPairedDevices.py
 create mode 100644 bluetooth_paired_devices.py

diff --git a/BluetoothPairedDevices.py b/BluetoothPairedDevices.py
deleted file mode 100644
index aa5b056..0000000
--- a/BluetoothPairedDevices.py
+++ /dev/null
@@ -1,45 +0,0 @@
-import sys
-import re
-import os
-
-if sys.platform == "darwin":
-    from binplist import binplist
-elif sys.platform == "win32":
-    import _winreg
-
-
-def ParrotZikMac():
-        p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|'
-                       'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}')
-        if sys.platform == "linux2":
-            out = os.popen("bluez-test-device list").read()
-            res = p.findall(out)
-            if len(res) > 0:
-                return res[0]
-
-        elif sys.platform == "darwin":
-            fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb")
-            plist = binplist.BinaryPlist(file_obj=fd)
-            parsed_plist = plist.Parse()
-            try:
-                for mac in parsed_plist['PairedDevices']:
-                    if p.match(mac.replace("-", ":")):
-                        return mac.replace("-", ":")
-            except Exception:
-                pass
-
-        elif sys.platform == "win32":
-            aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
-            aKey = _winreg.OpenKey(
-                aReg, 'SYSTEM\CurrentControlSet\Services\
-                BTHPORT\Parameters\Devices')
-            for i in range(10):
-                try:
-                    asubkey_name = _winreg.EnumKey(aKey, i)
-                    mac = ':'.join(asubkey_name[i:i+2] for i in range(0, 12, 2))
-                    res = p.findall(mac)
-                    if len(res) > 0:
-                        return res[0]
-
-                except EnvironmentError:
-                    pass
diff --git a/ParrotZik.py b/ParrotZik.py
index 8e4b31b..52414b5 100644
--- a/ParrotZik.py
+++ b/ParrotZik.py
@@ -1,58 +1,6 @@
-import sys
-
-from resource_manager import GenericResourceManager
 from resource_manager import Version1ResourceManager
 from resource_manager import Version2ResourceManager
 
-if sys.platform == "darwin":
-    import lightblue
-else:
-    import bluetooth
-
-
-def connect(addr=None):
-    uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb",
-             "8B6814D3-6CE7-4498-9700-9312C1711F63"]
-
-    if sys.platform == "darwin":
-        service_matches = lightblue.findservices(
-            name="Parrot RFcomm service", addr=addr)
-    else:
-        for uuid in uuids:
-            service_matches = bluetooth.find_service(uuid=uuid,
-                                                     address=addr)
-            if service_matches:
-                break
-
-    if len(service_matches) == 0:
-        print "Failed to find Parrot Zik RFCOMM service"
-        return GenericResourceManager(None)
-
-    if sys.platform == "darwin":
-        first_match = service_matches[0]
-        port = first_match[1]
-        name = first_match[2]
-        host = first_match[0]
-    else:
-        first_match = service_matches[0]
-        port = first_match["port"]
-        name = first_match["name"]
-        host = first_match["host"]
-
-    print "Connecting to \"%s\" on %s" % (name, host)
-
-    if sys.platform == "darwin":
-        sock = lightblue.socket()
-    else:
-        sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
-
-    sock.connect((host, port))
-
-    sock.send('\x00\x03\x00')
-    data = sock.recv(1024)
-    return GenericResourceManager(sock)
-
-
 
 class BatteryStates:
     CHARGED = 'charged'
diff --git a/ParrotZikTray b/ParrotZikTray
index 6850a3f..e43ac90 100755
--- a/ParrotZikTray
+++ b/ParrotZikTray
@@ -3,12 +3,12 @@ import functools
 
 import gtk
 
-import BluetoothPairedDevices
+import bluetooth_paired_devices
 from ParrotZik import BatteryStates
 from ParrotZik import ParrotZikVersion1
 from ParrotZik import ParrotZikVersion2
 from ParrotZik import NoiseControlTypes
-from ParrotZik import connect
+from bluetooth_paired_devices import connect
 from ParrotZik import Rooms
 from SysIndicator import MenuItem
 from SysIndicator import Menu
@@ -67,7 +67,7 @@ class ParrotZikIndicator(SysIndicator):
             else:
                 self.reconnect.stop()
         else:
-            mac = BluetoothPairedDevices.ParrotZikMac()
+            mac = bluetooth_paired_devices.get_parrot_zik_mac()
             if mac:
                 self.info_item.set_label("Connecting")
                 resource_manager = connect(mac)
diff --git a/bluetooth_paired_devices.py b/bluetooth_paired_devices.py
new file mode 100644
index 0000000..139ca3d
--- /dev/null
+++ b/bluetooth_paired_devices.py
@@ -0,0 +1,92 @@
+import sys
+import re
+import os
+
+from resource_manager import GenericResourceManager
+
+if sys.platform == "darwin":
+    from binplist import binplist
+    import lightblue
+else:
+    import bluetooth
+    if sys.platform == "win32":
+        import _winreg
+
+
+def get_parrot_zik_mac():
+        p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|'
+                       'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}')
+        if sys.platform == "linux2":
+            out = os.popen("bluez-test-device list").read()
+            res = p.findall(out)
+            if len(res) > 0:
+                return res[0]
+
+        elif sys.platform == "darwin":
+            fd = open("/Library/Preferences/com.apple.Bluetooth.plist", "rb")
+            plist = binplist.BinaryPlist(file_obj=fd)
+            parsed_plist = plist.Parse()
+            try:
+                for mac in parsed_plist['PairedDevices']:
+                    if p.match(mac.replace("-", ":")):
+                        return mac.replace("-", ":")
+            except Exception:
+                pass
+
+        elif sys.platform == "win32":
+            aReg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
+            aKey = _winreg.OpenKey(
+                aReg, 'SYSTEM\CurrentControlSet\Services\
+                BTHPORT\Parameters\Devices')
+            for i in range(10):
+                try:
+                    asubkey_name = _winreg.EnumKey(aKey, i)
+                    mac = ':'.join(asubkey_name[i:i+2] for i in range(0, 12, 2))
+                    res = p.findall(mac)
+                    if len(res) > 0:
+                        return res[0]
+
+                except EnvironmentError:
+                    pass
+
+
+def connect(addr=None):
+    if sys.platform == "darwin":
+        service_matches = lightblue.findservices(
+            name="Parrot RFcomm service", addr=addr)
+    else:
+        uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb",
+                 "8B6814D3-6CE7-4498-9700-9312C1711F63"]
+        service_matches = []
+        for uuid in uuids:
+            service_matches = bluetooth.find_service(uuid=uuid, address=addr)
+            if service_matches:
+                break
+
+    if len(service_matches) == 0:
+        print "Failed to find Parrot Zik RFCOMM service"
+        return GenericResourceManager(None)
+
+    if sys.platform == "darwin":
+        first_match = service_matches[0]
+        port = first_match[1]
+        name = first_match[2]
+        host = first_match[0]
+    else:
+        first_match = service_matches[0]
+        port = first_match["port"]
+        name = first_match["name"]
+        host = first_match["host"]
+
+    print "Connecting to \"%s\" on %s" % (name, host)
+
+    if sys.platform == "darwin":
+        sock = lightblue.socket()
+    else:
+        sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
+
+    sock.connect((host, port))
+
+    sock.send('\x00\x03\x00')
+    sock.recv(1024)
+    return GenericResourceManager(sock)
diff --git a/resource_manager.py b/resource_manager.py
index f0703ac..ddd39fb 100644
--- a/resource_manager.py
+++ b/resource_manager.py
@@ -1,9 +1,11 @@
 from operator import itemgetter
 import sys
+
 from BeautifulSoup import BeautifulSoup
 
 from message import Message
 
+
 class ResourceManagerBase(object):
     resources = [
     ]
@@ -137,4 +139,3 @@ class Version2ResourceManager(ResourceManagerBase):
         '/api/audio/noise_control/enabled': ['get', 'set'],
         '/api/audio/track/metadata': ['get'],
     }
-
-- 
cgit v1.2.1