Add initial version of the application. No, it isn't pretty, but it generates valid ical.

This commit is contained in:
Klaus-Uwe Mitterer 2017-06-12 13:57:16 +02:00
parent 442c4ca442
commit d2680cdcae
6 changed files with 118 additions and 0 deletions

1
__init__.py Normal file
View file

@ -0,0 +1 @@
from env import *

40
app.py Normal file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env python3
import handler
def app(env, re):
path = env["PATH_INFO"]
elements = path.split("/")
for _ in range(elements.count("")):
elements.remove("")
if len(elements) > 0:
if elements[0] == "env":
re('200 OK', [('Content-Type', 'text/html')])
return handler.env(env)
else:
try:
test = 1 if elements[0] == "test" else 0
date = str(int(elements[test]))
try:
event = elements[test + 1]
except:
event = None
rv = handler.cal(handler.date(date), event)
re('200 OK', [('Content-Type', 'text/plain' if test else 'text/calendar')])
return rv
except ValueError:
pass
except:
pass
else:
pass
return ""

3
handler/__init__.py Normal file
View file

@ -0,0 +1,3 @@
from handler.env import env
from handler.cal import cal
from handler.date import date

40
handler/cal.py Normal file
View file

@ -0,0 +1,40 @@
import datetime
def genday(dt):
now = datetime.date.today().strftime("%Y%m%dT000000Z")
then = dt.strftime("%Y%m%d")
di = "%i"
ds = "%s"
return '''BEGIN:VEVENT
UID:%s@kumig.it
DTSTAMP:%s
DTSTART;VALUE=DATE:%s
SUMMARY:%s days since %s
END:VEVENT
''' % (ds, now, then, di, ds)
def gencal(dt, offset = None, string = None, futuredays = 14):
offset = offset or (datetime.date.today() - dt).days
rv = '''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Klaus-Uwe Mitterer//DaysSince//EN
METHOD:PUBLISH
'''
for day in range(offset + futuredays):
rv += genday(dt + datetime.timedelta(days = day)) % ("%i%i%i%i" % (dt.year, dt.month, dt.day, day), day, "%i/%i/%i" % (dt.year, dt.month, dt.day))
rv += "END:VCALENDAR"
return rv
def cal(dt, string = None):
offset = (datetime.date.today() - dt).days
if offset < 0:
return ""
else:
return iter([gencal(dt, string).replace("\n", "\r\n").encode()])

11
handler/date.py Normal file
View file

@ -0,0 +1,11 @@
import datetime
def date(indate):
try:
try:
return datetime.datetime.strptime(indate, "%Y%m%d").date()
except Exception:
print(e)
return datetime.datetime.strptime(indate, "%y%m%d").date()
except Exception as e:
raise ValueError(e)

23
handler/env.py Normal file
View file

@ -0,0 +1,23 @@
import cgi
def env(data):
out = '''<!DOCTYPE HTML>
<html>
<head>
<title>Environment!</title>
</head>
<body>
<table>'''
for key, value in data.items():
try:
out += '''\n <tr><th>%s</th><td>%s</td></tr>''' % (cgi.escape(key), cgi.escape(repr(value)))
except Exception:
pass
out += '''
</table>
</body>
</html>'''
return iter([out.encode()])