Skip to content

Commit

Permalink
Remove Python2 code and tidy up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ukBaz committed Dec 22, 2020
1 parent 49ab367 commit 592b746
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 148 deletions.
19 changes: 2 additions & 17 deletions bluezero/GATT.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
"""Classes that represent the GATT features of a remote device."""

import dbus
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject

import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

from bluezero import constants
from bluezero import dbus_tools
from bluezero import device
from bluezero import tools

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


class Service:
Expand Down
5 changes: 4 additions & 1 deletion bluezero/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# D-Bus imports
import dbus
import dbus.mainloop.glib

# python-bluezero imports
from bluezero import constants
Expand All @@ -13,6 +14,8 @@

logger = tools.create_module_logger(__name__)

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)


class AdapterError(Exception):
pass
Expand Down Expand Up @@ -219,7 +222,7 @@ def nearby_discovery(self, timeout=10):
self._nearby_count = 0

# GLib.timeout_add(1000, self._discovering_timeout)
self.mainloop.add_timer(1000, self._discovering_timeout)
async_tools.add_timer_ms(1000, self._discovering_timeout)
self.adapter_methods.StartDiscovery()
self.mainloop.run()

Expand Down
21 changes: 2 additions & 19 deletions bluezero/advertisement.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,14 @@
import dbus
import dbus.exceptions
import dbus.service
import dbus.mainloop.glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject

import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

from bluezero import constants
from bluezero import dbus_tools
from bluezero import async_tools
from bluezero import adapter
from bluezero import tools

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
mainloop = GObject.MainLoop()

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


########################################
Expand Down
22 changes: 17 additions & 5 deletions bluezero/async_tools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# Main eventloop import
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
import logging

from bluezero import tools

logger = tools.create_module_logger(__name__)


def add_timer_ms(time, callback, data=None):
if data:
GLib.timeout_add(time, callback, data)
else:
GLib.timeout_add(time, callback)


def add_timer_seconds(time, callback, data=None):
if data:
GLib.timeout_add_seconds(time, callback, data)
else:
GLib.timeout_add_seconds(time, callback)


class EventLoop:
# def generic_error_cb(self, error):
# """Generic Error Callback function."""
Expand All @@ -17,6 +32,7 @@ class EventLoop:
# return object.__new__(cls)

def __init__(self):
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.mainloop = GLib.MainLoop()

def run(self):
Expand All @@ -27,7 +43,3 @@ def quit(self):

def is_running(self):
self.mainloop.is_running()

@staticmethod
def add_timer(time, callback):
GLib.timeout_add(time, callback)
13 changes: 2 additions & 11 deletions bluezero/central.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@

from time import sleep

import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

from bluezero import adapter
from bluezero import device
from bluezero import GATT
from bluezero import tools

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


class Central:
Expand Down
15 changes: 2 additions & 13 deletions bluezero/dbus_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,15 @@
# Standard libraries
import re
import subprocess
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

# D-Bus import
import dbus
import dbus.mainloop.glib

# python-bluezero constants import
from bluezero import constants
from bluezero import tools

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


def bluez_version():
Expand Down
7 changes: 0 additions & 7 deletions bluezero/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
"""
import dbus
import dbus.exceptions
import dbus.mainloop.glib
try:
from gi.repository import GLib as GObject
except ImportError:
import gobject as GObject

from bluezero import constants
from bluezero import dbus_tools
Expand Down Expand Up @@ -53,8 +48,6 @@ def __init__(self, adapter_addr, device_addr):
:param device_addr: Address of the remote Bluetooth device.
"""
self.bus = dbus.SystemBus()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.mainloop = GObject.MainLoop()

device_path = dbus_tools.get_dbus_path(adapter_addr, device_addr)
if not device_path:
Expand Down
5 changes: 2 additions & 3 deletions bluezero/localGATT.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
# D-Bus imports
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

# python-bluezero imports
from bluezero import constants
from bluezero import async_tools
from bluezero import tools

# Initialise the mainloop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
DBusGMainLoop(set_as_default=True)

logger = tools.create_module_logger(__name__)

Expand Down
13 changes: 2 additions & 11 deletions bluezero/media_player.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import dbus
import logging

try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

# python-bluezero imports
from bluezero import constants
from bluezero import dbus_tools
from bluezero import tools

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


class MediaPlayerError(Exception):
Expand Down
27 changes: 3 additions & 24 deletions bluezero/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# D-Bus imports
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service

# python-bluezero imports
from bluezero import async_tools
from bluezero import tools
from bluezero import dbus_tools
from bluezero import adapter
Expand All @@ -26,24 +26,7 @@
# array import
import array

import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

# Main eventloop import
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject


logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.addHandler(NullHandler())
logger = tools.create_module_logger(__name__)


########################################
Expand Down Expand Up @@ -130,11 +113,7 @@ def __init__(self, device_id=None):
"""
# Initialise the loop that the application runs in
GObject.threads_init()
dbus.mainloop.glib.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.mainloop = GObject.MainLoop()

self.mainloop = async_tools.EventLoop()
# Initialise the D-Bus path and register it
self.bus = dbus.SystemBus()
self.path = '/ukBaz/bluezero/application{}'.format(id(self))
Expand Down
14 changes: 1 addition & 13 deletions bluezero/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Utility functions for python-bluezero."""
import logging
from sys import version_info
import inspect


Expand Down Expand Up @@ -136,18 +135,7 @@ def url_to_advert(url, frame_type, tx_power):

def get_fn_parameters(fn):
""" return the number of input parameters of the fn , None on error"""
if version_info[0] < 3:
try:
# legacy python 2.x
return len(inspect.getargspec(fn).args)
except Exception as e:
return None
else:
try:
# python 3.x
return len(inspect.getfullargspec(fn).args)
except Exception as e:
return None
return len(inspect.getfullargspec(fn).args)


def create_module_logger(module_name):
Expand Down
13 changes: 4 additions & 9 deletions examples/adapter_example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from bluezero import adapter
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

from bluezero import adapter
from bluezero import tools


def main():
Expand Down Expand Up @@ -33,7 +29,6 @@ def main():

if __name__ == '__main__':
print(__name__)
logger = logging.getLogger('adapter')
logger = tools.create_module_logger('adapter')
logger.setLevel(logging.DEBUG)
logger.addHandler(NullHandler())
main()
Loading

0 comments on commit 592b746

Please sign in to comment.