From b750e9190046b15304574d4805fa479d2a1beaae Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Fri, 6 Apr 2012 13:47:45 +0200 Subject: [PATCH] Adding a opening hours parser, creates a table from a simple string, with translations --- xinclude/openinghours.py | 100 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 xinclude/openinghours.py diff --git a/xinclude/openinghours.py b/xinclude/openinghours.py new file mode 100755 index 0000000..aab8220 --- /dev/null +++ b/xinclude/openinghours.py @@ -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 += ''' + '''+title[lang]+''' + + + + + + '''+day_header[lang]+''' + '''+time_header[lang]+''' + + + ''' + + +for day,t in zip(day_names,times.values()) : + out += ''' + + %s + %s + ''' % (day,t) +out += ''' + + +
+''' + +sys.stdout.write(out.decode('utf-8')) -- 2.30.2