Skip to content

Commit c2257be

Browse files
committed
feat: Make device description conditionally available
1 parent bcee5cc commit c2257be

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

uhubctl/usb.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class Port:
112112
USB port representation from uhubctl
113113
"""
114114

115+
PORT_PATTERN = re.compile(r" Port (?P<port>\d): \d{4} (?P<status>[a-z ]+)\w?(\[(?:(?P<vid>[a-f0-9]{4}):(?P<pid>[a-f0-9]{4})(?P<description>.*)?)\])?")
116+
115117
def __init__(self, hub: Hub, port_number: int):
116118
"""
117119
Create new port instance
@@ -130,14 +132,15 @@ def status(self) -> bool:
130132
Port power status
131133
"""
132134
status = None
133-
pattern = re.compile(rf" Port {self.port_number}: \d{{4}} (power|off|indicator)")
134135

135136
args = ["-l", self.hub.path, "-p", str(self.port_number)]
136137
for line in UHubCtl.exec(args):
137-
reg = pattern.match(line)
138+
reg = self.PORT_PATTERN.match(line)
138139

139140
if reg:
140-
status = reg.group(1) == "power"
141+
if reg.group("port") != self.port_number:
142+
continue
143+
status = "power" in reg.group("status")
141144

142145
if status is None:
143146
raise Exception()
@@ -155,6 +158,26 @@ def status(self, status: bool) -> None:
155158

156159
UHubCtl.exec(args)
157160

161+
@property
162+
def name(self) -> str:
163+
"""
164+
Attached device name
165+
"""
166+
167+
args = ["-l", self.hub.path, "-p", str(self.port_number)]
168+
for line in UHubCtl.exec(args):
169+
reg = self.PORT_PATTERN.match(line)
170+
171+
print(line)
172+
173+
if reg:
174+
if reg.group("port") != self.port_number:
175+
continue
176+
177+
return reg.group("description")
178+
179+
# raise Exception("could not determine name")
180+
158181
@staticmethod
159182
def from_path(path: str):
160183
"""

uhubctl/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def version(cls) -> str:
2222
return cls._version
2323

2424
@classmethod
25-
def exec(cls, args: list = None) -> list:
25+
def exec(cls, args: list = None, description: bool = False) -> list:
2626
cmd = UHUBCTL_BINARY.split(" ")
2727

28-
if version.parse(cls.version()) > version.parse("2.3.0"):
28+
if not description and version.parse(cls.version()) > version.parse("2.3.0"):
2929
cmd.append("-N")
3030

3131
if args is not None:

0 commit comments

Comments
 (0)