Ville Skyttä | ca16c38 | 2017-05-29 10:08:14 +0300 | [diff] [blame] | 1 | # Reads the USB VID and PID from the file specified by sys.argv[1] and then |
Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 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 | |
Damien George | 96c6b8c | 2021-08-01 11:04:15 +1000 | [diff] [blame] | 11 | config_prefix = "MICROPY_HW_USB_" |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 12 | needed_keys = ("USB_PID_CDC_MSC", "USB_PID_CDC_HID", "USB_PID_CDC", "USB_VID") |
| 13 | |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame] | 14 | |
Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 15 | def parse_usb_ids(filename): |
| 16 | rv = dict() |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame] | 17 | for line in open(filename).readlines(): |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 18 | line = line.rstrip("\r\n") |
Christian Clauss | 8f8bd98 | 2023-03-10 06:28:44 +0100 | [diff] [blame] | 19 | match = re.match(r"^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$", line) |
Damien George | 96c6b8c | 2021-08-01 11:04:15 +1000 | [diff] [blame] | 20 | if match and match.group(1).startswith(config_prefix): |
| 21 | key = match.group(1).replace(config_prefix, "USB_") |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame] | 22 | val = match.group(2) |
Damien George | 96c6b8c | 2021-08-01 11:04:15 +1000 | [diff] [blame] | 23 | # print("key =", key, "val =", val) |
Dave Hylands | 01d6491 | 2015-10-02 23:25:31 -0700 | [diff] [blame] | 24 | if key in needed_keys: |
| 25 | rv[key] = val |
| 26 | for k in needed_keys: |
Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 27 | if k not in rv: |
| 28 | raise Exception("Unable to parse %s from %s" % (k, filename)) |
| 29 | return rv |
| 30 | |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 31 | |
Andrew Scheller | 1452221 | 2014-04-11 00:02:10 +0100 | [diff] [blame] | 32 | if __name__ == "__main__": |
| 33 | usb_ids_file = sys.argv[1] |
| 34 | template_file = sys.argv[2] |
| 35 | replacements = parse_usb_ids(usb_ids_file) |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 36 | for line in open(template_file, "r").readlines(): |
| 37 | print(string.Template(line).safe_substitute(replacements), end="") |