Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #2586

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/hidapi/hidapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

logger = logging.getLogger(__name__)

ACTION_ADD = "add"
ACTION_REMOVE = "remove"

# Global handle to hidapi
_hidapi = None
Expand Down Expand Up @@ -209,18 +211,18 @@ def run(self):
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)


# The filterfn is used to determine whether this is a device of interest to Solaar.
# It is given the bus id, vendor id, and product id and returns a dictionary
# with the required hid_driver and usb_interface and whether this is a receiver or device.
def _match(action, device, filterfn):
def _match(action: str, device, filterfn):
vid = device["vendor_id"]
pid = device["product_id"]

Expand Down Expand Up @@ -264,7 +266,7 @@ def _match(action, device, filterfn):
return
isDevice = filter.get("isDevice")

if action == "add":
if action == ACTION_ADD:
d_info = DeviceInfo(
path=device["path"].decode(),
bus_id=bus_id,
Expand All @@ -282,7 +284,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,
Expand Down Expand Up @@ -314,11 +316,11 @@ def find_paired_node_wpid(receiver_path, index):
def monitor_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

Expand All @@ -335,7 +337,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

Expand Down
14 changes: 9 additions & 5 deletions lib/hidapi/udev.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@

logger = logging.getLogger(__name__)

ACTION_ADD = "add"
ACTION_REMOVE = "remove"

fileopen = open

#
Expand Down Expand Up @@ -73,12 +76,13 @@ def exit():
# The filterfn is used to determine whether this is a device of interest to Solaar.
# It is given the bus id, vendor id, and product id and returns a dictionary
# with the required hid_driver and usb_interface and whether this is a receiver or device.
def _match(action, device, filterfn):
def _match(action: str, device, filterfn):
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Dbus event {action} {device}")
hid_device = device.find_parent("hid")
if hid_device is None: # only HID devices are of interest to Solaar
return

hid_id = hid_device.properties.get("HID_ID")
if not hid_id:
return # there are reports that sometimes the id isn't set up right so be defensive
Expand Down Expand Up @@ -114,7 +118,7 @@ def _match(action, device, filterfn):
interface_number = filter.get("usb_interface")
isDevice = filter.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")
Expand Down Expand Up @@ -152,7 +156,7 @@ def _match(action, device, filterfn):
)
return d_info

elif action == "remove":
elif action == ACTION_REMOVE:
d_info = DeviceInfo(
path=device.device_node,
bus_id=None,
Expand Down Expand Up @@ -224,11 +228,11 @@ def _process_udev_event(monitor, condition, cb, 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
Expand Down
Loading
Loading