Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 1 | # 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 | |
| 5 | from __future__ import print_function |
| 6 | |
| 7 | import sys |
| 8 | import re |
| 9 | import string |
| 10 | |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame^] | 11 | needed_keys = ('USB_PID_CDC_MSC', 'USB_PID_CDC_HID', 'USB_PID_CDC', 'USB_VID') |
| 12 | |
Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 13 | def parse_usb_ids(filename): |
| 14 | rv = dict() |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame^] | 15 | 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 Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 25 | if k not in rv: |
| 26 | raise Exception("Unable to parse %s from %s" % (k, filename)) |
| 27 | return rv |
| 28 | |
| 29 | if __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='') |