1
0
Fork 0

Talks generate schedule.ics calendar

This commit is contained in:
boyska 2017-05-06 02:00:42 +02:00 committed by dbz
parent aceb09d1c9
commit 83b581f3b7
2 changed files with 59 additions and 3 deletions

View file

@ -10,6 +10,7 @@ import logging
import re import re
import datetime import datetime
import shutil import shutil
import time
from docutils import nodes from docutils import nodes
from docutils.parsers.rst import directives, Directive from docutils.parsers.rst import directives, Directive
@ -44,6 +45,30 @@ def get_talk_names():
] ]
def all_talks():
return [get_talk_data(tn) for tn in get_talk_names()]
@memoize
def get_global_data():
fname = os.path.join(TALKS_PATH, 'meta.yaml')
if not os.path.isfile(fname):
return None
with io.open(fname, encoding='utf8') as buf:
try:
data = yaml.load(buf)
except Exception as exc:
logging.exception("Syntax error reading %s; skipping", fname)
return None
if data is None:
return None
if 'startdate' not in data:
logging.error("Missing startdate in global data")
data['startdate'] = datetime.datetime.now()
return data
@memoize @memoize
def get_talk_data(talkname): def get_talk_data(talkname):
fname = os.path.join(TALKS_PATH, talkname, 'meta.yaml') fname = os.path.join(TALKS_PATH, talkname, 'meta.yaml')
@ -72,11 +97,14 @@ def get_talk_data(talkname):
logging.warn("Talk <{}> has no `room` field".format(talkname)) logging.warn("Talk <{}> has no `room` field".format(talkname))
if 'time' not in data or 'day' not in data: if 'time' not in data or 'day' not in data:
logging.warn("Talk <{}> has no `time` or `day`".format(talkname)) logging.warn("Talk <{}> has no `time` or `day`".format(talkname))
if 'time' in data: if 'day' in data:
data['day'] = get_global_data()['startdate'] + datetime.timedelta(days=data['day'])
if 'time' in data and 'day' in data:
timeparts = re.findall(r'\d+', str(data['time'])) timeparts = re.findall(r'\d+', str(data['time']))
if 4 > len(timeparts) > 0: if 4 > len(timeparts) > 0:
timeparts = [int(p) for p in timeparts] timeparts = [int(p) for p in timeparts]
data['time'] = datetime.time(*timeparts) data['time'] = datetime.datetime.combine(data['day'],
datetime.time(*timeparts))
else: else:
logging.error("Talk <{}> has malformed `time`".format(talkname)) logging.error("Talk <{}> has malformed `time`".format(talkname))
data['id'] = talkname data['id'] = talkname
@ -123,7 +151,32 @@ class TalkDirective(Directive):
format='html') format='html')
] ]
# TODO: TalkGridDirective (griglia completa)
def talks_to_ics():
content = 'BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:pelican\n'
for t in all_talks():
content += talk_to_ics(t)
content += 'END:VCALENDAR\n'
return content
def talk_to_ics(talk):
start = talk['time']
end = start + datetime.timedelta(minutes=talk['duration'])
content = 'BEGIN:VEVENT\n'
content += "UID:%s@%d.hackmeeting.org\n" % (talk['id'], talk['day'].year)
content += "SUMMARY:%s\n" % talk['title']
content += "DTSTAMP:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
time.gmtime(float(start.strftime('%s'))))
content += "DTSTART:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
time.gmtime(float(
start.strftime('%s'))))
content += "DTEND:%s\n" % time.strftime('%Y%m%dT%H%M%SZ',
time.gmtime(float(
end.strftime('%s'))))
content += "LOCATION:%s\n" % talk['room']
content += 'END:VEVENT\n'
return content
class TalksGenerator(generators.Generator): class TalksGenerator(generators.Generator):
@ -143,6 +196,8 @@ class TalksGenerator(generators.Generator):
if os.path.isdir(outdir): if os.path.isdir(outdir):
shutil.rmtree(outdir) shutil.rmtree(outdir)
shutil.copytree(self.talks[talkname]['resources'], outdir) shutil.copytree(self.talks[talkname]['resources'], outdir)
with open(os.path.join(self.output_path, 'talks.ics'), 'w') as buf:
buf.write(talks_to_ics())
def add_talks_option_defaults(pelican): def add_talks_option_defaults(pelican):

1
talks/meta.yaml Normal file
View file

@ -0,0 +1 @@
startdate: 2017-06-15