Fixing UTF-8 encoding for the foreign texts and characters.
[treecutter.git] / xinclude / openinghours.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 import sys
5 import datetime
6 import locale
7
8 for arg in sys.argv[1:]:
9   al = arg.split("=")
10   if al[0] == "lang":
11     lang = al[1]
12   if al[0] == "xptr":
13     argument = al[1]
14
15 # example input:  ./openinghours.py lang=cs 'xptr=1,4;1-2|2,3;3-4:35'
16
17 loc_alias = {
18     'sv': 'sv_SE.utf8',
19     'de': 'de_DE.utf8',
20     'en': 'en_US.utf8',
21     'cs': 'cs_CZ.utf8',
22 }
23
24 title = {
25     'sv': u'Öppningstider',
26     'de': u'Öffnungszeiten',
27     'en': u'Opening hours',
28     'cs': u'Otevírací doba',
29 }
30 day_header = {
31     'sv': 'Dag',
32     'de': 'Tag',
33     'en': 'Day',
34     'cs': 'Den',
35 }
36 time_header = {
37     'sv': 'Tid',
38     'de': 'Zeit',
39     'en': 'Time',
40     'cs': 'Čas',
41 }
42
43
44 loc = locale.getlocale()
45 locale.setlocale(locale.LC_ALL, loc_alias[lang])
46 day_names = [ locale.nl_langinfo(x)
47               for x in (locale.DAY_2, locale.DAY_3, locale.DAY_4,
48               locale.DAY_5, locale.DAY_6, locale.DAY_7, locale.DAY_1) ]
49 locale.setlocale(locale.LC_ALL, loc)
50
51 times = dict(enumerate('-------',1))
52
53 blocks = argument.split('|')
54
55 def tfmt(time):
56     if ':' in time:
57         (th, tm) = time.split(':')
58     else:
59         th = time
60         tm = '00'
61     td = datetime.datetime(2000, 1, 1, int(th), int(tm), 0)
62     return '{:%H:%M}'.format(td)
63
64 for b in blocks:
65     (days, time) = b.split(';')
66     days = days.split(',')
67     (ts, te) = time.split('-')
68     t = tfmt(ts)+' - '+tfmt(te)
69     for d in days:
70         times[int(d)] = t
71
72 out = u''
73 out += '''<table frame='all' xmlns="http://docbook.org/ns/docbook"
74        xmlns:xlink="http://www.w3.org/1999/xlink">
75   <title>'''+title[lang]+'''</title>
76   <tgroup cols='2' align='left' colsep='1' rowsep='1'>
77     <colspec colname='day'/>
78     <colspec colname='time'/>
79     <thead>
80       <row>
81         <entry align="center">'''+day_header[lang]+'''</entry>
82         <entry align="center">'''+time_header[lang]+'''</entry>
83       </row>
84     </thead>
85  <tbody>'''
86
87
88 for day,t in zip(day_names,times.values()) :
89     out += '''
90       <row>
91         <entry>%s</entry>
92         <entry>%s</entry>
93       </row>''' % (day,t)
94 out += '''
95     </tbody>
96   </tgroup>
97 </table>
98 '''
99
100 sys.stdout.write(out.encode('utf-8'))