blob: 177a68c4a45fb3e5e3b001d5e0f6ac62571f6cd8 [file] [log] [blame]
Ville Skyttäca16c382017-05-29 10:08:14 +03001# Reads the USB VID and PID from the file specified by sys.argv[1] and then
Andrew Scheller14522212014-04-11 00:02:10 +01002# 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
Damien George96c6b8c2021-08-01 11:04:15 +100011config_prefix = "MICROPY_HW_USB_"
Damien George69661f32020-02-27 15:36:53 +110012needed_keys = ("USB_PID_CDC_MSC", "USB_PID_CDC_HID", "USB_PID_CDC", "USB_VID")
13
Dave Hylands01d64912015-10-02 23:25:31 -070014
Andrew Scheller14522212014-04-11 00:02:10 +010015def parse_usb_ids(filename):
16 rv = dict()
Dave Hylands01d64912015-10-02 23:25:31 -070017 for line in open(filename).readlines():
Damien George69661f32020-02-27 15:36:53 +110018 line = line.rstrip("\r\n")
Christian Clauss8f8bd982023-03-10 06:28:44 +010019 match = re.match(r"^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$", line)
Damien George96c6b8c2021-08-01 11:04:15 +100020 if match and match.group(1).startswith(config_prefix):
21 key = match.group(1).replace(config_prefix, "USB_")
Dave Hylands01d64912015-10-02 23:25:31 -070022 val = match.group(2)
Damien George96c6b8c2021-08-01 11:04:15 +100023 # print("key =", key, "val =", val)
Dave Hylands01d64912015-10-02 23:25:31 -070024 if key in needed_keys:
25 rv[key] = val
26 for k in needed_keys:
Andrew Scheller14522212014-04-11 00:02:10 +010027 if k not in rv:
28 raise Exception("Unable to parse %s from %s" % (k, filename))
29 return rv
30
Damien George69661f32020-02-27 15:36:53 +110031
Andrew Scheller14522212014-04-11 00:02:10 +010032if __name__ == "__main__":
33 usb_ids_file = sys.argv[1]
34 template_file = sys.argv[2]
35 replacements = parse_usb_ids(usb_ids_file)
Damien George69661f32020-02-27 15:36:53 +110036 for line in open(template_file, "r").readlines():
37 print(string.Template(line).safe_substitute(replacements), end="")