diff options
Diffstat (limited to 'serial/tools/list_ports_common.py')
-rw-r--r-- | serial/tools/list_ports_common.py | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/serial/tools/list_ports_common.py b/serial/tools/list_ports_common.py index df12939..617f3dc 100644 --- a/serial/tools/list_ports_common.py +++ b/serial/tools/list_ports_common.py @@ -7,7 +7,13 @@ # (C) 2015 Chris Liechti <cliechti@gmx.net> # # SPDX-License-Identifier: BSD-3-Clause + +from __future__ import absolute_import + import re +import glob +import os +import os.path def numsplit(text): @@ -29,9 +35,9 @@ def numsplit(text): class ListPortInfo(object): """Info collection base class for serial ports""" - def __init__(self, device=None): + def __init__(self, device, skip_link_detection=False): self.device = device - self.name = None + self.name = os.path.basename(device) self.description = 'n/a' self.hwid = 'n/a' # USB specific data @@ -42,6 +48,9 @@ class ListPortInfo(object): self.manufacturer = None self.product = None self.interface = None + # special handling for links + if not skip_link_detection and device is not None and os.path.islink(device): + self.hwid = 'LINK={}'.format(os.path.realpath(device)) def usb_description(self): """return a short string to name the port based on USB info""" @@ -66,9 +75,16 @@ class ListPortInfo(object): self.hwid = self.usb_info() def __eq__(self, other): - return self.device == other.device + return isinstance(other, ListPortInfo) and self.device == other.device + + def __hash__(self): + return hash(self.device) def __lt__(self, other): + if not isinstance(other, ListPortInfo): + raise TypeError('unorderable types: {}() and {}()'.format( + type(self).__name__, + type(other).__name__)) return numsplit(self.device) < numsplit(other.device) def __str__(self): @@ -85,6 +101,20 @@ class ListPortInfo(object): else: raise IndexError('{} > 2'.format(index)) + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +def list_links(devices): + """\ + search all /dev devices and look for symlinks to known ports already + listed in devices. + """ + links = [] + for device in glob.glob('/dev/*'): + if os.path.islink(device) and os.path.realpath(device) in devices: + links.append(device) + return links + + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # test if __name__ == '__main__': |