From 6a98de04d910217ddf7b1677570013ef9cbf466d Mon Sep 17 00:00:00 2001 From: Dirk Sarpe <dirk.sarpe@idiv.de> Date: Thu, 8 Aug 2019 12:58:16 +0200 Subject: [PATCH] fix linter warnings and errors - refactored script into functions - functions can take arguments (step to be more configurable) - more telling variable names - import order --- event_display.py | 122 +++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/event_display.py b/event_display.py index 16ead72..e270fa1 100644 --- a/event_display.py +++ b/event_display.py @@ -1,54 +1,72 @@ #!/usr/bin/python -# parse ical calendar and insert values into html template - -import requests -import arrow -from ics import Calendar -from jinja2 import Environment, FileSystemLoader -import os - - -url = 'https://portal.idiv.de/ssf/ical/basic.ics?bi=393&ui=1026&pd=171497477d81eb659bd832ffe26c8bfe39d65fc3&v=1' -tz = 'Europe/Berlin' -now = arrow.now(tz) -period = (now.floor('day'), now.replace(weeks=+4)) - - -c = Calendar(requests.get(url).text) -html_list = [] -for e in c.events: - if e.end > period[0] and e.begin < period[1]: - # only display events which have not ended yet - # and start earlier than defined above - duration = e.end - e.begin - # short, full day and multi day events display datetime differently - if duration.days < 1: - event_date = (e.begin.format('DD.MM.YYYY, HH:mm') - + ' – ' - + e.end.format('HH:mm')) - elif duration.days == 1: - event_date = e.begin.format('DD.MM.YYYY') + ', all day' - elif duration.days > 1: - # in ics file the end date is the day after end date at 00:00:00 - corrected_end_date = e.end.replace(days = - 1) - event_date = (e.begin.format('DD.MM.YYYY') - + ' – ' - + corrected_end_date.format('DD.MM.YYYY')) - else: - event_date = '' - # create list of dictionaries for jinja2 template - event = dict(date = event_date, name = e.name, location = e.location) - html_list.append(event) - -# Capture our current directory -this_dir = os.path.dirname(os.path.abspath(__file__)) - -# Create the jinja2 environment. -# Notice the use of trim_blocks, which greatly helps control whitespace. -j2_env = Environment(loader=FileSystemLoader(this_dir), - trim_blocks=True) -output = j2_env.get_template('template.html').render(html_list = html_list) -out_file = open(this_dir + "/website/events.html", "w") -out_file.write(output.encode("utf-8")) -out_file.close() +def main(url, timezone, future_weeks, filename): + """parse ical calendar and insert values into html template. + """ + html_list = ics2list(url, timezone, future_weeks) + calendarlist2jinja(html_list, filename) + +def ics2list(url, timezone, future_weeks): + """parse ical calendar into a list + """ + import requests + import arrow + from ics import Calendar + + now = arrow.now(timezone) + period = (now.floor('day'), now.replace(weeks=+future_weeks)) + + calendar = Calendar(requests.get(url).text) + html_list = [] + for event in calendar.events: + if event.end > period[0] and event.begin < period[1]: + # only display events which have not ended yet + # and start earlier than defined above + duration = event.end - event.begin + # short, full day and multi day events display datetime differently + if duration.days < 1: + event_date = (event.begin.format('DD.MM.YYYY, HH:mm') + + ' – ' + + event.end.format('HH:mm')) + elif duration.days == 1: + event_date = event.begin.format('DD.MM.YYYY') + ', all day' + elif duration.days > 1: + # in ics file the end date is the day after end date at 00:00:00 + corrected_end_date = event.end.replace(days=- 1) + event_date = (event.begin.format('DD.MM.YYYY') + + ' – ' + + corrected_end_date.format('DD.MM.YYYY')) + else: + event_date = '' + # create list of dictionaries for jinja2 template + event = dict(date=event_date, + name=event.name, + location=event.location) + html_list.append(event) + return html_list + +def calendarlist2jinja(html_list, filename): + """write list of events to jinja html template + """ + + import os + from jinja2 import Environment, FileSystemLoader + + # Capture our current directory + this_dir = os.path.dirname(os.path.abspath(__file__)) + + # Create the jinja2 environment. + # Notice the use of trim_blocks, which greatly helps control whitespace. + j2_env = Environment(loader=FileSystemLoader(this_dir), + trim_blocks=True) + output = j2_env.get_template('template.html').render(html_list=html_list) + out_file = open(this_dir + filename, "w") + out_file.write(output.encode("utf-8")) + out_file.close() + + +if __name__ == '__main__': + main(url='https://portal.idiv.de/ssf/ical/basic.ics?bi=393&ui=1026&pd=171497477d81eb659bd832ffe26c8bfe39d65fc3&v=1', + timezone='Europe/Berlin', + future_weeks=4, + filename="/website/events.html") -- GitLab