IcalManage package¶
Using the parser¶
The parser is based on the great lib icalendar. for parsing the ICS file, but it only use the parsing part for loading an existing valid ics file.
The function is prototyped like this :
def ical_to_dict(stream)
The stream parameter is stream of an ical file, you can set it with a request stream, like this :
r = requests.get(ical_url, stream=True)
data = ical_to_dict(r)
Or you can simply read an local ical file
If the file content cannot be read or parsed by icalendar, the function will log an error and return False
The ical_to_dict function will return a dict like this :
{
'current_events': list_of_events,
'next_events': list_of_mini_events
}
The current_events contain a list containing dict with full informations about events in progress. The event in progress dict look like this :
And the list_of_mini_events is a list containing basics information about incoming events like this :
{
"end": "2014-11-26T17:30:00",
"name": "ert",
"place": "Salle 301",
"start": "2014-11-26T16:30:00"
}
In the actual version of the api, the parser is directly expose in JSON and returned by the get api function But you can use it in other place by importing it
from icalmanage.icalparser import ical_to_dict
The parser as been tested on google agenda ical file. But because of a bug when retrieving dates from the ical file, the api.icalmanage.helpers file contain a simple function for managing utc offset :
def set_utc(dt):
utc_of = timezone('Europe/Paris')
now = datetime.now()
return dt + utc_of.utcoffset(now)
This function allow you to manually add the utc offset to the returned datetime object. Unfortunately the module don’t has any configuration file (yet), so the timezone is hardcoded (sadly), but configuration is coming soon !
This function is call by default by the ical_to_dict function, which is the main function of the parser.
api.icalmanage.helpers module¶
- api.icalmanage.helpers.attendee_to_login(attendee)¶
Get a list of vCalAddress and return a list of string without domain name in mail address
Parameters: attendee – list of vCalAddress Returns: list of string Return type: list
- api.icalmanage.helpers.format_dt(dt)¶
return a formated string from dt object It will remove utc information
Parameters: dt (datetime) – datetime object to format Returns: the formated string Return type: str
- api.icalmanage.helpers.format_room(room)¶
Format room name for display
Parameters: room (str) – The string representing the room Returns: the formatted string Return type: str
- api.icalmanage.helpers.get_room(events, room)¶
Get all events from events dict where event is in location room
Parameters: - events (dict) – dictionary of events
- room (str) – the name of the room
Returns: a list of events for the given room
Return type: list
- api.icalmanage.helpers.set_utc(dt)¶
This function is used for correct a bug in icalendar datetime from google agenda It will add the utc offset of the localised current datetime to the dt param and return it
Parameters: dt – the datetime to update Returns: dt Return type: datetime
api.icalmanage.icalparser module¶
- api.icalmanage.icalparser.check_event_current(ev, day_end)¶
Check if an event is today and after now
Parameters: - ev – icalendar event object
- day_end – the end of the current day
Returns: True or False for the event
- api.icalmanage.icalparser.check_event_next_day(ev, day_end)¶
Check if an event occur the nex day
Parameters: - ev – icalendar event object
- day_end – the end of the current day
Returns: True or False for the event
- api.icalmanage.icalparser.ev_to_partial_dict(ev)¶
Return a dict for a given event, this dict is for next_events only
Parameters: ev – icalendar event object Returns: a dict containing basic informations about the event
- api.icalmanage.icalparser.ical_to_dict(stream)¶
get all event of the CURRENT day and format them to a dict ready to be encoded in json
Parameters: stream – icalendar file object from get request Returns: a dict containing formated data Return type: dict