"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / gpscap.py
1 """
2
3 gpscap - GPS/AIS capability dictionary class.
4
5 This file is Copyright (c) 2010 by the GPSD project
6 BSD terms apply: see the file COPYING in the distribution root for details.
7 """
8 import ConfigParser
9
10 class GPSDictionary(ConfigParser.RawConfigParser):
11     def __init__(self, *files):
12         "Initialize the capability dictionary"
13         ConfigParser.RawConfigParser.__init__(self)
14         if not files:
15             files = ["gpscap.ini", "/usr/share/gpsd/gpscap.ini"]
16         self.read(files)
17         # Resolve uses= members
18         while True:
19             keepgoing = False
20             for section in self.sections():
21                 if self.has_option(section, "uses"):
22                     parent = self.get(section, "uses")
23                     if self.has_option(parent, "uses"):
24                         continue
25                     # Found a parent section without a uses = part.
26                     for heritable in self.options(parent):
27                         if not self.has_option(section, heritable):
28                             self.set(section,
29                                      heritable,
30                                      self.get(parent, heritable))
31                             keepgoing = True
32                     self.remove_option(section, "uses")
33             if not keepgoing:
34                 break
35         # Sanity check: All items must have a type field.
36         for section in self.sections():
37             if not self.has_option(section, "type"):
38                 raise ConfigParser.Error("%s has no type" % section)
39             elif self.get(section, "type") not in ("engine", "vendor", "device"):
40                 raise ConfigParser.Error("%s has invalid type" % section)
41         # Sanity check: All devices must point at a vendor object.
42         # Side effect: build the lists of vendors and devices.
43         self.vendors = []
44         self.devices = []
45         for section in self.sections():
46             if self.get(section, "type") == "vendor":
47                 self.vendors.append(section)
48             if self.get(section, "type") == "device":
49                 self.devices.append(section)
50         self.vendors.sort()
51         for section in self.sections():
52             if self.get(section, "type") == "device":
53                 if not self.has_option(section, "vendor"):
54                     raise ConfigParser.Error("%s has no vendor" % section)
55                 if self.get(section, "vendor") not in self.vendors:
56                     raise ConfigParser.Error("%s has invalid vendor" % section)
57
58     def HTMLDump(self, ofp):
59         thead = """<table border='1' style='font-size:small;' bgcolor='#CCCCCC'>
60 <caption>Listing %s devices from %s vendors</caption>
61 <tr>
62 <th>Name</th>
63 <th>Packaging</th>
64 <th>Engine</th>
65 <th>Interface</th>
66 <th>Tested with</th>
67 <th>NMEA version</th>
68 <th width='50%%'>Notes</th>
69 </tr>
70 """
71         vhead = "<tr><td style='text-align:center;' colspan='7'><a href='%s'>%s</a></td></tr>\n"
72         hotpluggables = ("pl2303", "CP2101")
73         ofp.write(thead % (len(self.devices), len(self.vendors)))
74         for vendor in self.vendors:
75             ofp.write(vhead % (self.get(vendor, "vendor_site"), vendor))
76             relevant = []
77             for dev in self.devices:
78                 if self.get(dev, "vendor") == vendor:
79                     relevant.append(dev)
80             relevant.sort()
81             for dev in relevant:
82                 rowcolor = "#FFFFFF"
83                 if self.get(dev, "packaging") == "OEM module":
84                     rowcolor = "LimeGreen"
85                 elif self.get(dev, "packaging") == "chipset":
86                     rowcolor = "LightYellow"
87                 elif self.get(dev, "packaging") == "handset":
88                     rowcolor = "Cyan"
89                 elif self.get(dev, "packaging") == "hansdfree":
90                     rowcolor = "DarkCyan"
91
92                 ofp.write("<tr bgcolor='%s'>\n" % rowcolor)
93                 namefield = dev
94                 if self.has_option(dev, "techdoc"):
95                     namefield = "<a href='%s'>%s</a>" % (self.get(dev, "techdoc"), dev)
96                 if self.has_option(dev, "discontinued"):
97                     namefield = namefield + "&nbsp;<img title='Device discontinued' src='discontinued.png'/>"
98                 ofp.write("<td>%s</td>\n" % namefield)
99                 ofp.write("<td>%s</td>\n" % self.get(dev, "packaging"))
100                 engine = self.get(dev, "engine")
101                 if self.has_option(engine, "techdoc"):
102                     engine = "<a href='%s'>%s</a>" % (self.get(engine, "techdoc"), engine)
103                 if self.has_option(dev, "subtype"):
104                     engine += " (" + self.get(dev, "subtype") + ")"
105                 ofp.write("<td>%s</td>\n" % engine)
106                 interfaces = self.get(dev, "interfaces")
107                 if self.has_option(dev, "pps"):
108                     interfaces += ",PPS"
109                 ofp.write("<td>%s</td>\n" % interfaces)
110                 testfield = ""
111                 if self.has_option(dev, "tested"):
112                     tested = self.get(dev, "tested")
113                     if tested == "regression":
114                         testfield += "<img title='Have regression test' src='regression.png'>"
115                     else:
116                         testfield += tested
117                 if self.has_option(dev, "noconfigure"):
118                     testfield += "<img title='Requires -b option' src='noconfigure.png'>"
119                 if self.get(dev, "rating") == "excellent":
120                     testfield += "<img src='star.png'/><img src='star.png'/><img src='star.png'/><img src='star.png'/>"
121                 elif self.get(dev, "rating") == "good":
122                     testfield += "<img src='star.png'/><img src='star.png'/'><img src='star.png'/>"
123                 elif self.get(dev, "rating") == "fair":
124                     testfield += "<img src='star.png'/><img src='star.png'/>"
125                 elif self.get(dev, "rating") == "poor":
126                     testfield += "<img src='star.png'/>"
127                 elif self.get(dev, "rating") == "broken":
128                     testfield += "<img title='Device is broken' src='bomb.png'/>"
129                 if self.has_option(dev, "usbchip") and self.get(dev, "usbchip") in hotpluggables:
130                     testfield += "<img src='hotplug.png'/>"
131                 ofp.write("<td>%s</td>\n" % testfield)
132                 nmea = "&nbsp;"
133                 if self.has_option(dev, "nmea"):
134                     nmea = self.get(dev, "nmea")
135                 ofp.write("<td>%s</td>\n" % nmea)
136                 if self.has_option(dev, "notes"):
137                     notes = self.get(dev, "notes")
138                 else:
139                     notes = ""
140                 if self.has_option(dev, "submitter"):
141                     notes += " Reported by %s." % self.get(dev, "submitter")
142                 notes = notes.replace("@", "&#x40;").replace("<", "&lt;").replace(">", "&gt;")
143                 ofp.write("<td>%s</td>\n" % notes)
144                 ofp.write("</tr>\n")
145         ofp.write("</table>\n")
146
147
148 if __name__ == "__main__":
149     import sys
150     d = GPSDictionary()
151     d.HTMLDump(sys.stdout)