Adding a opening hours parser, creates a table from a simple string, with translations
authorFredrik Unger <fred@tree.se>
Fri, 6 Apr 2012 11:47:45 +0000 (13:47 +0200)
committerFredrik Unger <fred@tree.se>
Fri, 6 Apr 2012 11:47:45 +0000 (13:47 +0200)
xinclude/openinghours.py [new file with mode: 0755]

diff --git a/xinclude/openinghours.py b/xinclude/openinghours.py
new file mode 100755 (executable)
index 0000000..aab8220
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+# encoding: utf-8
+#
+import sys
+import datetime
+import locale
+
+for arg in sys.argv[1:]:
+  al = arg.split("=")
+  if al[0] == "lang":
+    lang = al[1]
+  if al[0] == "xptr":
+    argument = al[1]
+
+# example input:  ./openinghours.py lang=cs 'xptr=1,4;1-2|2,3;3-4:35'
+
+loc_alias = {
+    'sv': 'sv_SE.utf8',
+    'de': 'de_DE.utf8',
+    'en': 'en_US.utf8',
+    'cs': 'cs_CZ.utf8',
+}
+
+title = {
+    'sv': 'Öppningstider',
+    'de': 'Öffnungszeiten',
+    'en': 'Opening hours',
+    'cs': 'Otevírací doba',
+}
+day_header = {
+    'sv': 'Dag',
+    'de': 'Tag',
+    'en': 'Day',
+    'cs': 'Den',
+}
+time_header = {
+    'sv': 'Tid',
+    'de': 'Zeit',
+    'en': 'Time',
+    'cs': 'Čas',
+}
+
+
+loc = locale.getlocale()
+locale.setlocale(locale.LC_ALL, loc_alias[lang])
+day_names = [ locale.nl_langinfo(x)
+              for x in (locale.DAY_2, locale.DAY_3, locale.DAY_4,
+              locale.DAY_5, locale.DAY_6, locale.DAY_7, locale.DAY_1) ]
+locale.setlocale(locale.LC_ALL, loc)
+
+times = dict(enumerate('-------',1))
+
+blocks = argument.split('|')
+
+def tfmt(time):
+    if ':' in time:
+        (th, tm) = time.split(':')
+    else:
+        th = time
+        tm = '00'
+    td = datetime.datetime(2000, 1, 1, int(th), int(tm), 0)
+    return '{:%H:%M}'.format(td)
+
+for b in blocks:
+    (days, time) = b.split(';')
+    days = days.split(',')
+    (ts, te) = time.split('-')
+    t = tfmt(ts)+' - '+tfmt(te)
+    for d in days:
+        times[int(d)] = t
+
+out = ''
+out += '''<table frame='all' xmlns="http://docbook.org/ns/docbook"
+       xmlns:xlink="http://www.w3.org/1999/xlink">
+  <title>'''+title[lang]+'''</title>
+  <tgroup cols='2' align='left' colsep='1' rowsep='1'>
+    <colspec colname='day'/>
+    <colspec colname='time'/>
+    <thead>
+      <row>
+        <entry align="center">'''+day_header[lang]+'''</entry>
+        <entry align="center">'''+time_header[lang]+'''</entry>
+      </row>
+    </thead>
+ <tbody>'''
+
+
+for day,t in zip(day_names,times.values()) :
+    out += '''
+      <row>
+        <entry>%s</entry>
+        <entry>%s</entry>
+      </row>''' % (day,t)
+out += '''
+    </tbody>
+  </tgroup>
+</table>
+'''
+
+sys.stdout.write(out.decode('utf-8'))