Tabs vs Spaces
[frenchie/icalparse.git] / parserrules.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011 James French <[email protected]>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22
23 # This file describes a series of rules which must handle a vobject object and
24 # return it to the calling script
25
26 # The doc string will be presented to the user when run as verbose, so
27 # please be polite
28
29 import vobject
30
31 ruleConfig = {}
32 ruleConfig["defaultTZ"] = "UTC"
33
34 def facebookOrganiser(cal):
35         '''Adds organiser details to the body of facebook calendars.'''
36
37         if 'prodid' in cal.contents:
38                 if not "Facebook" in cal.prodid.value: return cal
39
40         if 'vevent_list' not in dir(cal):
41                 return cal
42
43         for event in cal.vevent_list:
44                 if 'organizer' not in event.contents: continue
45                 try:
46                         a = event.organizer.cn_paramlist
47                         organizer = "Organised by: " + event.organizer.cn_param + " ("
48                         organizer += event.organizer.value.lstrip('MAILTO:') + ")\n\n"
49                         event.description.value = organizer + event.description.value
50                 except AttributeError:
51                         organizer = "Organized by: " + event.organizer.value
52                         event.description.value = organizer + "\n\n" + event.description.value
53         return cal
54
55 def whatPrivacy(cal):
56         '''Marks events public so google calendar doesn't have a sad about them.'''
57
58         if 'vevent_list' not in dir(cal):
59                 return cal
60
61         for event in cal.vevent_list:
62                 if 'class' in event.contents:
63                         # Bit of a hack as class is a reserved word in python
64                         del event.contents['class']
65                         event.add('class').value = "PUBLIC"
66
67         return cal
68
69 def dropAttributes(cal):
70         '''Removing unwanted metadata'''
71         if "facebook" in ruleConfig:
72                 if ruleConfig["facebook"] == True: return cal
73
74         if 'vevent_list' not in dir(cal):
75                 return cal
76
77         eventBlacklist = [x.lower() for x in [
78                 "X-ALT-DESC",
79                 "X-MICROSOFT-CDO-BUSYSTATUS",
80                 "X-MICROSOFT-CDO-IMPORTANCE",
81                 "X-MICROSOFT-DISALLOW-COUNTER",
82                 "X-MS-OLK-ALLOWEXTERNCHECK",
83                 "X-MS-OLK-AUTOSTARTCHECK",
84                 "X-MS-OLK-CONFTYPE",
85                 "X-MS-OLK-AUTOFILLLOCATION",
86                 "TRANSP",
87                 "SEQUENCE",
88                 "PRIORITY"
89         ]]
90
91         mainBlacklist = [x.lower() for x in [
92                 "X-CLIPSTART",
93                 "X-CALSTART",
94                 "X-OWNER",
95                 "X-MS-OLK-WKHRSTART",
96                 "X-MS-OLK-WKHREND",
97                 "X-WR-RELCALID",
98                 "X-MS-OLK-WKHRDAYS",
99                 "X-MS-OLK-APPTSEQTIME",
100                 "X-CLIPEND",
101                 "X-CALEND",
102                 "VTIMEZONE",
103                 "X-PRIMARY-CALENDAR"
104         ]]
105
106         for event in cal.vevent_list:
107                 for blacklist in eventBlacklist:
108                         if blacklist in event.contents: del event.contents[blacklist]
109
110         for blkl in mainBlacklist:
111                 while blkl in cal.contents: del cal.contents[blkl]
112
113         return cal
114
115 def exDate(cal):
116         '''Replacing multi-value EXDATES with multiple single-value EXDATES'''
117         if "facebook" in ruleConfig:
118                 if ruleConfig["facebook"] == True: return cal
119
120         if 'vevent_list' not in dir(cal):
121                 return cal
122
123         from datetime import datetime
124         from pytz import timezone
125
126         default = timezone(ruleConfig["defaultTZ"])
127
128         for event in cal.vevent_list:
129                 if 'exdate' not in event.contents: continue
130                 dates = event.exdate.value
131
132                 del event.contents['exdate']
133
134                 for date in dates:
135                         if isinstance(date, datetime):
136                                 if date.tzinfo is None: date = date.replace(tzinfo = default)
137                                 date = date.astimezone(vobject.icalendar.utc)
138                         entry = event.add('exdate')
139                         entry.value = [date]
140
141         return cal
142
143 def utcise(cal):
144         '''Removing local timezones in favour of UTC. If the remote calendar specifies a timezone
145         then use it, otherwise assume it's in the user-specified or default values'''
146
147         if 'vevent_list' not in dir(cal):
148                 return cal
149
150         from datetime import datetime
151         from pytz import timezone
152
153         default = timezone(ruleConfig["defaultTZ"])
154
155         for event in cal.vevent_list:
156                 dtstart = getattr(event, 'dtstart', None)
157                 dtend = getattr(event, 'dtend', None)
158
159                 for i in (dtstart, dtend):
160                         if not i: continue
161                         dt = i.value
162                         if isinstance(dt, datetime):
163                                 if dt.tzinfo is None: dt = dt.replace(tzinfo = default)
164                                 i.value = dt.astimezone(vobject.icalendar.utc)
165
166         return cal
167
168 def unwantedParams(cal):
169         '''Removing unwanted parameters'''
170
171         blklist = [
172                 "LANGUAGE",
173                 "X-VOBJ-ORIGINAL-TZID",
174                 "TZID"
175         ]
176
177         if 'vevent_list' not in dir(cal):
178                 return cal
179
180         for event in cal.vevent_list:
181                 for attr in event.contents:
182                         attr = getattr(event, attr)
183                         try:
184                                 for i in blklist:
185                                         while i in attr.params: del attr.params[i]
186                         except AttributeError: continue
187
188         return cal

UCC git Repository :: git.ucc.asn.au