X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=parserrules.py;h=4ba71e8a3544d20e9a5502c22d6d059f9bb2a96a;hb=a60d888467b796bdf7ed7cd63c53cef8dcfc779c;hp=cf0fbd66e165e0dad6ef7ebac3ae40fc46112720;hpb=32a3500faead4a17ef28bb398d21fa040cf9ffc6;p=frenchie%2Ficalparse.git diff --git a/parserrules.py b/parserrules.py index cf0fbd6..4ba71e8 100644 --- a/parserrules.py +++ b/parserrules.py @@ -1,10 +1,36 @@ #!/usr/bin/python - -# Rules for tackling facebook and google calendar - I want visibility of the -# organiser... not useful Google! +# +# Copyright (c) 2011 James French +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# This file describes a series of rules which must handle a vobject object and +# return it to the calling script + +# The doc string will be presented to the user when run as verbose, so +# please be polite import vobject +ruleConfig = {} +ruleConfig["defaultTZ"] = "UTC" + def facebookOrganiser(cal): '''Adds organiser details to the body of facebook calendars.''' @@ -13,11 +39,14 @@ def facebookOrganiser(cal): for event in cal.vevent_list: if not event.contents.has_key(u'organizer'): continue - organizer = "Organised by: " + event.organizer.cn_param + " (" - organizer += event.organizer.value.lstrip('MAILTO:') + ")\n\n" - - event.description.value = organizer + event.description.value - + try: + a = event.organizer.cn_paramlist + organizer = "Organised by: " + event.organizer.cn_param + " (" + organizer += event.organizer.value.lstrip('MAILTO:') + ")\n\n" + event.description.value = organizer + event.description.value + except AttributeError: + organizer = "Organized by: " + event.organizer.value + event.description.value = organizer + "\n\n" + event.description.value return cal def whatPrivacy(cal): @@ -25,14 +54,16 @@ def whatPrivacy(cal): for event in cal.vevent_list: if event.contents.has_key(u'class'): - del event.contents[u'class'] # Bit of a hack as class is a reserved word in python + del event.contents[u'class'] event.add('class').value = "PUBLIC" return cal -def dropMSKeys(cal): - '''Drops microsoft keys, good for outlook, just bandwidth when not.''' +def dropAttributes(cal): + '''Removing unwanted metadata''' + if "facebook" in ruleConfig: + if ruleConfig["facebook"] == True: return cal eventBlacklist = [x.lower() for x in [ "X-ALT-DESC", @@ -42,30 +73,98 @@ def dropMSKeys(cal): "X-MS-OLK-ALLOWEXTERNCHECK", "X-MS-OLK-AUTOSTARTCHECK", "X-MS-OLK-CONFTYPE", - "X-MS-OLK-AUTOFILLLOCATION" + "X-MS-OLK-AUTOFILLLOCATION", + "TRANSP", + "SEQUENCE", + "PRIORITY" ]] - vcalBlacklist = [x.lower() for x in [ - "X-CALEND", - "X-CALSTART", - "X-CLIPEND", - "X-CLIPSTART", - "X-MS-OLK-WKHRDAYS", - "X-MS-OLK-WKHREND", - "X-MS-OLK-WKHRSTART", - "X-OWNER", - "X-PRIMARY-CALENDAR", - "X-PUBLISHED-TTL", - "X-WR-CALDESC", - "X-WR-CALNAME", - "X-WR-RELCALID" + mainBlacklist = [x.lower() for x in [ + "X-CLIPSTART", + "X-CALSTART", + "X-OWNER", + "X-MS-OLK-WKHRSTART", + "X-MS-OLK-WKHREND", + "X-WR-RELCALID", + "X-MS-OLK-WKHRDAYS", + "X-MS-OLK-APPTSEQTIME", + "X-CLIPEND", + "X-CALEND", + "VTIMEZONE", + "X-PRIMARY-CALENDAR" ]] for event in cal.vevent_list: for blacklist in eventBlacklist: if event.contents.has_key(blacklist): del event.contents[blacklist] - for blacklist in vcalBlacklist: - if cal.contents.has_key(blacklist): del cal.contents[blacklist] + for blkl in mainBlacklist: + while blkl in cal.contents: del cal.contents[blkl] + + return cal + +def exDate(cal): + '''Replacing multi-value EXDATES with multiple single-value EXDATES''' + if "facebook" in ruleConfig: + if ruleConfig["facebook"] == True: return cal + + from datetime import datetime + from pytz import timezone + + default = timezone(ruleConfig["defaultTZ"]) + + for event in cal.vevent_list: + if not event.contents.has_key(u'exdate'): continue + dates = event.exdate.value + + del event.contents[u'exdate'] + + for date in dates: + if isinstance(date, datetime): + if date.tzinfo is None: date = date.replace(tzinfo = default) + date = date.astimezone(vobject.icalendar.utc) + entry = event.add(u'exdate') + entry.value = [date] + + return cal + +def utcise(cal): + '''Removing local timezones in favour of UTC. If the remote calendar specifies a timezone + then use it, otherwise assume it's in the user-specified or default values''' + + from datetime import datetime + from pytz import timezone + + default = timezone(ruleConfig["defaultTZ"]) + + for event in cal.vevent_list: + dtstart = getattr(event, 'dtstart', None) + dtend = getattr(event, 'dtend', None) + + for i in (dtstart, dtend): + if not i: continue + dt = i.value + if isinstance(dt, datetime): + if dt.tzinfo is None: dt = dt.replace(tzinfo = default) + i.value = dt.astimezone(vobject.icalendar.utc) + + return cal + +def unwantedParams(cal): + '''Removing unwanted parameters''' + + blklist = [ + "LANGUAGE", + "X-VOBJ-ORIGINAL-TZID", + "TZID" + ] + + for event in cal.vevent_list: + for attr in event.contents: + attr = getattr(event, attr) + try: + for i in blklist: + while i in attr.params: del attr.params[i] + except AttributeError: continue return cal