add option for naming

master
François Boulogne 3 months ago
parent f835556acf
commit 88da9c0ae1

@ -6,8 +6,12 @@ import re
import serial.tools.list_ports import serial.tools.list_ports
def list_USB_devices(): def list_USB_devices(full=True):
""" """
Return a list of USB devices as a dict.
if full is True, the name is Manufacturer_SerialNumber
otherwise, just Manufacturer
""" """
full_pattern = re.compile('[^a-zA-Z0-9\\\/]|_') full_pattern = re.compile('[^a-zA-Z0-9\\\/]|_')
@ -16,45 +20,45 @@ def list_USB_devices():
for p in ports: for p in ports:
if p.manufacturer: if p.manufacturer:
key = p.manufacturer key = p.manufacturer
if p.serial_number: if p.serial_number and full:
key += '_' + p.serial_number key += '_' + p.serial_number
key = re.sub(full_pattern, '_', key) key = re.sub(full_pattern, '_', key)
result[key] = p.device result[key] = p.device
return result return result
def list_USB_devices_old(): #def list_USB_devices_old():
result = {} # result = {}
path = Path('/sys/bus/usb/devices/') # path = Path('/sys/bus/usb/devices/')
#
for syspath in path.glob('usb*'): # for syspath in path.glob('usb*'):
for devfile in syspath.glob('**/dev'): # for devfile in syspath.glob('**/dev'):
if devfile.is_file(): # if devfile.is_file():
devpath = devfile.parent # devpath = devfile.parent
# Get devnames # # Get devnames
proc = subprocess.Popen(['udevadm', 'info', '-q', 'name', '-p', devpath], # proc = subprocess.Popen(['udevadm', 'info', '-q', 'name', '-p', devpath],
stdout=subprocess.PIPE) # stdout=subprocess.PIPE)
output = proc.stdout.read() # output = proc.stdout.read()
devname = output.decode('utf-8') # devname = output.decode('utf-8')
if not devname.startswith('bus') and not devname.startswith('input'): # if not devname.startswith('bus') and not devname.startswith('input'):
# get properties # # get properties
proc = subprocess.Popen(['udevadm', 'info', '-q', 'property', '--export', '-p', devpath], # proc = subprocess.Popen(['udevadm', 'info', '-q', 'property', '--export', '-p', devpath],
stdout=subprocess.PIPE) # stdout=subprocess.PIPE)
output = proc.stdout.read() # output = proc.stdout.read()
output = output.decode('utf-8') # output = output.decode('utf-8')
# Not nice, but convert the output into dict # # Not nice, but convert the output into dict
output = output.replace("='", "\':\'") # output = output.replace("='", "\':\'")
output = re.sub('^', "\'", output, flags=re.MULTILINE) # output = re.sub('^', "\'", output, flags=re.MULTILINE)
output = '{' + output[:-1].replace('\n', ', ') + '}' # output = '{' + output[:-1].replace('\n', ', ') + '}'
properties = ast.literal_eval(output) # properties = ast.literal_eval(output)
if 'ID_SERIAL' in properties: # if 'ID_SERIAL' in properties:
p = str(path.joinpath('/dev', devname)) # p = str(path.joinpath('/dev', devname))
p = p.replace('\n', '') # p = p.replace('\n', '')
result[properties['ID_SERIAL']] = p # result[properties['ID_SERIAL']] = p
return result # return result
def update_USB_devices(devices): def update_USB_devices(devices, fullname=True):
""" """
Find paths for devices. Find paths for devices.
@ -62,8 +66,9 @@ def update_USB_devices(devices):
detection, and bind the path. detection, and bind the path.
devices: dictionnary devices: dictionnary
fullname: if True, Manufacturer_SN, else Manufacturer.
""" """
res = list_USB_devices() res = list_USB_devices(full=fullname)
for device in devices: for device in devices:
name = devices[device]['name'] name = devices[device]['name']
if name in res: if name in res:

@ -1,4 +1,4 @@
__version__ = '0.1.2' __version__ = '0.1.3'
from .LTS import * from .LTS import *
from .USBdev import * from .USBdev import *

Loading…
Cancel
Save