From f291feaa8a33645850818f811ba8d7f9eb9d4c31 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 13:41:06 +0200 Subject: Refactor. --- parrot_zik/indicator/base.py | 3 +- parrot_zik/indicator/linux.py | 3 +- parrot_zik/indicator/mac.py | 3 +- parrot_zik/indicator/windows.py | 3 +- parrot_zik/parrot_zik_tray | 85 ----------------------------------------- parrot_zik/parrot_zik_tray.py | 80 ++++++++++++++++++++++++++++++++++++++ setup.py | 6 ++- 7 files changed, 93 insertions(+), 90 deletions(-) delete mode 100755 parrot_zik/parrot_zik_tray create mode 100755 parrot_zik/parrot_zik_tray.py diff --git a/parrot_zik/indicator/base.py b/parrot_zik/indicator/base.py index 8204fa9..b29368e 100644 --- a/parrot_zik/indicator/base.py +++ b/parrot_zik/indicator/base.py @@ -7,7 +7,8 @@ class BaseIndicator(object): def setIcon(self, name): raise NotImplementedError - def main(self): + @classmethod + def main(cls): raise NotImplementedError def show_about_dialog(self, widget): diff --git a/parrot_zik/indicator/linux.py b/parrot_zik/indicator/linux.py index ff06587..fc0f425 100644 --- a/parrot_zik/indicator/linux.py +++ b/parrot_zik/indicator/linux.py @@ -31,7 +31,8 @@ class LinuxIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.set_icon(name) - def main(self): + @classmethod + def main(cls): gtk.main() def quit(self, _): diff --git a/parrot_zik/indicator/mac.py b/parrot_zik/indicator/mac.py index d28ee26..232e010 100644 --- a/parrot_zik/indicator/mac.py +++ b/parrot_zik/indicator/mac.py @@ -21,7 +21,8 @@ class DarwinIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.setIcon(name, self.icon_directory) - def main(self): + @classmethod + def main(cls): AppHelper.runEventLoop() def show_about_dialog(self, widget): diff --git a/parrot_zik/indicator/windows.py b/parrot_zik/indicator/windows.py index 118f620..79a4ef3 100644 --- a/parrot_zik/indicator/windows.py +++ b/parrot_zik/indicator/windows.py @@ -30,7 +30,8 @@ class WindowsIndicator(BaseIndicator): def setIcon(self, name): self.statusicon.set_from_file(self.icon_directory + name + '.png') - def main(self): + @classmethod + def main(cls): gtk.main() def quit(self, _): diff --git a/parrot_zik/parrot_zik_tray b/parrot_zik/parrot_zik_tray deleted file mode 100755 index 0e5b951..0000000 --- a/parrot_zik/parrot_zik_tray +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python - -from parrot_zik.interface.version1 import ParrotZikVersion1Interface -from parrot_zik.interface.version2 import ParrotZikVersion2Interface -from parrot_zik import resource_manager -from parrot_zik import bluetooth_paired_devices -from parrot_zik.indicator import MenuItem -from parrot_zik.indicator import Menu -from parrot_zik.indicator import SysIndicator -from parrot_zik.utils import repeat - -REFRESH_FREQUENCY = 30000 -RECONNECT_FREQUENCY = 5000 - - - -class ParrotZikIndicator(SysIndicator): - def __init__(self): - - self.menu = Menu() - - self.info_item = MenuItem("Parrot Zik Not connected", - None, sensitive=False) - self.menu.append(self.info_item) - - self.version_1_interface = ParrotZikVersion1Interface(self) - self.version_2_interface = ParrotZikVersion2Interface(self) - self.quit_item = MenuItem("Quit", self.quit, checkitem=True) - self.menu.append(self.quit_item) - - SysIndicator.__init__(self, icon="zik-audio-headset", menu=self.menu) - - self.active_interface = None - - @repeat - def reconnect(self): - if self.active_interface: - self.reconnect.stop() - else: - self.info("Trying to connect") - try: - manager = bluetooth_paired_devices.connect() - except bluetooth_paired_devices.BluetoothIsNotOn: - self.info("Bluetooth is turned off") - except bluetooth_paired_devices.DeviceNotConnected: - self.info("Parrot Zik Not connected") - except bluetooth_paired_devices.ConnectionFailure: - self.info("Failed to connect") - else: - if manager.api_version.startswith('1'): - interface = self.version_1_interface - else: - interface = self.version_2_interface - try: - interface.activate(manager) - except resource_manager.DeviceDisconnected: - interface.deactivate() - else: - self.autorefresh(self) - self.autorefresh.start(self, REFRESH_FREQUENCY) - self.reconnect.stop() - - def info(self, message): - self.info_item.set_label(message) - print(message) - - @repeat - def autorefresh(self): - if self.active_interface: - self.active_interface.refresh() - else: - self.reconnect.start(self, RECONNECT_FREQUENCY) - self.autorefresh.stop() - - def main(self): - self.reconnect.start(self, RECONNECT_FREQUENCY) - SysIndicator.main(self) - - -if __name__ == "__main__": - try: - indicator = ParrotZikIndicator() - indicator.main() - except KeyboardInterrupt: - pass diff --git a/parrot_zik/parrot_zik_tray.py b/parrot_zik/parrot_zik_tray.py new file mode 100755 index 0000000..35931d7 --- /dev/null +++ b/parrot_zik/parrot_zik_tray.py @@ -0,0 +1,80 @@ +from parrot_zik.interface.version1 import ParrotZikVersion1Interface +from parrot_zik.interface.version2 import ParrotZikVersion2Interface +from parrot_zik import resource_manager +from parrot_zik import bluetooth_paired_devices +from parrot_zik.indicator import MenuItem +from parrot_zik.indicator import Menu +from parrot_zik.indicator import SysIndicator +from parrot_zik.utils import repeat + +REFRESH_FREQUENCY = 30000 +RECONNECT_FREQUENCY = 5000 + + + +class ParrotZikIndicator(SysIndicator): + def __init__(self): + + self.menu = Menu() + + self.info_item = MenuItem("Parrot Zik Not connected", + None, sensitive=False) + self.menu.append(self.info_item) + + self.version_1_interface = ParrotZikVersion1Interface(self) + self.version_2_interface = ParrotZikVersion2Interface(self) + self.quit_item = MenuItem("Quit", self.quit, checkitem=True) + self.menu.append(self.quit_item) + + SysIndicator.__init__(self, icon="zik-audio-headset", menu=self.menu) + + self.active_interface = None + + @repeat + def reconnect(self): + if self.active_interface: + self.reconnect.stop() + else: + self.info("Trying to connect") + try: + manager = bluetooth_paired_devices.connect() + except bluetooth_paired_devices.BluetoothIsNotOn: + self.info("Bluetooth is turned off") + except bluetooth_paired_devices.DeviceNotConnected: + self.info("Parrot Zik Not connected") + except bluetooth_paired_devices.ConnectionFailure: + self.info("Failed to connect") + else: + if manager.api_version.startswith('1'): + interface = self.version_1_interface + else: + interface = self.version_2_interface + try: + interface.activate(manager) + except resource_manager.DeviceDisconnected: + interface.deactivate() + else: + self.autorefresh(self) + self.autorefresh.start(self, REFRESH_FREQUENCY) + self.reconnect.stop() + + def info(self, message): + self.info_item.set_label(message) + print(message) + + @repeat + def autorefresh(self): + if self.active_interface: + self.active_interface.refresh() + else: + self.reconnect.start(self, RECONNECT_FREQUENCY) + self.autorefresh.stop() + + @classmethod + def main(cls): + try: + indicator = cls() + cls.reconnect.start(indicator, RECONNECT_FREQUENCY) + super(ParrotZikIndicator, cls).main() + except KeyboardInterrupt: + pass diff --git a/setup.py b/setup.py index 4600764..89d083a 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,10 @@ setup( ], packages=['parrot_zik', 'parrot_zik.interface', 'parrot_zik.indicator'], - scripts=["parrot_zik/parrot_zik_tray"], + entry_points={ + 'console_scripts': [ + 'parrot_zik_tray=parrot_zik.parrot_zik_tray:ParrotZikIndicator.main', + ] + }, include_package_data=True, ) -- cgit v1.2.1