Skip to content

Commit bafb213

Browse files
committed
feat: Create Port from USB path
Add static method which creates Path object from given USB path. Signed-off-by: Nicolai Buchwitz <nb@tipi-net.de>
1 parent 1432f21 commit bafb213

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

tests/test_port.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ def test_port_status(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
5050

5151
mock_hub.cmd(fp, port.port_number)
5252
assert port.status is False
53+
54+
55+
def test_port_from_path():
56+
mock_data = [("1", 2), ("2-1", 1), ("1-2.3.1", 5)]
57+
for element in mock_data:
58+
path, port_number = element
59+
60+
port = Port.from_path(f"{path}.{port_number}")
61+
62+
assert port.hub.path == path
63+
assert port.port_number == port_number

uhubctl/usb.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,23 @@ def status(self, status: bool) -> None:
157157

158158
_uhubctl(args)
159159

160+
@staticmethod
161+
def from_path(path: str):
162+
"""
163+
Create new port instance from USB path
164+
165+
Arguments:
166+
path: USB path
167+
168+
Returns:
169+
Port
170+
"""
171+
hub_path, port_id = path.rsplit(".", maxsplit=1)
172+
173+
hub = Hub(hub_path, enumerate_ports=False)
174+
port = hub.add_port(port_id)
175+
176+
return port
177+
160178
def __str__(self) -> str:
161179
return f"USB Port {self.hub.path}.{self.port_number}"

0 commit comments

Comments
 (0)