Skip to content

Commit 5964b98

Browse files
committed
Linter fixes and add types for part of eudev monitor
1 parent 169378a commit 5964b98

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

scc/device_monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def get_vendor_product(self, syspath: str, subsystem: str | None = None) -> tupl
214214

215215
# Sleep for 1 second to make sure info is available on system
216216
time.sleep(1)
217+
logging.debug('TODO: Hardcoded 1s sleep!') # https://github.com/C0rn3j/sc-controller/commit/43d148327a1b92042d67dd6cfdf1128aa6f9b25d
217218
node = self._dev_for_hci(syspath)
218219
if node:
219220
name = node.split("/")[-1]
@@ -256,8 +257,7 @@ def _find_bt_address(syspath: str) -> str | None:
256257

257258
@staticmethod
258259
def get_usb_address(syspath: str) -> tuple[int, int]:
259-
"""
260-
For given syspath, reads and returns (busnum, devnum) as ints.
260+
"""For given syspath, read and return (busnum, devnum) as ints.
261261
262262
May throw all kinds of OSErrors
263263
"""

scc/lib/eudevmonitor.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import errno
2424
import os
2525
from ctypes.util import find_library
26-
from typing import NamedTuple, Type, TypeVar
26+
from typing import NamedTuple, TypeVar
2727

2828

2929
class Eudev:
@@ -43,7 +43,7 @@ def __init__(self):
4343
raise OSError("Failed to initialize udev context")
4444

4545
@staticmethod
46-
def _setup_lib(lib):
46+
def _setup_lib(lib: ctypes.CDLL):
4747
"""Just so it's away from init and can be folded in IDE."""
4848
# udev
4949
lib.udev_new.restype = ctypes.c_void_p
@@ -115,21 +115,22 @@ def __del__(self):
115115
self._lib.udev_unref(self._ctx)
116116
self._ctx = None
117117

118-
119-
def enumerate(self, subclass=None):
118+
enumerate_typevar = TypeVar("enumerate_typevar", bound="Enumerator")
119+
def enumerate(self, subclass: type [enumerate_typevar] | None = None) -> enumerate_typevar | Enumerator:
120120
"""Return new Enumerator instance."""
121-
enumerator = self._lib.udev_enumerate_new(self._ctx)
121+
enumerator: ctypes.c_void_p | None = self._lib.udev_enumerate_new(self._ctx)
122122
if enumerator is None:
123123
raise OSError("Failed to initialize enumerator")
124124
if subclass is not None:
125-
assert issubclass(subclass, Enumerator)
126-
subclass = subclass or Enumerator
127-
return subclass(self, enumerator)
125+
assert issubclass(subclass, Enumerator), f"subclass must be a subclass of Enumerator but {subclass} was provided"
126+
# print('enumerate - ', type(enumerator), enumerator)
127+
final_class = subclass or Enumerator
128+
return final_class(self, enumerator)
128129

129-
T = TypeVar("T", bound="Monitor")
130-
def monitor(self, subclass: type [T] | None = None) -> T | Monitor:
130+
monitor_typevar = TypeVar("monitor_typevar", bound="Monitor")
131+
def monitor(self, subclass: type [monitor_typevar] | None = None) -> monitor_typevar | Monitor:
131132
"""Return new Monitor instance."""
132-
monitor = self._lib.udev_monitor_new_from_netlink(self._ctx, b"udev")
133+
monitor: ctypes.c_void_p | None = self._lib.udev_monitor_new_from_netlink(self._ctx, b"udev")
133134
if monitor is None:
134135
raise OSError("Failed to initialize monitor")
135136
if subclass is not None:
@@ -151,7 +152,7 @@ class Enumerator:
151152
All match_* methods are returning self for chaining.
152153
"""
153154

154-
def __init__(self, eudev: Eudev, enumerator: Enumerator):
155+
def __init__(self, eudev: Eudev, enumerator: ctypes.c_void_p) -> None:
155156
self._eudev = eudev
156157
self._enumerator = enumerator
157158
self._keep_in_mem = []
@@ -165,7 +166,7 @@ def __del__(self):
165166
self._enumerator = None
166167

167168

168-
def _add_match(self, whichone, *pars):
169+
def _add_match(self, whichone: str, *pars) -> Enumerator:
169170
if self._enumeration_started:
170171
raise RuntimeError("Cannot add match after enumeration is started")
171172
fn = getattr(self._eudev._lib, "udev_enumerate_add_" + whichone)
@@ -183,9 +184,9 @@ def match_sysattr(self, sysattr, value): return self._add_match("match_sysattr",
183184
def nomatch_sysattr(self, sysattr, value): return self._add_match("nomatch_sysattr", sysattr, value)
184185
@twoargs
185186
def match_property(self, property, value): return self._add_match("match_property", property, value)
186-
def match_subsystem(self, subsystem): return self._add_match("match_subsystem", subsystem)
187-
def nomatch_subsystem(self, subsystem): return self._add_match("nomatch_subsystem", subsystem)
188-
def match_sysname(self, sysname): return self._add_match("match_sysname", sysname)
187+
def match_subsystem(self, subsystem: str): return self._add_match("match_subsystem", subsystem)
188+
def nomatch_subsystem(self, subsystem: str): return self._add_match("nomatch_subsystem", subsystem)
189+
def match_sysname(self, sysname: str): return self._add_match("match_sysname", sysname)
189190
def match_tag(self, tag): return self._add_match("match_tag", tag)
190191
def match_is_initialized(self): return self._add_match("match_is_initialized")
191192
# match_parent is not implemented
@@ -237,15 +238,15 @@ class Monitor:
237238
All match_* methods are returning self for chaining
238239
"""
239240

240-
def __init__(self, eudev: Eudev, monitor):
241+
def __init__(self, eudev: Eudev, monitor: ctypes.c_void_p) -> None:
241242
self._eudev = eudev
242243
self._monitor = monitor
243244
self._monitor_started = False
244245
self._keep_in_mem = []
245246
self._enabled_matches = set()
246247

247248

248-
def __del__(self):
249+
def __del__(self) -> None:
249250
if self._monitor is not None:
250251
self._eudev._lib.udev_monitor_unref(self._monitor)
251252
self._monitor = None

0 commit comments

Comments
 (0)