ical.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. """
  3. ical plugin for Pelican
  4. =======================
  5. This plugin looks for and parses an .ics file if it is defined in a given
  6. page's :calendar: metadata. One calendar can be defined per page.
  7. """
  8. from icalendar import Calendar, Event
  9. from pelican import signals , utils
  10. import pytz
  11. import datetime
  12. import os.path
  13. def init_cal(generator):
  14. # initialisation of the calendar dictionary
  15. # you can add one calendar per page
  16. calDict = {}
  17. generator.context['events'] = calDict
  18. def add_ical(generator, metadata):
  19. # check if a calendar is here
  20. if 'calendar' in metadata.keys():
  21. summ = []
  22. path = metadata['calendar']
  23. if not os.path.isabs(path):
  24. path = os.path.abspath(metadata['calendar'])
  25. cal = Calendar.from_ical(open(path,'rb').read())
  26. for element in cal.walk():
  27. eventdict = {}
  28. if element.name == "VEVENT":
  29. if element.get('summary') != None:
  30. eventdict['summary'] = element.get('summary')
  31. if element.get('description') != None:
  32. eventdict['description'] = element.get('description')
  33. if element.get('url') != None:
  34. eventdict['url'] = element.get('url')
  35. if element.get('dtstart') != None:
  36. eventdict['dtstart'] = element.get('dtstart').dt
  37. if element.get('dtend') != None:
  38. eventdict['dtend'] = element.get('dtend').dt
  39. summ.append(eventdict)
  40. # the id of the calendar is the slugified name of the page
  41. calId = utils.slugify(metadata['title'])
  42. generator.context['events'][calId] = summ
  43. def register():
  44. signals.pages_generator_init.connect(init_cal)
  45. signals.pages_generate_context.connect(add_ical)