Skip to content

Commit 92ad25c

Browse files
committed
Improve description filter generation.
* don't generate invalid filter when description contains port name multiple times * if a bad filter got generated use empty filter instead
1 parent 01508f6 commit 92ad25c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

inkcut/device/transports/serialport/plugin.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,20 @@ def make_filter(self, port_info: SerialPortInfo):
105105
# also more likely to have stable device path so there is less need for filter.
106106
if port_info.description:
107107
text = port_info.description
108-
text = text.replace(port_info.device_path, '').strip()
109-
text = text.strip('-: ')
110-
if text:
111-
self.port_filter_name = re.escape(text)
108+
text = text.strip(' -:()')
109+
parts = text.split(port_info.device_path)
110+
best = ""
111+
for part in parts:
112+
if len(part) > len(best):
113+
best = part
114+
115+
best = best.strip(' -:()')
116+
117+
if best:
118+
self.port_filter_name = re.escape(best)
119+
if not re.search(self.port_filter_name, port_info.description):
120+
log.warn("Failed to create port description filter for '{}'".format(port_info.description))
121+
self.port_filter_name = ""
112122

113123
def port_by_path(self, device_path):
114124
for port in self.ports:

tests/transport/test_serial_config.py

+8
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ def test_make_filter():
8585
config = SerialConfigBase()
8686
config.make_filter(info)
8787
assert config.port_filter_name == "description"
88+
89+
info = SerialPortInfo(device_path='COM3', description=r" (COM3) description (COM3) ",
90+
usb_vid=123, usb_pid=321)
91+
config = SerialConfigBase()
92+
config.make_filter(info)
93+
assert config.port_matches(info)
94+
assert not config.port_matches(SerialPortInfo(device_path='COM3', description=r"zescriptionz",
95+
usb_vid=123, usb_pid=321))

0 commit comments

Comments
 (0)