Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(554)

Side by Side Diff: tools/usb_ids/usb_ids.py

Issue 11344039: Adding USB ID vendor and product lookups. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding documentation strings. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« device/usb/usb_ids.cc ('K') | « device/usb/usb_ids_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import itertools
6 import optparse
7 import re
8
9 VENDOR_PATTERN = re.compile("^(?P<id>[0-9a-fA-F]{4})\s+(?P<name>.+)$")
10 PRODUCT_PATTERN = re.compile("^\t(?P<id>[0-9a-fA-F]{4})\s+(?P<name>.+)$")
11
12 def EscapeName(name):
13 name = name.replace("\\", "\\\\")
14 name = name.replace("\"", "\\\"")
15 name = name.replace("?", "\?")
16 return name
17
18 def ParseTable(input_path):
19 input_file = open(input_path, "r")
20 input = input_file.read().split("\n").__iter__()
21 input_file.close()
22
23 vendor = None
24 vendors = []
25
26 try:
27 while True:
28 line = input.next()
29
30 vendor_match = VENDOR_PATTERN.match(line)
31 if vendor_match:
32 if vendor:
33 vendors.append(vendor)
34 vendor = {}
35 vendor["id"] = int(vendor_match.group("id"), 16)
36 vendor["name"] = vendor_match.group("name")
37 vendor["products"] = []
38 continue
39
40 product_match = PRODUCT_PATTERN.match(line)
41 if product_match:
42 if not vendor:
43 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
44 product = {}
45 product["id"] = int(product_match.group("id"), 16)
46 product["name"] = product_match.group("name")
47 vendor["products"].append(product)
48 except StopIteration:
49 pass
50
51 table = {}
52 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.
53 table[vendor["id"]] = vendor
54
55 return table
56
57 def GenerateDeviceDefinitions(table):
58 output = ""
59
60 for vendor_id in sorted(table.keys()):
61 vendor = table[vendor_id]
62 if len(vendor["products"]) == 0:
63 continue
64
65 output += "static const UsbProduct vendor_%.4x_products[] = {\n" % \
66 vendor["id"]
67 for product in vendor["products"]:
68 output += " {0x%.4x, \"%s\"},\n" % (product["id"],
69 EscapeName(product["name"]))
70 output += "};\n"
71
72 return output
73
74 def GenerateVendorDefinitions(table):
75 output = "const size_t UsbIds::vendor_size_ = %d;\n" % len(table.keys())
76 output += "const UsbVendor UsbIds::vendors_[] = {\n"
77
78 for vendor_id in sorted(table.keys()):
79 vendor = table[vendor_id]
80
81 product_table = "NULL"
82 if len(vendor["products"]) != 0:
83 product_table = "vendor_%.4x_products" % (vendor["id"])
84 output += " {0x%.4x, \"%s\", %d, %s},\n" % (vendor["id"],
85 EscapeName(vendor["name"]), len(vendor["products"]), product_table)
86
87 output += "};\n"
88 return output
89
90 if __name__ == "__main__":
91 parser = optparse.OptionParser(
92 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.
93 parser.add_option("-i", "--input", help="Path to usb.ids")
94 parser.add_option("-o", "--output", help="Output file path")
95
96 (opts, args) = parser.parse_args()
97 table = ParseTable(opts.input)
98
99 output = """
100 #ifndef GENERATED_USB_IDS_H_
101 #define GENERATED_USB_IDS_H_
102
103 #include "device/usb/usb_ids.h"
104
105 namespace device {
bryeung 2012/10/31 15:01:27 blank line after namespace
Garret Kelly 2012/10/31 15:24:26 Done.
106 """
107 output += GenerateDeviceDefinitions(table)
108 output += GenerateVendorDefinitions(table)
109 output += """
110 } // namespace device
bryeung 2012/10/31 15:01:27 blank line before end of namespace
Garret Kelly 2012/10/31 15:24:26 Done.
111
112 #endif // GENERATED_USB_IDS_H_
113 """
114
115 output_file = open(opts.output, "w+")
116 output_file.write(output)
117 output_file.close()
OLDNEW
« device/usb/usb_ids.cc ('K') | « device/usb/usb_ids_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698