Replace action strings with constants
Avoids spelling mistakes and helps readability.
This commit is contained in:
parent
cba3533869
commit
54aace050c
|
@ -44,6 +44,8 @@ if typing.TYPE_CHECKING:
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ACTION_ADD = "add"
|
||||
ACTION_REMOVE = "remove"
|
||||
|
||||
# Global handle to hidapi
|
||||
_hidapi = None
|
||||
|
@ -213,10 +215,10 @@ class _DeviceMonitor(Thread):
|
|||
current_devices = {tuple(dev.items()): dev for dev in _enumerate_devices()}
|
||||
for key, device in self.prev_devices.items():
|
||||
if key not in current_devices:
|
||||
self.device_callback("remove", device)
|
||||
self.device_callback(ACTION_REMOVE, device)
|
||||
for key, device in current_devices.items():
|
||||
if key not in self.prev_devices:
|
||||
self.device_callback("add", device)
|
||||
self.device_callback(ACTION_ADD, device)
|
||||
self.prev_devices = current_devices
|
||||
sleep(self.polling_delay)
|
||||
|
||||
|
@ -268,7 +270,7 @@ def _match(action, device, filterfn):
|
|||
return
|
||||
isDevice = filter_func.get("isDevice")
|
||||
|
||||
if action == "add":
|
||||
if action == ACTION_ADD:
|
||||
d_info = DeviceInfo(
|
||||
path=device["path"].decode(),
|
||||
bus_id=bus_id,
|
||||
|
@ -286,7 +288,7 @@ def _match(action, device, filterfn):
|
|||
)
|
||||
return d_info
|
||||
|
||||
elif action == "remove":
|
||||
elif action == ACTION_REMOVE:
|
||||
d_info = DeviceInfo(
|
||||
path=device["path"].decode(),
|
||||
bus_id=None,
|
||||
|
@ -326,11 +328,11 @@ def monitor_glib(glib: GLib, callback, filterfn):
|
|||
|
||||
def device_callback(action, device):
|
||||
# print(f"device_callback({action}): {device}")
|
||||
if action == "add":
|
||||
if action == ACTION_ADD:
|
||||
d_info = _match(action, device, filterfn)
|
||||
if d_info:
|
||||
glib.idle_add(callback, action, d_info)
|
||||
elif action == "remove":
|
||||
elif action == ACTION_REMOVE:
|
||||
# Removed devices will be detected by Solaar directly
|
||||
pass
|
||||
|
||||
|
@ -347,7 +349,7 @@ def enumerate(filterfn):
|
|||
:returns: a list of matching ``DeviceInfo`` tuples.
|
||||
"""
|
||||
for device in _enumerate_devices():
|
||||
d_info = _match("add", device, filterfn)
|
||||
d_info = _match(ACTION_ADD, device, filterfn)
|
||||
if d_info:
|
||||
yield d_info
|
||||
|
||||
|
|
|
@ -51,6 +51,9 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
fileopen = open
|
||||
|
||||
ACTION_ADD = "add"
|
||||
ACTION_REMOVE = "remove"
|
||||
|
||||
#
|
||||
# exposed API
|
||||
# docstrings mostly copied from hidapi.h
|
||||
|
@ -108,7 +111,8 @@ def _match(action, device, filter_func: typing.Callable[[int, int, int, bool, bo
|
|||
if not hidpp_short and not hidpp_long:
|
||||
return
|
||||
except Exception as e: # if can't process report descriptor fall back to old scheme
|
||||
hidpp_short = hidpp_long = None
|
||||
hidpp_short = None
|
||||
hidpp_long = None
|
||||
logger.info(
|
||||
"Report Descriptor not processed for DEVICE %s BID %s VID %s PID %s: %s", device.device_node, bid, vid, pid, e
|
||||
)
|
||||
|
@ -119,7 +123,7 @@ def _match(action, device, filter_func: typing.Callable[[int, int, int, bool, bo
|
|||
interface_number = filtered_result.get("usb_interface")
|
||||
isDevice = filtered_result.get("isDevice")
|
||||
|
||||
if action == "add":
|
||||
if action == ACTION_ADD:
|
||||
hid_driver_name = hid_device.properties.get("DRIVER")
|
||||
intf_device = device.find_parent("usb", "usb_interface")
|
||||
usb_interface = None if intf_device is None else intf_device.attributes.asint("bInterfaceNumber")
|
||||
|
@ -157,7 +161,7 @@ def _match(action, device, filter_func: typing.Callable[[int, int, int, bool, bo
|
|||
)
|
||||
return d_info
|
||||
|
||||
elif action == "remove":
|
||||
elif action == ACTION_REMOVE:
|
||||
d_info = DeviceInfo(
|
||||
path=device.device_node,
|
||||
bus_id=None,
|
||||
|
@ -236,11 +240,11 @@ def monitor_glib(glib: GLib, callback, filterfn):
|
|||
if event:
|
||||
action, device = event
|
||||
# print ("***", action, device)
|
||||
if action == "add":
|
||||
if action == ACTION_ADD:
|
||||
d_info = _match(action, device, filterfn)
|
||||
if d_info:
|
||||
glib.idle_add(cb, action, d_info)
|
||||
elif action == "remove":
|
||||
elif action == ACTION_REMOVE:
|
||||
# the GLib notification does _not_ match!
|
||||
pass
|
||||
return True
|
||||
|
@ -272,7 +276,7 @@ def enumerate(filter_func: typing.Callable[[int, int, int, bool, bool], dict[str
|
|||
if logger.isEnabledFor(logging.DEBUG):
|
||||
logger.debug("Starting dbus enumeration")
|
||||
for dev in pyudev.Context().list_devices(subsystem="hidraw"):
|
||||
dev_info = _match("add", dev, filter_func)
|
||||
dev_info = _match(ACTION_ADD, dev, filter_func)
|
||||
if dev_info:
|
||||
yield dev_info
|
||||
|
||||
|
|
|
@ -41,6 +41,8 @@ from gi.repository import GLib # NOQA: E402 # isort:skip
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
ACTION_ADD = "add"
|
||||
|
||||
_GHOST_DEVICE = namedtuple("_GHOST_DEVICE", ("receiver", "number", "name", "kind", "online"))
|
||||
_GHOST_DEVICE.__bool__ = lambda self: False
|
||||
_GHOST_DEVICE.__nonzero__ = _GHOST_DEVICE.__bool__
|
||||
|
@ -278,7 +280,7 @@ def start_all():
|
|||
if logger.isEnabledFor(logging.INFO):
|
||||
logger.info("starting receiver listening threads")
|
||||
for device_info in base.receivers_and_devices():
|
||||
_process_receiver_event("add", device_info)
|
||||
_process_receiver_event(ACTION_ADD, device_info)
|
||||
|
||||
|
||||
def stop_all():
|
||||
|
@ -368,6 +370,6 @@ def _process_receiver_event(action, device_info):
|
|||
if listener_thread is not None:
|
||||
assert isinstance(listener_thread, SolaarListener)
|
||||
listener_thread.stop()
|
||||
if action == "add":
|
||||
if action == ACTION_ADD:
|
||||
_process_add(device_info, 3)
|
||||
return False
|
||||
|
|
Loading…
Reference in New Issue