Skip to content

Commit e491980

Browse files
committed
feat: Cache autodiscovery data
Use data from port autodisovery to pre-populate Ports cache.
1 parent b51e4ee commit e491980

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

tests/helper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ def register_port_details(
6565
fp.register(["uhubctl", "-l", self.path, "-p", str(port_number)], stdout=stdout)
6666

6767
def cmd(
68-
self,
69-
fp: pytest_subprocess.FakeProcess,
70-
port_number: int = None,
71-
new_status: bool = None,
68+
self, fp: pytest_subprocess.FakeProcess, port_number: int = None, new_status: bool = None, n_arg: bool = True
7269
) -> None:
7370
if port_number is not None:
7471
assert port_number <= self.num_ports
7572

76-
cmd = ["uhubctl", "-N", "-l", str(self.path)]
73+
cmd = ["uhubctl"]
74+
if n_arg:
75+
cmd.append("-N")
76+
cmd += ["-l", str(self.path)]
7777
stdout = self.__stdout(port_filter=port_number)
7878

7979
if port_number is not None:

tests/test_hub.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ def test_str():
4545

4646

4747
def test_port_enumeration(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
48-
mock_hub.cmd(fp)
48+
mock_hub.cmd(fp, n_arg=False)
4949
mock_hub.discover_ports()
5050

5151
assert len(mock_hub.ports) == 5
5252

5353

5454
def test_no_devices(fp: pytest_subprocess.FakeProcess):
5555
fp.register(["uhubctl", "-N"], stdout=["No compatible devices detected!".encode()])
56+
fp.register(["uhubctl", "-v"], stdout="2.4.0-43-ge1e4d450")
5657

5758
hubs = uhubctl.discover_hubs()
5859

5960
assert hubs == []
6061

6162

6263
def test_find_port(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
63-
mock_hub.cmd(fp)
64+
mock_hub.cmd(fp, n_arg=False)
6465
mock_hub.discover_ports()
6566

6667
assert mock_hub.find_port(1) is not None

uhubctl/usb.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ def discover_ports(self) -> None:
9696
"""
9797
pattern = re.compile(r" Port (\d+): \d{4} ")
9898

99-
for line in UHubCtl.exec(["-l", self.path]):
99+
for line in UHubCtl.exec(["-l", self.path], description=True):
100100
regex = pattern.match(line)
101101

102102
if regex:
103-
port = Port(self, regex.group(1))
103+
# create Port object and pre-populate cache with line from autodiscovery
104+
port = Port(self, regex.group(1), cache=line)
104105
self.ports.append(port)
105106

106107
def __str__(self) -> str:
@@ -116,7 +117,7 @@ class Port:
116117
r" Port (?P<port>\d): \d{4} (?P<status>[a-z ]+)\s?(\[(?:(?P<vid>[a-f0-9]{4}):(?P<pid>[a-f0-9]{4})\s?(?P<description>.*)?)\])?"
117118
)
118119

119-
def __init__(self, hub: Hub, port_number: int):
120+
def __init__(self, hub: Hub, port_number: int, cache: str = None):
120121
"""
121122
Create new port instance
122123
@@ -127,7 +128,7 @@ def __init__(self, hub: Hub, port_number: int):
127128
"""
128129
self.hub = hub
129130
self.port_number = int(port_number)
130-
self.__cache = None
131+
self._cache = cache
131132

132133
@property
133134
def status(self) -> bool:
@@ -170,8 +171,8 @@ def __attribute(self, name: str, cache: bool = True, result_filter: callable = N
170171
raise AttributeError(name)
171172

172173
# use cache for port details if possible (fetching is slow)
173-
if cache and self.__cache is not None:
174-
lines = [self.__cache]
174+
if cache and self._cache is not None:
175+
lines = [self._cache]
175176
else:
176177
args = ["-l", self.hub.path, "-p", str(self.port_number)]
177178
lines = UHubCtl.exec(args, description=True)
@@ -183,7 +184,7 @@ def __attribute(self, name: str, cache: bool = True, result_filter: callable = N
183184
if int(reg.group("port")) != self.port_number:
184185
continue
185186

186-
self.__cache = line
187+
self._cache = line
187188

188189
if callable(result_filter):
189190
return result_filter(reg.group(name))

0 commit comments

Comments
 (0)