another web bugfix, cleanup
[platform/upstream/crda.git] / web / Regulatory.py
1 # -*- coding: iso-8859-1 -*-
2 """
3     Regulatory Database
4
5     @copyright: 2008 Johannes Berg
6     @license: GNU GPL, see COPYING for details.
7 """
8
9 import codecs
10 from dbparse import DBParser, band_flags
11
12 Dependencies = ["time"]
13
14 def _country(macro, bpc, code):
15     band, power, country = bpc
16     result = []
17
18     f = macro.formatter
19
20     result.extend([
21         f.heading(1, 1),
22         f.text('Regulatory definition for %s' % _get_iso_code(code)),
23         f.heading(0, 1),
24     ])
25
26     result.append(f.table(1))
27     result.extend([
28         f.table_row(1),
29           f.table_cell(1), f.strong(1),
30             f.text('Band (MHz)'),
31           f.strong(0), f.table_cell(0),
32           f.table_cell(1), f.strong(1),
33             f.text('Max BW (MHz)'),
34           f.strong(0), f.table_cell(0),
35           f.table_cell(1), f.strong(1),
36             f.text('Flags'),
37           f.strong(0), f.table_cell(0),
38           f.table_cell(1), f.strong(1),
39             f.text('Environment'),
40           f.strong(0), f.table_cell(0),
41           f.table_cell(1), f.strong(1),
42             f.text('Max antenna gain (dBi)'),
43           f.strong(0), f.table_cell(0),
44           f.table_cell(1), f.strong(1),
45             f.text('Max IR PTMP (dBm)'),
46           f.strong(0), f.table_cell(0),
47           f.table_cell(1), f.strong(1),
48             f.text('Max IR PTP (dBm)'),
49           f.strong(0), f.table_cell(0),
50           f.table_cell(1), f.strong(1),
51             f.text('Max EIRP PTMP (dBm)'),
52           f.strong(0), f.table_cell(0),
53           f.table_cell(1), f.strong(1),
54             f.text('Max EIRP PTP (dBm)'),
55           f.strong(0), f.table_cell(0),
56         f.table_row(0),
57     ])
58
59     l = list(country[code])
60     l.sort(cmp=lambda x,y: cmp(band[x[0]], band[y[0]]))
61     for b, p in l:
62         b = band[b]
63         p = power[p]
64         flags = []
65         for flag, val in band_flags.iteritems():
66             if b[3] & val:
67                 flags.append(flag)
68         e = {
69             'O': 'Outdoor',
70             'I': 'Indoor',
71             ' ': 'Out- & Indoor'
72         }[p[0]]
73         result.extend([
74             f.table_row(1),
75               f.table_cell(1),
76                 f.text('%.3f - %.3f' % (b[0], b[1])),
77               f.table_cell(0),
78               f.table_cell(1),
79                 f.text('%.3f' % (b[2],)),
80               f.table_cell(0),
81               f.table_cell(1),
82                 f.text(', '.join(flags)),
83               f.table_cell(0),
84               f.table_cell(1),
85                 f.text(e),
86               f.table_cell(0),
87               f.table_cell(1),
88                 f.text('%.3f' % p[1]),
89               f.table_cell(0),
90               f.table_cell(1),
91                 f.text('%.3f' % p[2]),
92               f.table_cell(0),
93               f.table_cell(1),
94                 f.text('%.3f' % p[3]),
95               f.table_cell(0),
96               f.table_cell(1),
97                 f.text('%.3f' % p[4]),
98               f.table_cell(0),
99               f.table_cell(1),
100                 f.text('%.3f' % p[5]),
101               f.table_cell(0),
102             f.table_row(0),
103         ])
104     
105     result.append(f.table(0))
106
107     result.append(f.linebreak(0))
108     result.append(f.linebreak(0))
109     result.append(macro.request.page.link_to(macro.request, 'return to country list'))
110     return ''.join(result)
111
112 _iso_list = {}
113
114 def _get_iso_code(code):
115     if not _iso_list:
116         for line in codecs.open('/usr/share/iso-codes/iso_3166.tab', encoding='utf-8'):
117             line = line.strip()
118             code, name = line.split('\t')
119             _iso_list[code] = name
120         _iso_list['00'] = 'Debug 1'
121         _iso_list['01'] = 'Debug 2'
122         _iso_list['02'] = 'Debug 3'
123         _iso_list['03'] = 'Debug 4'
124     return _iso_list.get(code, 'Unknown (%s)' % code)
125
126 def macro_Regulatory(macro):
127     _ = macro.request.getText
128     request = macro.request
129     f = macro.formatter
130
131     country = request.form.get('alpha2', [None])[0]
132
133     dbpath = '/tmp/db.txt'
134     if hasattr(request.cfg, 'regdb_path'):
135         dbpath = request.cfg.regdb_path
136     bpc = DBParser().parse(open(dbpath))
137
138     if country:
139         return _country(macro, bpc, country)
140
141     band, power, country = bpc
142     countries = country.keys()
143     countries = [(_get_iso_code(code), code) for code in countries]
144     countries.sort()
145
146     result = []
147
148     result.extend([
149         f.heading(1, 1),
150         f.text('Countries'),
151         f.heading(0, 1),
152     ])
153
154     result.append(f.bullet_list(1))
155     for name, code in countries:
156         result.extend([
157           f.listitem(1),
158           request.page.link_to(request, name, querystr={'alpha2': code}),
159           f.listitem(0),
160         ])
161     result.append(f.bullet_list(0))
162
163     return ''.join(result)
164