web bugfix
[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
27         
28     result.append(f.table(1))
29     result.extend([
30         f.table_row(1),
31           f.table_cell(1), f.strong(1),
32             f.text('Band (MHz)'),
33           f.strong(0), f.table_cell(0),
34           f.table_cell(1), f.strong(1),
35             f.text('Max BW (MHz)'),
36           f.strong(0), f.table_cell(0),
37           f.table_cell(1), f.strong(1),
38             f.text('Flags'),
39           f.strong(0), f.table_cell(0),
40           f.table_cell(1), f.strong(1),
41             f.text('Environment'),
42           f.strong(0), f.table_cell(0),
43           f.table_cell(1), f.strong(1),
44             f.text('Max antenna gain (dBi)'),
45           f.strong(0), f.table_cell(0),
46           f.table_cell(1), f.strong(1),
47             f.text('Max IR PTMP (dBm)'),
48           f.strong(0), f.table_cell(0),
49           f.table_cell(1), f.strong(1),
50             f.text('Max IR PTP (dBm)'),
51           f.strong(0), f.table_cell(0),
52           f.table_cell(1), f.strong(1),
53             f.text('Max EIRP PTMP (dBm)'),
54           f.strong(0), f.table_cell(0),
55           f.table_cell(1), f.strong(1),
56             f.text('Max EIRP PTP (dBm)'),
57           f.strong(0), f.table_cell(0),
58         f.table_row(0),
59     ])
60
61     for b, p in country[code]:
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
130     country = request.form.get('alpha2', [None])[0]
131
132     dbpath = '/tmp/db.txt'
133     if hasattr(request.cfg, 'regdb_path'):
134         dbpath = request.cfg.regdb_path
135     bpc = DBParser().parse(open(dbpath))
136
137     if country:
138         return _country(macro, bpc, country)
139
140     band, power, country = bpc
141     countries = country.keys()
142     countries = [(_get_iso_code(code), code) for code in countries]
143     countries.sort()
144
145     result = []
146     result.append(macro.formatter.bullet_list(1))
147     for name, code in countries:
148         result.extend([
149           macro.formatter.listitem(1),
150           request.page.link_to(request, name, querystr={'alpha2': code}),
151           macro.formatter.listitem(0),
152         ])
153     result.append(macro.formatter.bullet_list(0))
154
155     return ''.join(result)
156