Index: tools/usb_ids/usb_ids.py |
diff --git a/tools/usb_ids/usb_ids.py b/tools/usb_ids/usb_ids.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b480061df57ddc7782e0de0966a84509e004535b |
--- /dev/null |
+++ b/tools/usb_ids/usb_ids.py |
@@ -0,0 +1,117 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import itertools |
+import optparse |
+import re |
+ |
+VENDOR_PATTERN = re.compile("^(?P<id>[0-9a-fA-F]{4})\s+(?P<name>.+)$") |
+PRODUCT_PATTERN = re.compile("^\t(?P<id>[0-9a-fA-F]{4})\s+(?P<name>.+)$") |
+ |
+def EscapeName(name): |
+ name = name.replace("\\", "\\\\") |
+ name = name.replace("\"", "\\\"") |
+ name = name.replace("?", "\?") |
+ return name |
+ |
+def ParseTable(input_path): |
+ input_file = open(input_path, "r") |
+ input = input_file.read().split("\n").__iter__() |
+ input_file.close() |
+ |
+ vendor = None |
+ vendors = [] |
+ |
+ try: |
+ while True: |
+ line = input.next() |
+ |
+ vendor_match = VENDOR_PATTERN.match(line) |
+ if vendor_match: |
+ if vendor: |
+ vendors.append(vendor) |
+ vendor = {} |
+ vendor["id"] = int(vendor_match.group("id"), 16) |
+ vendor["name"] = vendor_match.group("name") |
+ vendor["products"] = [] |
+ continue |
+ |
+ product_match = PRODUCT_PATTERN.match(line) |
+ if product_match: |
+ if not vendor: |
+ print "oops!" |
bryeung
2012/10/31 15:01:27
could this actually happen? If not, I'd prefer a
Garret Kelly
2012/10/31 15:24:26
If the usb.ids file is properly formatted, then no
|
+ product = {} |
+ product["id"] = int(product_match.group("id"), 16) |
+ product["name"] = product_match.group("name") |
+ vendor["products"].append(product) |
+ except StopIteration: |
+ pass |
+ |
+ table = {} |
+ for vendor in vendors: |
bryeung
2012/10/31 15:01:27
why iterate again? Couldn't this be built up as y
Garret Kelly
2012/10/31 15:24:26
Good catch. Done.
|
+ table[vendor["id"]] = vendor |
+ |
+ return table |
+ |
+def GenerateDeviceDefinitions(table): |
+ output = "" |
+ |
+ for vendor_id in sorted(table.keys()): |
+ vendor = table[vendor_id] |
+ if len(vendor["products"]) == 0: |
+ continue |
+ |
+ output += "static const UsbProduct vendor_%.4x_products[] = {\n" % \ |
+ vendor["id"] |
+ for product in vendor["products"]: |
+ output += " {0x%.4x, \"%s\"},\n" % (product["id"], |
+ EscapeName(product["name"])) |
+ output += "};\n" |
+ |
+ return output |
+ |
+def GenerateVendorDefinitions(table): |
+ output = "const size_t UsbIds::vendor_size_ = %d;\n" % len(table.keys()) |
+ output += "const UsbVendor UsbIds::vendors_[] = {\n" |
+ |
+ for vendor_id in sorted(table.keys()): |
+ vendor = table[vendor_id] |
+ |
+ product_table = "NULL" |
+ if len(vendor["products"]) != 0: |
+ product_table = "vendor_%.4x_products" % (vendor["id"]) |
+ output += " {0x%.4x, \"%s\", %d, %s},\n" % (vendor["id"], |
+ EscapeName(vendor["name"]), len(vendor["products"]), product_table) |
+ |
+ output += "};\n" |
+ return output |
+ |
+if __name__ == "__main__": |
+ parser = optparse.OptionParser( |
+ description="Generates an embeddable USB ID lookup table.") |
bryeung
2012/10/31 15:01:27
"an embeddable" could maybe changed to "a C++" for
Garret Kelly
2012/10/31 15:24:26
Done.
|
+ parser.add_option("-i", "--input", help="Path to usb.ids") |
+ parser.add_option("-o", "--output", help="Output file path") |
+ |
+ (opts, args) = parser.parse_args() |
+ table = ParseTable(opts.input) |
+ |
+ output = """ |
+#ifndef GENERATED_USB_IDS_H_ |
+#define GENERATED_USB_IDS_H_ |
+ |
+#include "device/usb/usb_ids.h" |
+ |
+namespace device { |
bryeung
2012/10/31 15:01:27
blank line after namespace
Garret Kelly
2012/10/31 15:24:26
Done.
|
+""" |
+ output += GenerateDeviceDefinitions(table) |
+ output += GenerateVendorDefinitions(table) |
+ output += """ |
+} // namespace device |
bryeung
2012/10/31 15:01:27
blank line before end of namespace
Garret Kelly
2012/10/31 15:24:26
Done.
|
+ |
+#endif // GENERATED_USB_IDS_H_ |
+""" |
+ |
+ output_file = open(opts.output, "w+") |
+ output_file.write(output) |
+ output_file.close() |