-
Notifications
You must be signed in to change notification settings - Fork 0
/
vidpid.py
55 lines (47 loc) · 1.75 KB
/
vidpid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
this module allows to convert the vendor and product identifiers present
in the usb.ids file into a dictionary
any other file can be passed as a parameter as long as it respects the
original formatting
see http://www.linux-usb.org/usb.ids
"""
import re
# caret (^) is not used because re.match operates at the beginning of the string
regexp = re.compile("\t?[0-9a-f]{4} .")
class VidsPids(dict):
"""
builds a dictionary from the usb.ids file
"""
def __init__(self, source="/usr/share/hwdata/usb.ids"):
super().__init__()
for line in open(source, errors="backslashreplace"):
line = line.rstrip()
if regexp.match(line.lower()):
if line[0] != "\t":
vid, vendor = int(line[:4], 16), line[6:]
self[vid] = (vendor, {})
else:
line = line[1:]
pid, product = int(line[:4], 16), line[6:]
self[vid][1][pid] = product
def vendor(self, vendorid):
"""
returns the name of the specified vendor
the vendor ID is an integer (32902 or 0x8086)
"""
vidpids = self.get(vendorid)
return vidpids[0] if vidpids else ""
def products(self, vendorid):
"""
returns the specified vendor's products dictionary
the vendor ID is an integer (32902 or 0x8086)
"""
vidpids = self.get(vendorid)
return vidpids[1] if vidpids else {}
def product(self, vendorid, productid):
"""
returns the specified product for the specified vendor
the vendor ID and the product ID are integers (32902 or 0x8086)
"""
pids = self.products(vendorid)
return pids.get(productid) if pids else ""