try to make it look like python

This commit is contained in:
XenGi 2018-08-15 00:49:06 +02:00
parent 35fe345288
commit 4a331c18f6
No known key found for this signature in database
GPG key ID: 1A9D657D06B5820E
3 changed files with 24 additions and 19 deletions

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
icalendar

View file

@ -1,14 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
from datetime import datetime, timedelta
from dateutil.parser import parse
from dateutil.rrule import rruleset, rrulestr
import icalendar
import locale
import logging import logging
import locale
from pprint import pprint from pprint import pprint
from dateutil.parser import parse
from datetime import datetime, timedelta
from dateutil.rrule import rruleset, rrulestr
import icalendar
def vevent_to_event(event, rrstart=None): def vevent_to_event(event, rrstart=None):
if rrstart == None: if rrstart == None:
@ -17,6 +18,7 @@ def vevent_to_event(event, rrstart=None):
begin = rrstart begin = rrstart
return { "name": event['SUMMARY'].to_ical(), "url": event['URL'].to_ical(), "begin": begin } return { "name": event['SUMMARY'].to_ical(), "url": event['URL'].to_ical(), "begin": begin }
def parse_single_event(event, start, end): def parse_single_event(event, start, end):
logging.info("Processing single event %s" % event['SUMMARY'].to_ical().decode('utf-8')) logging.info("Processing single event %s" % event['SUMMARY'].to_ical().decode('utf-8'))
dtstart = parse(event['DTSTART'].to_ical()) dtstart = parse(event['DTSTART'].to_ical())
@ -25,6 +27,7 @@ def parse_single_event(event, start, end):
else: else:
return None return None
def parse_recurring_event(event, start, end): def parse_recurring_event(event, start, end):
logging.info("Processing recurring event %s" % event['SUMMARY'].to_ical().decode('utf-8')) logging.info("Processing recurring event %s" % event['SUMMARY'].to_ical().decode('utf-8'))
dtstart = parse(event['DTSTART'].to_ical()) dtstart = parse(event['DTSTART'].to_ical())
@ -71,7 +74,8 @@ def format_events(events):
% (dateStr, event['url'].decode('utf-8'), event['name'].decode('utf-8'))) % (dateStr, event['url'].decode('utf-8'), event['name'].decode('utf-8')))
print('</table>') print('</table>')
def main():
if __name__ == "__main__":
if len(sys.argv) < 3: if len(sys.argv) < 3:
print("Usage: %s calendar max_days max_items" % sys.argv[0]) print("Usage: %s calendar max_days max_items" % sys.argv[0])
sys.exit(-1) sys.exit(-1)
@ -84,5 +88,3 @@ def main():
events=find_events(calendar, datetime.now(), datetime.now() + timedelta(days=max_days), max_items) events=find_events(calendar, datetime.now(), datetime.now() + timedelta(days=max_days), max_items)
format_events(events) format_events(events)
if __name__ == "__main__":
main()

View file

@ -1,25 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from glob import glob from glob import glob
import icalendar
import pytz import pytz
import icalendar
cals = [] cals = []
merged = icalendar.Calendar() merged = icalendar.Calendar()
merged.add('prodid', '-//CCCB Calendar Generator//berlin.ccc.de//') merged.add('prodid', '-//CCCB Calendar Generator//berlin.ccc.de//')
merged.add('version', '2.0') merged.add('version', '2.0')
for icsfilestr in glob("public/*/**/*.ics", recursive=True): for icsfilestr in glob('public/*/**/*.ics', recursive=True):
with open(icsfilestr, 'r') as icsfile: with open(icsfilestr, 'r') as icsfile:
print("Importing", icsfilestr) print('Importing', icsfilestr)
cals.append(icalendar.Calendar.from_ical(icsfile.read())) cals.append(icalendar.Calendar.from_ical(icsfile.read()))
for cal in cals: for cal in cals:
for e in cal.subcomponents: for e in cal.subcomponents:
merged.add_component(e) merged.add_component(e)
outfile="static/all.ics" outfile = 'static/all.ics'
with open(outfile, 'wb') as f: with open(outfile, 'wb') as f:
print(f"writing to {outfile}...") print(f'writing to {outfile}...')
f.write(merged.to_ical()) f.write(merged.to_ical())