Skip to content

Commit a940342

Browse files
authored
Merge pull request #38 from maresb/fix-permissions-instructions
Fix permissions instructions
2 parents a689145 + bda8df4 commit a940342

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

src/labelle/lib/devices/usb_device.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import logging
44
import platform
5-
from typing import NoReturn
5+
from textwrap import dedent
6+
from typing import Any, NoReturn
67

78
import usb
89

@@ -35,59 +36,57 @@ def __init__(self, dev: usb.core.Device) -> None:
3536
self._devout = None
3637

3738
@property
38-
def hash(self):
39-
try:
40-
return (
41-
f"<{self._dev.manufacturer}|{self._dev.product}"
42-
f"|{self._dev.serial_number}>"
43-
)
44-
except (ValueError, usb.core.USBError):
45-
return None
39+
def hash(self) -> str:
40+
return self.usb_id
4641

47-
def _get_dev_attribute(self, attr):
42+
def _get_dev_attribute(self, attr: str) -> Any:
4843
try:
4944
return getattr(self._dev, attr)
5045
except (ValueError, usb.core.USBError):
5146
return None
5247

5348
@property
54-
def manufacturer(self):
49+
def manufacturer(self) -> str | None:
5550
return self._get_dev_attribute("manufacturer")
5651

5752
@property
58-
def product(self):
53+
def product(self) -> str | None:
5954
return self._get_dev_attribute("product")
6055

6156
@property
62-
def serial_number(self):
57+
def serial_number(self) -> str | None:
6358
return self._get_dev_attribute("serial_number")
6459

6560
@property
66-
def id_vendor(self):
67-
return self._get_dev_attribute("idVendor")
61+
def id_vendor(self) -> int:
62+
id_ = self._get_dev_attribute("idVendor")
63+
if id_ is None:
64+
raise UsbDeviceError("Could not get idVendor")
65+
return id_
6866

6967
@property
70-
def id_product(self):
71-
return self._get_dev_attribute("idProduct")
68+
def id_product(self) -> int:
69+
id_ = self._get_dev_attribute("idProduct")
70+
if id_ is None:
71+
raise UsbDeviceError("Could not get idProduct")
72+
return id_
7273

7374
@property
74-
def vendor_product_id(self):
75-
vendor_id = int(self.id_vendor)
76-
product_id = int(self.id_product)
77-
return f"{vendor_id:04x}:{product_id:04x}"
75+
def vendor_product_id(self) -> str:
76+
return f"{self.id_vendor:04x}:{self.id_product:04x}"
7877

7978
@property
80-
def usb_id(self):
79+
def usb_id(self) -> str:
8180
bus = self._get_dev_attribute("bus")
8281
address = self._get_dev_attribute("address")
8382
return f"Bus {bus:03} Device {address:03}: ID {self.vendor_product_id}"
8483

8584
@staticmethod
86-
def _is_supported_vendor(dev: usb.core.Device):
85+
def _is_supported_vendor(dev: usb.core.Device) -> bool:
8786
return dev.idVendor == DEV_VENDOR
8887

8988
@property
90-
def is_supported(self):
89+
def is_supported(self) -> bool:
9190
return (
9291
self._is_supported_vendor(self._dev)
9392
and self.id_product in SUPPORTED_PRODUCTS
@@ -165,26 +164,28 @@ def _instruct_on_access_denied_linux(self) -> NoReturn:
165164
# else:
166165
# restart_udev_command = None
167166

168-
udev_rule = ", ".join(
167+
udev_rule = ",'\\\n '".join(
169168
[
170169
'ACTION=="add"',
171170
'SUBSYSTEMS=="usb"',
172-
f'ATTRS{{idVendor}}=="{self._dev.idVendor:04x}"',
173-
f'ATTRS{{idProduct}}=="{self._dev.idProduct:04x}"',
171+
f'ATTRS{{idVendor}}=="{self.id_vendor:04x}"',
172+
f'ATTRS{{idProduct}}=="{self.id_product:04x}"',
174173
'MODE="0666"',
175174
]
176175
)
177176

178177
msg = f"""
178+
179179
You do not have sufficient access to the device. You probably want to add the a udev
180180
rule in /etc/udev/rules.d with the following command:
181181
182-
echo '{udev_rule}' | sudo tee /etc/udev/rules.d/91-labelle-{self._dev.idProduct:x}.rules
182+
echo '{udev_rule}' \\
183+
| sudo tee /etc/udev/rules.d/91-labelle-{self.id_vendor:04x}-{self.id_product:04x}.rules
183184
184185
Next, refresh udev with:
185186
186-
sudo udevadm control --reload-rules
187-
sudo udevadm trigger --attr-match=idVendor="0922"
187+
sudo udevadm control --reload-rules
188+
sudo udevadm trigger --attr-match=idVendor="0922"
188189
189190
Finally, turn your device off and back on again to activate the new permissions.
190191
@@ -194,8 +195,8 @@ def _instruct_on_access_denied_linux(self) -> NoReturn:
194195
In case you still cannot connect, or if you have any information or ideas, please
195196
post them at that link.
196197
""" # noqa: E501
197-
LOG.error(msg)
198-
raise UsbDeviceError("Insufficient access to the device")
198+
msg = dedent(msg)
199+
raise UsbDeviceError(msg)
199200

200201
def _set_configuration(self) -> None:
201202
try:
@@ -282,9 +283,9 @@ def is_match(self, patterns: list[str] | None) -> bool:
282283
for pattern in patterns:
283284
pattern = pattern.lower()
284285
match &= (
285-
pattern in self.manufacturer.lower()
286-
or pattern in self.product.lower()
287-
or pattern in self.serial_number.lower()
286+
pattern in (self.manufacturer or "").lower()
287+
or pattern in (self.product or "").lower()
288+
or pattern in (self.serial_number or "").lower()
288289
)
289290
return match
290291

0 commit comments

Comments
 (0)