blob: 420db34c5a95995eff9de139b2306db900808af8 [file] [log] [blame]
Andrew Scheller14522212014-04-11 00:02:10 +01001# Reads the USB VID and PID from the file specifed by sys.arg[1] and then
2# inserts those values into the template file specified by sys.argv[2],
3# printing the result to stdout
4
5from __future__ import print_function
6
7import sys
8import re
9import string
10
Dave Hylands01d64912015-10-02 23:25:31 -070011needed_keys = ('USB_PID_CDC_MSC', 'USB_PID_CDC_HID', 'USB_PID_CDC', 'USB_VID')
12
Andrew Scheller14522212014-04-11 00:02:10 +010013def parse_usb_ids(filename):
14 rv = dict()
Dave Hylands01d64912015-10-02 23:25:31 -070015 for line in open(filename).readlines():
16 line = line.rstrip('\r\n')
17 match = re.match('^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$', line)
18 if match and match.group(1).startswith('USBD_'):
19 key = match.group(1).replace('USBD', 'USB')
20 val = match.group(2)
21 print("key =", key, "val =", val)
22 if key in needed_keys:
23 rv[key] = val
24 for k in needed_keys:
Andrew Scheller14522212014-04-11 00:02:10 +010025 if k not in rv:
26 raise Exception("Unable to parse %s from %s" % (k, filename))
27 return rv
28
29if __name__ == "__main__":
30 usb_ids_file = sys.argv[1]
31 template_file = sys.argv[2]
32 replacements = parse_usb_ids(usb_ids_file)
33 for line in open(template_file, 'r').readlines():
34 print(string.Template(line).safe_substitute(replacements), end='')