Mercurial > hg > gcmultimerge
annotate multimerge.py @ 23:ff47f8088ef9
Make things more OO.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 04 Jul 2016 14:20:56 +0300 |
parents | 793b7997cc2b |
children | 1bce0c6a673c |
rev | line source |
---|---|
1 | 1 #!/usr/bin/python |
3 | 2 # coding=utf-8 |
2 | 3 ### |
4 ### Google Calendar MultiMerge v0.000001 | |
5 ### (C) 2016 Matti 'ccr' Hamalainen <ccr@tnsp.org> | |
6 ### | |
7 ### Python 2.7 <= x < 3 required! Please refer to | |
8 ### README.txt for information on other depencies. | |
9 ### | |
1 | 10 import os |
11 import sys | |
12 import signal | |
13 import re | |
14 import time | |
15 import datetime | |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
16 |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
17 import smtplib |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
18 from email.mime.text import MIMEText |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
19 |
1 | 20 import httplib2 |
21 import ConfigParser | |
20 | 22 |
1 | 23 import oauth2client |
24 from oauth2client import client | |
25 from oauth2client import tools | |
4 | 26 from oauth2client import file |
1 | 27 from googleapiclient import discovery |
28 | |
29 | |
30 ### | |
31 ### Misc. helper functions | |
32 ### | |
33 | |
34 ## Wrapper for print() that does not break when redirecting stdin/out | |
35 ## because of piped output not having a defined encoding. We default | |
36 ## to UTF-8 encoding in output here. | |
37 def gcm_print(smsg): | |
38 gcm_msgbuf.append(smsg.encode("UTF-8")) | |
39 if sys.stdout.encoding != None: | |
40 print(smsg.encode(sys.stdout.encoding)) | |
41 else: | |
42 print(smsg.encode("UTF-8")) | |
43 | |
44 | |
45 ## Fatal errors | |
46 def gcm_fatal(smsg): | |
47 gcm_print(u"ERROR: "+ smsg) | |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
48 if cfg.email_ok and cfg.email: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
49 ## If e-mail is set, send e-mail |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
50 msg = MIMEText(("\n".join(gcm_msgbuf)).encode("UTF-8"), "plain") |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
51 msg.set_charset("UTF-8") |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
52 msg["Subject"] = cfg.email_subject |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
53 msg["From"] = cfg.email_sender |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
54 msg["To"] = ",".join(cfg.email_to) |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
55 try: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
56 smtpH = smtplib.SMTP('localhost') |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
57 smtpH.sendmail(cfg.email_sender, cfg.email_to, msg.as_string()) |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
58 smtpH.quit() |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
59 except: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
60 gcm_print("FATAL: Oh crap, e-mail sending failed.") |
1 | 61 sys.exit(1) |
62 | |
63 | |
64 ## Debug messages | |
65 def gcm_debug(smsg): | |
66 if cfg.debug: | |
67 gcm_print(u"DBG: "+ smsg) | |
68 else: | |
69 gcm_msgbuf.append(u"DBG: "+ smsg.encode("UTF-8")) | |
70 | |
71 | |
72 ## Handle SIGINT signals here | |
73 def gcm_signal_handler(signal, frame): | |
74 gcm_print("\nQuitting due to SIGINT / Ctrl+C!") | |
75 sys.exit(0) | |
76 | |
77 | |
78 def gcm_get_credentials(mcfg): | |
79 store = oauth2client.file.Storage(mcfg.credential_file) | |
80 credentials = store.get() | |
81 if not credentials or credentials.invalid: | |
82 flow = client.flow_from_clientsecrets(mcfg.secret_file, mcfg.scope) | |
83 flow.user_agent = mcfg.app_name | |
84 credentials = tools.run_flow(flow, store, mcfg) | |
85 if not credentials or credentials.invalid: | |
86 gcm_fatal("Failed to authenticate / invalid credentials.") | |
87 return credentials | |
88 | |
89 | |
90 def gcm_dump_events(events): | |
91 for event in events: | |
92 ev_start = event["start"].get("dateTime", event["start"].get("date")) | |
93 ev_end = event["end"].get("dateTime", event["end"].get("date")) | |
94 gcm_print(u"{0:25} - {1:25} : {2}".format(ev_start, ev_end, event["summary"])) | |
95 | |
96 | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
97 |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
98 |
1 | 99 class GCMSettings(dict): |
100 def __init__(self): | |
101 self.m_data = {} | |
102 self.m_saveable = {} | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
103 self.m_validate = {} |
1 | 104 self.m_translate = {} |
105 | |
106 def __getattr__(self, name): | |
107 if name in self.m_data: | |
108 return self.m_data[name] | |
109 else: | |
110 gcm_fatal("GCMSettings.__getattr__(): No such attribute '"+ name +"'.") | |
111 | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
112 def mvalidate(self, name, value): |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
113 if name in self.m_validate and self.m_validate[name]: |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
114 if not self.m_validate[name](value): |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
115 gcm_fatal("GCMSettings.mvalidate(): Invalid value for attribute '{0}': {1}".format(name, value)) |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
116 |
1 | 117 def mtranslate(self, name, value): |
118 if name in self.m_translate and self.m_translate[name]: | |
119 return self.m_translate[name](value) | |
120 else: | |
121 return value | |
122 | |
123 def mdef(self, name, saveable, validate, translate, value): | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
124 self.mvalidate(name, value) |
1 | 125 self.m_saveable[name] = saveable |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
126 self.m_validate[name] = validate |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
127 self.m_translate[name] = translate |
1 | 128 self.m_data[name] = self.mtranslate(name, value) |
129 | |
130 def mset(self, name, value): | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
131 self.mvalidate(name, value) |
1 | 132 if name in self.m_data: |
133 self.m_data[name] = self.mtranslate(name, value) | |
134 else: | |
135 gcm_fatal("GCMSettings.mset(): No such attribute '"+ name +"'.") | |
136 | |
137 def mget(self, name): | |
138 if name in self.m_data: | |
139 return self.m_data[name] | |
140 else: | |
141 return None | |
142 | |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
143 def mread(self, cfgparser, sect): |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
144 for name in self.m_saveable: |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
145 if cfgparser.has_option(sect, name): |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
146 value = cfgparser.get(sect, name) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
147 self.mset(name, value) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
148 gcm_debug("{0} -> '{1}' == {2}".format(name, value, self.mget(name))) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
149 |
23 | 150 def is_str(self, mvalue): |
151 return isinstance(mvalue, basestring) | |
152 | |
153 def is_string(self, mvalue): | |
154 return mvalue == None or self.is_str(mvalue) | |
155 | |
156 def is_log_level(self, mvalue): | |
157 if not self.is_str(mvalue): | |
158 return False | |
159 else: | |
160 return mvalue.upper() in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"] | |
161 | |
162 def trans_log_level(self, mvalue): | |
163 return mvalue.upper() | |
164 | |
165 def is_filename(self, mvalue): | |
166 if not self.is_str(mvalue): | |
167 return False | |
168 else: | |
169 return re.match("^[a-z0-9][a-z0-9\.\_\-]+$", mvalue, flags=re.IGNORECASE) | |
170 | |
171 def trans_bool(self, mvalue): | |
172 if self.is_str(mvalue): | |
173 if re.match("^\s*(true|1|on|yes)\s*$", mvalue, re.IGNORECASE): | |
174 mvalue = True | |
175 elif re.match("^\s*(false|0|off|no)\s*$", mvalue, re.IGNORECASE): | |
176 mvalue = False | |
177 else: | |
178 return None | |
179 return mvalue | |
180 | |
181 def is_bool(self, mvalue): | |
182 mval = self.trans_bool(mvalue) | |
183 if not isinstance(mval, bool): | |
184 gcm_fatal("GCMSettings.is_bool(): Invalid boolean value '{0}', should be true|false|1|0|on|off|yes|no.".format(mvalue)) | |
185 else: | |
186 return True | |
187 | |
188 def trans_list(self, mvalue): | |
189 morig = mvalue | |
190 if self.is_str(mvalue): | |
191 mvalue = re.split("\s*,\s*", mvalue, flags=re.IGNORECASE) | |
192 if not isinstance(mvalue, list): | |
193 gcm_fatal("GCMSettings.trans_list(): Could not parse list '{0}'.".format(mvalue)) | |
194 elif not isinstance(mvalue, list): | |
195 gcm_fatal("GCMSettings.trans_list(): Invalid value '{0}'.".format(mvalue)) | |
196 return mvalue | |
197 | |
198 def is_list(self, mvalue): | |
199 return self.trans_list(mvalue) | |
200 | |
201 def is_email(self, mvalue): | |
202 if not self.is_string(mvalue): | |
203 return False | |
204 else: | |
205 return re.match("^.*?\s+<[a-z0-9]+[a-z0-9\.\+\-]*\@[a-z0-9]+[a-z0-9\.\-]+>\s*$|[a-z0-9]+[a-z0-9\.\+\-]*\@[a-z0-9]+[a-z0-9\.\-]+", mvalue, flags=re.IGNORECASE) | |
206 | |
207 def trans_email_list(self, mvalue): | |
208 if mvalue == None: | |
209 return mvalue | |
210 else: | |
211 return self.trans_list(mvalue.strip()) | |
212 | |
213 def is_email_list(self, mvalue): | |
214 mvalue = self.trans_email_list(mvalue) | |
215 if mvalue != None: | |
216 for email in mvalue: | |
217 if not self.is_email(email): | |
218 gcm_fatal("Invalid e-mail address '{0}' in list {1}.".format(email, ", ".join(mvalue))) | |
219 return True | |
220 | |
1 | 221 |
222 ### | |
223 ### Main program starts | |
224 ### | |
225 gcm_msgbuf = [] | |
226 signal.signal(signal.SIGINT, gcm_signal_handler) | |
227 | |
228 | |
229 ## Settings | |
230 cfg = GCMSettings() | |
231 | |
23 | 232 cfg.mdef("debug", True, cfg.is_bool, cfg.trans_bool, False) |
1 | 233 |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
234 cfg.mdef("email_ok", False, None, None, False) |
23 | 235 cfg.mdef("email", True, cfg.is_bool, cfg.trans_bool, False) |
236 cfg.mdef("email_to", True, cfg.is_email_list, cfg.trans_email_list, None) | |
237 cfg.mdef("email_sender", True, cfg.is_email, None, None) | |
238 cfg.mdef("email_subject", True, cfg.is_string, None, "Google Calendar MultiMerge status") | |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
239 |
23 | 240 cfg.mdef("source_regex", True, cfg.is_string, None, "^R:\s*(.*?)\s*\(\s*(.+?)\s*\)\s*$") |
241 cfg.mdef("source_regmap", False, cfg.is_list, cfg.trans_list, [1, 2]) | |
1 | 242 cfg.mdef("source_regmap_len", False, None, None, len(cfg.source_regmap)) |
243 | |
23 | 244 cfg.mdef("dest_name", True, cfg.is_string, None, u"Raahen kansainvälisyystoiminta") |
245 cfg.mdef("dest_id", True, cfg.is_string, None, None) | |
1 | 246 |
247 cfg.mdef("noauth_local_webserver", False, None, None, True) | |
248 #cfg.mdef("auth_host_name", False, None, None, "localhost") | |
249 #cfg.mdef("auth_host_port", False, None, None, [8080, 8090]) | |
23 | 250 cfg.mdef("logging_level", True, cfg.is_log_level, cfg.trans_log_level, "ERROR") |
1 | 251 |
252 # No need to touch these | |
253 cfg.mdef("app_name", False, None, None, "Google Calendar MultiMerge") | |
254 cfg.mdef("scope", False, None, None, "https://www.googleapis.com/auth/calendar") | |
255 #cfg.mdef("scope", False, None, None, "https://www.googleapis.com/auth/calendar.readonly") | |
23 | 256 cfg.mdef("secret_file", True, cfg.is_filename, None, "client_secret.json") |
257 cfg.mdef("credential_file", True, cfg.is_filename, None, "client_credentials.json") | |
1 | 258 |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
259 |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
260 ## Read, parse and validate configuration file |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
261 if len(sys.argv) > 1: |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
262 gcm_debug("Reading configuration from '{0}'.".format(sys.argv[1])) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
263 try: |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
264 cfgparser = ConfigParser.RawConfigParser() |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
265 cfgparser.read(sys.argv[1]) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
266 except Exception as e: |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
267 gcm_fatal("Failed to read configuration file '{0}': {1}".format(sys.argv[1], str(e))) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
268 |
7
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
269 # Check that the required section exists |
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
270 section = "gcm" |
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
271 if not cfgparser.has_section(section): |
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
272 gcm_fatal("Invalid configuration, missing '{0}' section.".format(section)) |
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
273 |
10
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
274 # Debug setting is a special case, we need to get it |
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
275 # set before everything else, so do it here .. |
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
276 if cfgparser.has_option(section, "debug"): |
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
277 cfg.mset("debug", cfgparser.get(section, "debug")) |
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
278 |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
279 # Parse the settings and validate |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
280 cfg.mread(cfgparser, section) |
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
281 |
8 | 282 |
283 ## Validate settings | |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
284 if cfg.email: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
285 if cfg.email_subject == None or len(cfg.email_subject) == 0: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
286 gcm_fatal("E-mail enabled but email_subject not set.") |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
287 elif cfg.email_sender == None: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
288 gcm_fatal("E-mail enabled but email_sender not set.") |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
289 elif cfg.email_to == None: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
290 gcm_fatal("E-mail enabled but email_to not set.") |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
291 else: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
292 cfg.mset("email_ok", True) |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
293 |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
294 |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
295 if len(cfg.source_regmap) != cfg.source_regmap_len: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
296 gcm_fatal("Setting source_regmap list must be {0} items.".format(cfg.source_regmap_len)) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
297 else: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
298 # Force to integers |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
299 try: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
300 cfg.source_regmap = map(lambda x: int(x), cfg.source_regmap) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
301 except Exception as e: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
302 gcm_fatal("Invalid source_regmap: {0}".format(str(e))) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
303 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
304 |
8 | 305 if not cfg.dest_name and not cfg.dest_id: |
306 gcm_fatal("Target calendar ID or name required, but not set.") | |
307 | |
308 | |
309 if cfg.dest_name: | |
310 cfg.mset("dest_name", cfg.mget("dest_name").strip()) | |
311 | |
312 | |
1 | 313 ## Initialize and authorize API connection |
314 credentials = gcm_get_credentials(cfg) | |
315 http = credentials.authorize(httplib2.Http()) | |
316 service = discovery.build("calendar", "v3", http=http) | |
317 | |
318 | |
319 ## Fetch complete calendar list | |
320 gcm_debug("Fetching available calendars ..") | |
321 calendars = [] | |
19 | 322 cal_token = None |
1 | 323 while True: |
324 # We want everything except deleted and hidden calendars | |
17 | 325 result = service.calendarList().list( |
1 | 326 showHidden=False, |
327 showDeleted=False, | |
19 | 328 pageToken=cal_token |
1 | 329 ).execute() |
330 | |
17 | 331 calendars.extend(result.get("items", [])) |
19 | 332 cal_token = result.get("nextPageToken") |
333 if not cal_token: | |
1 | 334 break |
335 | |
336 if len(calendars) == 0: | |
337 gcm_fatal("No calendars found?") | |
338 | |
339 | |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
340 ## Filter desired SOURCE calendars based on specified regexp |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
341 src_re = re.compile(cfg.source_regex) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
342 src_calendars = [] |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
343 for calendar in calendars: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
344 if "summary" in calendar: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
345 if not cfg.dest_id and cfg.dest_name == calendar["summary"].strip(): |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
346 cfg.mset("dest_id", calendar["id"]) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
347 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
348 mre = src_re.match(calendar["summary"]) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
349 if mre: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
350 calendar["gcm_title"] = mre.group(cfg.source_regmap[0]) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
351 calendar["gcm_id"] = mre.group(cfg.source_regmap[1]) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
352 src_calendars.append(calendar) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
353 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
354 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
355 ## Check if we have target ID |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
356 if not cfg.dest_id: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
357 gcm_fatal(u"Could not find target/destination calendar ID for '"+ cfg.dest_name +"'.") |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
358 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
359 |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
360 ## Now, we fetch and collect events |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
361 gcm_debug(u"Fetching calendar events .. ") |
19 | 362 src_events = [] |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
363 for calendar in src_calendars: |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
364 gcm_debug("- "+calendar["id"]) |
17 | 365 result = service.events().list( |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
366 timeZone="EEST", |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
367 calendarId=calendar["id"], |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
368 singleEvents=True, |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
369 showDeleted=False, |
17 | 370 # orderBy="startTime", |
371 ).execute() | |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
372 |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
373 # Add events, if any, to main list |
17 | 374 events = result.get("items", []) |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
375 if events: |
21
f392d495c5b6
We need to extend a list, not append to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
376 src_events.extend(events) |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
377 if cfg.debug: |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
378 gcm_dump_events(events) |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
379 |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
380 |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
381 ## Get current events |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
382 gcm_debug(u"Fetching current target calendar events {0}".format(cfg.dest_id)) |
17 | 383 result = service.events().list( |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
384 calendarId=cfg.dest_id, |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
385 singleEvents=True, |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
386 showDeleted=True).execute() |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
387 |
22 | 388 dst_events = result.get("items", []) |
389 if dst_events: | |
390 gcm_debug(u"Found {0} event(s).".format(len(dst_events))) | |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
391 else: |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
392 gcm_debug(u"No current events.") |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
393 |
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
394 |