Mercurial > hg > gcmultimerge
annotate multimerge.py @ 99:4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Oct 2016 13:07:24 +0300 |
parents | fe3bfabf0b5f |
children | b4058d935560 |
rev | line source |
---|---|
1 | 1 #!/usr/bin/python |
3 | 2 # coding=utf-8 |
2 | 3 ### |
36 | 4 ### Google Calendar MultiMerge |
80
4bed40e35fae
Fix copyright and add information about license.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
5 ### Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
4bed40e35fae
Fix copyright and add information about license.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
6 ### (C) Copyright 2016 Tecnic Software productions (TNSP) |
4bed40e35fae
Fix copyright and add information about license.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
7 ### |
4bed40e35fae
Fix copyright and add information about license.
Matti Hamalainen <ccr@tnsp.org>
parents:
78
diff
changeset
|
8 ### For license information, see file "COPYING". |
2 | 9 ### |
10 ### Python 2.7 <= x < 3 required! Please refer to | |
11 ### README.txt for information on other depencies. | |
12 ### | |
1 | 13 import os |
14 import sys | |
15 import signal | |
16 import re | |
45
035be8a9e982
Force reading of configuration in Unicode UTF-8.
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
17 import codecs |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
18 import math |
73
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
19 import time |
29
50711871fd1e
Comment out currently unnecessary imports.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
20 #import datetime |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
21 |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
22 import smtplib |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
23 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
|
24 |
1 | 25 import httplib2 |
26 import ConfigParser | |
20 | 27 |
1 | 28 import oauth2client |
29 from oauth2client import client | |
30 from oauth2client import tools | |
4 | 31 from oauth2client import file |
1 | 32 from googleapiclient import discovery |
33 | |
34 | |
35 ### | |
36 ### Misc. helper functions | |
37 ### | |
66
b08d159e35c7
Sanitize gcm_no_compare_fields[]
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
38 gcm_no_compare_fields = [ |
b08d159e35c7
Sanitize gcm_no_compare_fields[]
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
39 "id", "iCalUID", "etag", "sequence", "gcm_cal_id", |
72
1ab40033bb87
Add 'creator' to event equality comparision excluded fields.
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
40 "created", "updated", "htmlLink", "organizer", "creator", |
66
b08d159e35c7
Sanitize gcm_no_compare_fields[]
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
41 ] |
b08d159e35c7
Sanitize gcm_no_compare_fields[]
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
42 |
76
a63cc3633adb
Put log levels array into its own global variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
43 gcm_log_levels = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"] |
a63cc3633adb
Put log levels array into its own global variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
44 |
1 | 45 |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
46 def gcm_get_log_level(): |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
47 return gcm_log_levels.index(cfg.logging_level) |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
48 |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
49 |
1 | 50 ## Wrapper for print() that does not break when redirecting stdin/out |
51 ## because of piped output not having a defined encoding. We default | |
52 ## to UTF-8 encoding in output here. | |
53 def gcm_print(smsg): | |
47 | 54 gcm_msgbuf.append(smsg) |
1 | 55 if sys.stdout.encoding != None: |
56 print(smsg.encode(sys.stdout.encoding)) | |
57 else: | |
58 print(smsg.encode("UTF-8")) | |
59 | |
60 | |
61 ## Fatal errors | |
62 def gcm_fatal(smsg): | |
63 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
|
64 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
|
65 ## 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 try: |
95
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
72 server = smtplib.SMTP(cfg.email_server) |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
73 if gcm_check_debug(4): |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
74 server.set_debuglevel(10) |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
75 |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
76 if cfg.email_use_tls: |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
77 server.starttls() |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
78 |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
79 server.login(cfg.email_srv_user, cfg.email_srv_password) |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
80 server.sendmail(cfg.email_sender, cfg.email_to, msg.as_string()) |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
81 server.quit() |
70
d5e3ca4b609d
Improve error handling when e-mail sending fails.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
82 except Exception as e: |
d5e3ca4b609d
Improve error handling when e-mail sending fails.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
83 gcm_print(u"FATAL: Oh crap, e-mail sending failed: {0}".format(str(e))) |
1 | 84 sys.exit(1) |
85 | |
86 | |
87 ## Debug messages | |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
88 def gcm_check_debug(level): |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
89 return cfg.debug and gcm_get_log_level() >= level |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
90 |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
91 def gcm_debug(level, smsg): |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
92 if gcm_check_debug(level): |
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
93 gcm_print(u"DBG: {0}".format(smsg)) |
1 | 94 else: |
47 | 95 gcm_msgbuf.append(u"DBG: {0}".format(smsg)) |
1 | 96 |
97 | |
24 | 98 ## Handler for SIGINT signals |
1 | 99 def gcm_signal_handler(signal, frame): |
54 | 100 gcm_print(u"\nQuitting due to SIGINT / Ctrl+C!") |
1 | 101 sys.exit(0) |
102 | |
103 | |
24 | 104 ## Function for handling Google API credentials |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
105 def gcm_get_credentials(mcfg, credential_file, secret_file): |
42 | 106 try: |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
107 store = oauth2client.file.Storage(credential_file) |
42 | 108 except Exception as e: |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
109 gcm_fatal(u"Failed to read credential file:\n{0}\n\nERROR: {1}\n".format(credential_file, str(e))) |
42 | 110 |
1 | 111 credentials = store.get() |
112 if not credentials or credentials.invalid: | |
42 | 113 try: |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
114 flow = client.flow_from_clientsecrets(secret_file, mcfg.scope) |
42 | 115 except Exception as e: |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
116 gcm_fatal(u"Failed to fetch client secret:\n{0}\n\nERROR: {1}\n".format(secret_file, str(e))) |
42 | 117 |
1 | 118 flow.user_agent = mcfg.app_name |
119 credentials = tools.run_flow(flow, store, mcfg) | |
120 if not credentials or credentials.invalid: | |
65 | 121 gcm_fatal(u"Failed to authenticate / invalid credentials.") |
1 | 122 return credentials |
123 | |
124 | |
125 def gcm_dump_events(events): | |
126 for event in events: | |
127 ev_start = event["start"].get("dateTime", event["start"].get("date")) | |
128 ev_end = event["end"].get("dateTime", event["end"].get("date")) | |
129 gcm_print(u"{0:25} - {1:25} : {2}".format(ev_start, ev_end, event["summary"])) | |
130 | |
56 | 131 |
132 ## Generate gcm IDs for given list of events | |
63 | 133 def gcm_generate_ids(events, calendar_id, sep): |
26
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
134 if not events: |
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
135 return events |
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
136 |
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
137 for ev in events: |
28 | 138 ev["gcm_cal_id"] = calendar_id |
63 | 139 ev["gcm_id"] = calendar_id + sep + ev["iCalUID"] |
26
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
140 |
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
141 return events |
1267d61f6224
Add function for generating unique internal IDs.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
142 |
1 | 143 |
53
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
144 ## Find event by its gcm_id from given list or return None if not found |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
145 def gcm_get_event_by_gcm_id(list, id): |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
146 for event in list: |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
147 if event["gcm_id"] == id: |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
148 return event |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
149 return None |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
150 |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
151 |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
152 |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
153 def gcm_compare_events(ev1, ev2): |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
154 for field in ev1: |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
155 if not field in gcm_no_compare_fields and ev1[field] != ev2[field]: |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
156 return False |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
157 return True |
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
158 |
63 | 159 |
67 | 160 ### |
161 ### Class for parsing and manipulating RGB colors | |
162 ### | |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
163 class GCMColor(): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
164 def __init__(self, src = None): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
165 if src == None: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
166 self.r = self.g = self.b = 0 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
167 elif isinstance(src, basestring): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
168 if len(src) == 6: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
169 self.r = int(src[0:2], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
170 self.g = int(src[2:4], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
171 self.b = int(src[4:6], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
172 elif len(src) == 7 and src[0] == "#": |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
173 self.r = int(src[1:3], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
174 self.g = int(src[3:5], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
175 self.b = int(src[6:7], 16) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
176 else: |
65 | 177 gcm_fatal(u"Expected hex-triplet string for GCMColor() initializer: {0}".format(src)) |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
178 elif isinstance(src, GCMColor): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
179 self.r = src.r |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
180 self.g = src.g |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
181 self.b = src.b |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
182 else: |
65 | 183 gcm_fatal(u"Invalid initializer for GCMColor() object.") |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
184 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
185 def delta(self, other): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
186 ctmp = GCMColor() |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
187 ctmp.r = other.r - self.r |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
188 ctmp.g = other.g - self.g |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
189 ctmp.b = other.b - self.b |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
190 return ctmp |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
191 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
192 def dist(self, other): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
193 ctmp = self.delta(other) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
194 return math.sqrt(ctmp.r * ctmp.r + ctmp.g * ctmp.g + ctmp.b * ctmp.b) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
195 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
196 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
197 def gcm_find_nearest_color(colors, cfind, maxdist): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
198 c_fg = GCMColor(cfind["foreground"]) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
199 c_bg = GCMColor(cfind["background"]) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
200 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
201 bdist_fg = 99999999999 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
202 bdist_bg = 99999999999 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
203 best_fit = None |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
204 for id, col in colors.iteritems(): |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
205 dist_fg = GCMColor(col["foreground"]).dist(c_fg) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
206 dist_bg = GCMColor(col["background"]).dist(c_bg) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
207 if dist_fg <= bdist_fg and dist_bg <= bdist_bg: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
208 best_fit = id |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
209 bdist_fg = dist_fg |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
210 bdist_bg = dist_bg |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
211 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
212 if bdist_fg <= maxdist and bdist_bg <= maxdist: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
213 return best_fit |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
214 else: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
215 return None |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
216 |
53
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
217 |
24 | 218 ## |
219 ## Class for handling configuration / settings | |
220 ## | |
1 | 221 class GCMSettings(dict): |
222 def __init__(self): | |
223 self.m_data = {} | |
92
a348dfa6b132
Rename a variable/class member.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
224 self.m_settable = {} |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
225 self.m_validate = {} |
1 | 226 self.m_translate = {} |
227 | |
228 def __getattr__(self, name): | |
229 if name in self.m_data: | |
230 return self.m_data[name] | |
231 else: | |
65 | 232 gcm_fatal(u"GCMSettings.__getattr__(): No such attribute '"+ name +"'.") |
1 | 233 |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
234 def mvalidate(self, name, value): |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
235 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
|
236 if not self.m_validate[name](value): |
65 | 237 gcm_fatal(u"GCMSettings.mvalidate(): Invalid value for attribute '{0}': {1}".format(name, value)) |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
238 |
1 | 239 def mtranslate(self, name, value): |
240 if name in self.m_translate and self.m_translate[name]: | |
241 return self.m_translate[name](value) | |
242 else: | |
243 return value | |
244 | |
92
a348dfa6b132
Rename a variable/class member.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
245 def mdef(self, name, settable, validate, translate, value): |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
246 self.mvalidate(name, value) |
92
a348dfa6b132
Rename a variable/class member.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
247 self.m_settable[name] = settable |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
248 self.m_validate[name] = validate |
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
249 self.m_translate[name] = translate |
1 | 250 self.m_data[name] = self.mtranslate(name, value) |
251 | |
252 def mset(self, name, value): | |
5
9d4152f32223
Add some code for settings validation.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
253 self.mvalidate(name, value) |
1 | 254 if name in self.m_data: |
255 self.m_data[name] = self.mtranslate(name, value) | |
256 else: | |
43 | 257 gcm_fatal(u"GCMSettings.mset(): No such attribute '"+ name +"'.") |
1 | 258 |
259 def mget(self, name): | |
260 if name in self.m_data: | |
261 return self.m_data[name] | |
262 else: | |
263 return None | |
264 | |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
265 def mread(self, cfgparser, sect): |
92
a348dfa6b132
Rename a variable/class member.
Matti Hamalainen <ccr@tnsp.org>
parents:
89
diff
changeset
|
266 for name in self.m_settable: |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
267 if cfgparser.has_option(sect, name): |
46
51c87abe5a1e
This Unicode conversion here is probably redundant.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
268 value = cfgparser.get(sect, name) |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
269 self.mset(name, value) |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
270 gcm_debug(4, u"{0} -> '{1}' == {2}".format(name, value, self.mget(name))) |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
271 |
23 | 272 def is_str(self, mvalue): |
273 return isinstance(mvalue, basestring) | |
274 | |
275 def is_string(self, mvalue): | |
276 return mvalue == None or self.is_str(mvalue) | |
277 | |
278 def is_log_level(self, mvalue): | |
279 if not self.is_str(mvalue): | |
280 return False | |
281 else: | |
76
a63cc3633adb
Put log levels array into its own global variable.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
282 return mvalue.upper() in gcm_log_levels |
23 | 283 |
284 def trans_log_level(self, mvalue): | |
285 return mvalue.upper() | |
286 | |
287 def is_filename(self, mvalue): | |
288 if not self.is_str(mvalue): | |
289 return False | |
290 else: | |
291 return re.match("^[a-z0-9][a-z0-9\.\_\-]+$", mvalue, flags=re.IGNORECASE) | |
292 | |
293 def trans_bool(self, mvalue): | |
294 if self.is_str(mvalue): | |
295 if re.match("^\s*(true|1|on|yes)\s*$", mvalue, re.IGNORECASE): | |
296 mvalue = True | |
297 elif re.match("^\s*(false|0|off|no)\s*$", mvalue, re.IGNORECASE): | |
298 mvalue = False | |
299 else: | |
300 return None | |
301 return mvalue | |
302 | |
303 def is_bool(self, mvalue): | |
304 mval = self.trans_bool(mvalue) | |
305 if not isinstance(mval, bool): | |
65 | 306 gcm_fatal(u"GCMSettings.is_bool(): Invalid boolean value '{0}', should be true|false|1|0|on|off|yes|no.".format(mvalue)) |
23 | 307 else: |
308 return True | |
309 | |
310 def trans_list(self, mvalue): | |
311 morig = mvalue | |
312 if self.is_str(mvalue): | |
313 mvalue = re.split("\s*,\s*", mvalue, flags=re.IGNORECASE) | |
314 if not isinstance(mvalue, list): | |
65 | 315 gcm_fatal(u"GCMSettings.trans_list(): Could not parse list '{0}'.".format(mvalue)) |
23 | 316 elif not isinstance(mvalue, list): |
65 | 317 gcm_fatal(u"GCMSettings.trans_list(): Invalid value '{0}'.".format(mvalue)) |
23 | 318 return mvalue |
319 | |
320 def is_list(self, mvalue): | |
321 return self.trans_list(mvalue) | |
322 | |
323 def is_email(self, mvalue): | |
324 if not self.is_string(mvalue): | |
325 return False | |
326 else: | |
327 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) | |
328 | |
329 def trans_email_list(self, mvalue): | |
330 if mvalue == None: | |
331 return mvalue | |
332 else: | |
333 return self.trans_list(mvalue.strip()) | |
334 | |
335 def is_email_list(self, mvalue): | |
336 mvalue = self.trans_email_list(mvalue) | |
337 if mvalue != None: | |
338 for email in mvalue: | |
339 if not self.is_email(email): | |
65 | 340 gcm_fatal(u"Invalid e-mail address '{0}' in list {1}.".format(email, ", ".join(mvalue))) |
23 | 341 return True |
342 | |
1 | 343 |
344 ### | |
345 ### Main program starts | |
346 ### | |
347 gcm_msgbuf = [] | |
348 signal.signal(signal.SIGINT, gcm_signal_handler) | |
349 | |
87
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
350 gcm_bench_start = time.time() |
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
351 |
1 | 352 |
353 ## Settings | |
354 cfg = GCMSettings() | |
355 | |
23 | 356 cfg.mdef("debug", True, cfg.is_bool, cfg.trans_bool, False) |
1 | 357 |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
358 cfg.mdef("email_ok", False, None, None, False) |
23 | 359 cfg.mdef("email", True, cfg.is_bool, cfg.trans_bool, False) |
360 cfg.mdef("email_to", True, cfg.is_email_list, cfg.trans_email_list, None) | |
361 cfg.mdef("email_sender", True, cfg.is_email, None, None) | |
49 | 362 cfg.mdef("email_subject", True, cfg.is_string, None, u"Google Calendar MultiMerge status") |
95
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
363 |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
364 cfg.mdef("email_use_tls", True, cfg.is_bool, cfg.trans_bool, False) |
71
41881ba09ba8
Allow setting SMTP server .. needs more work tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
70
diff
changeset
|
365 cfg.mdef("email_server", True, cfg.is_string, None, None) |
95
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
366 cfg.mdef("email_srv_user", True, cfg.is_string, None, None) |
b5cc76f18256
Fix e-mail sending and implement basic TLS support for outgoing SMTP.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
367 cfg.mdef("email_srv_password", True, cfg.is_string, None, None) |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
368 |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
369 cfg.mdef("src_regex", True, cfg.is_string, None, u"^R:\s*(.*?)\s*\(\s*(.+?)\s*\)\s*$") |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
370 cfg.mdef("src_regmap", False, cfg.is_list, cfg.trans_list, [1, 2]) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
371 cfg.mdef("src_regmap_len", False, None, None, len(cfg.src_regmap)) |
1 | 372 |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
373 cfg.mdef("dst_name", True, cfg.is_string, None, None) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
374 cfg.mdef("dst_regex", True, cfg.is_string, None, None) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
375 cfg.mdef("dst_id", True, cfg.is_string, None, None) |
1 | 376 |
377 cfg.mdef("noauth_local_webserver", False, None, None, True) | |
378 #cfg.mdef("auth_host_name", False, None, None, "localhost") | |
379 #cfg.mdef("auth_host_port", False, None, None, [8080, 8090]) | |
23 | 380 cfg.mdef("logging_level", True, cfg.is_log_level, cfg.trans_log_level, "ERROR") |
1 | 381 |
382 # No need to touch these | |
383 cfg.mdef("app_name", False, None, None, "Google Calendar MultiMerge") | |
384 cfg.mdef("scope", False, None, None, "https://www.googleapis.com/auth/calendar") | |
385 #cfg.mdef("scope", False, None, None, "https://www.googleapis.com/auth/calendar.readonly") | |
23 | 386 cfg.mdef("secret_file", True, cfg.is_filename, None, "client_secret.json") |
387 cfg.mdef("credential_file", True, cfg.is_filename, None, "client_credentials.json") | |
1 | 388 |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
389 |
89 | 390 ## Check arguments |
391 if len(sys.argv) <= 1: | |
392 gcm_fatal(u"No configuration file specified.\nUsage: {0} <configfile>".format(sys.argv[0])) | |
393 | |
394 | |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
395 ## Read, parse and validate configuration file |
89 | 396 gcm_debug(3, u"Reading configuration from '{0}'.".format(sys.argv[1])) |
397 try: | |
398 cfgparser = ConfigParser.RawConfigParser() | |
399 cfgparser.readfp(codecs.open(sys.argv[1], "r", "UTF-8")) | |
400 except Exception as e: | |
401 gcm_fatal(u"Failed to read configuration file '{0}': {1}".format(sys.argv[1], str(e))) | |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
402 |
89 | 403 # Check that the required section exists |
404 section = "gcm" | |
405 if not cfgparser.has_section(section): | |
406 gcm_fatal(u"Invalid configuration, missing '{0}' section.".format(section)) | |
7
f2ecfb3e04ee
Check that the required section exists in configuration.
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
407 |
89 | 408 # Debug setting is a special case, we need to get it |
409 # set before everything else, so do it here .. | |
410 if cfgparser.has_option(section, "debug"): | |
411 cfg.mset("debug", cfgparser.get(section, "debug")) | |
10
b237b96602ad
We need to handle "debug" setting before other settings, so we need a
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
412 |
89 | 413 # Parse the settings and validate |
414 cfg.mread(cfgparser, section) | |
6
ee6bf617f839
Implement configuration file reading.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
415 |
8 | 416 |
417 ## Validate settings | |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
418 if cfg.email: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
419 if cfg.email_subject == None or len(cfg.email_subject) == 0: |
65 | 420 gcm_fatal(u"E-mail enabled but email_subject not set.") |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
421 elif cfg.email_sender == None: |
65 | 422 gcm_fatal(u"E-mail enabled but email_sender not set.") |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
423 elif cfg.email_to == None: |
65 | 424 gcm_fatal(u"E-mail enabled but email_to not set.") |
14
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
425 else: |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
426 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
|
427 |
8262efacf3fb
Initial implementation of sending e-mail in fatal error cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
428 |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
429 if len(cfg.src_regmap) != cfg.src_regmap_len: |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
430 gcm_fatal(u"Setting src_regmap list must be {0} items.".format(cfg.src_regmap_len)) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
431 else: |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
432 # Force to integers |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
433 try: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
434 cfg.src_regmap = map(lambda x: int(x), cfg.src_regmap) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
435 except Exception as e: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
436 gcm_fatal(u"Invalid src_regmap: {0}".format(str(e))) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
437 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
438 |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
439 if not cfg.dst_regex and not cfg.dst_id: |
65 | 440 gcm_fatal(u"Target calendar ID or name required, but not set.") |
8 | 441 |
442 | |
1 | 443 ## Initialize and authorize API connection |
99
4b84bb5bb8b5
Make credential_file and secret_file arguments to gcm_get_credentials().
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
444 credentials = gcm_get_credentials(cfg, cfg.credential_file, cfg.secret_file) |
1 | 445 http = credentials.authorize(httplib2.Http()) |
446 service = discovery.build("calendar", "v3", http=http) | |
447 | |
448 | |
449 ## Fetch complete calendar list | |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
450 gcm_debug(3, u"Fetching available calendars ..") |
1 | 451 calendars = [] |
19 | 452 cal_token = None |
1 | 453 while True: |
454 # We want everything except deleted and hidden calendars | |
17 | 455 result = service.calendarList().list( |
1 | 456 showHidden=False, |
457 showDeleted=False, | |
19 | 458 pageToken=cal_token |
1 | 459 ).execute() |
460 | |
17 | 461 calendars.extend(result.get("items", [])) |
19 | 462 cal_token = result.get("nextPageToken") |
463 if not cal_token: | |
1 | 464 break |
465 | |
466 if len(calendars) == 0: | |
65 | 467 gcm_fatal(u"No calendars found?") |
1 | 468 |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
469 gcm_debug(3, u"{0} calendars total found.".format(len(calendars))) |
50
f8618bae162a
Improve debug messages and comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
470 |
1 | 471 |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
472 ## Filter desired SOURCE calendars based on specified regexp |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
473 src_re = re.compile(cfg.src_regex, re.UNICODE) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
474 dst_re = re.compile(cfg.dst_regex, re.UNICODE) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
475 src_calendars = [] |
60
37705c4a35e1
Show target calendar information in debug mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
476 dst_calendar = None |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
477 for calendar in calendars: |
51
54644b29a9a3
Match also summaryOverride attribute against the source calendar regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
50
diff
changeset
|
478 if u"summary" in calendar: |
50
f8618bae162a
Improve debug messages and comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
479 # Find destination calendar ID if not set |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
480 if not cfg.dst_id and dst_re.match(calendar["summary"]): |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
481 cfg.mset("dst_id", calendar["id"]) |
60
37705c4a35e1
Show target calendar information in debug mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
482 dst_calendar = calendar |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
483 elif cfg.dst_id and calendar["id"] == cfg.dst_id: |
86
d9e799a27ceb
Make cfg.dest_id actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
484 dst_calendar = calendar |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
485 |
51
54644b29a9a3
Match also summaryOverride attribute against the source calendar regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
50
diff
changeset
|
486 # If summary or summaryOverride match the regexp, add calendar |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
487 mre = src_re.match(calendar["summary"]) |
52 | 488 if not mre and u"summaryOverride" in calendar: |
489 mre = src_re.match(calendar[u"summaryOverride"]) | |
490 | |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
491 if mre: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
492 calendar["gcm_title"] = mre.group(cfg.src_regmap[0]) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
493 calendar["gcm_id"] = mre.group(cfg.src_regmap[1]) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
494 src_calendars.append(calendar) |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
495 |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
496 gcm_debug(3, u"{0} source calendars found.".format(len(src_calendars))) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
497 |
50
f8618bae162a
Improve debug messages and comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
498 |
f8618bae162a
Improve debug messages and comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
499 ## Check if we have destination calendar ID |
60
37705c4a35e1
Show target calendar information in debug mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
500 if not dst_calendar: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
501 gcm_fatal(u"Could not find target/destination calendar ID for '"+ cfg.dst_name +"'.") |
60
37705c4a35e1
Show target calendar information in debug mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
502 else: |
93 | 503 gcm_debug(3, u"Target calendar '{0}' [ ID: {1} ]".format(dst_calendar["summary"], dst_calendar["id"])) |
9
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
504 |
01c933dba120
Filter source calendars based on regexp.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
505 |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
506 ## Fetch colors |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
507 try: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
508 colors = service.colors().get().execute() |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
509 except Exception as e: |
65 | 510 gcm_fatal(u"Failed to fetch calendar color settings:\n\n{0}".format(str(e))) |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
511 |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
512 |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
513 ## Now, we fetch and collect events |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
514 gcm_debug(3, u"Fetching calendar events .. ") |
19 | 515 src_events = [] |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
516 for calendar in src_calendars: |
97 | 517 gcm_debug(4, u"- {0} ({1})".format(calendar["id"], calendar["summary"])) |
68 | 518 try: |
519 result = service.events().list( | |
520 timeZone="EEST", | |
521 calendarId=calendar["id"], | |
522 singleEvents=True, | |
523 showDeleted=False, | |
524 # orderBy="startTime", | |
525 ).execute() | |
526 except Exception as e: | |
527 gcm_fatal(u"Failed to fetch calendar events for {0}:\n\n{1}\n\nERROR: {2}\n".format(calendar["id"], calendar, str(e))) | |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
528 |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
529 c_found = None |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
530 if "colorId" in calendar and calendar["colorId"] in colors["calendar"]: |
97 | 531 gcm_debug(4, u" Calendar color: {0}".format(colors["calendar"][calendar["colorId"]])) |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
532 c_found = gcm_find_nearest_color(colors["event"], colors["calendar"][calendar["colorId"]], 100) |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
533 if c_found: |
97 | 534 gcm_debug(4, u" Found nearest event color ID: {0}, {1}".format(c_found, colors["event"][c_found])) |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
535 else: |
97 | 536 gcm_debug(4, u" No matching event color found!") |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
537 |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
538 # Add events, if any, to main list |
63 | 539 events = gcm_generate_ids(result.get("items", []), calendar["id"], "___") |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
540 if events: |
31
6becdaa5c45f
Set event colors (just sequentially now) based on which source calendar they belong to.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
541 for event in events: |
62
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
542 if c_found != None: |
4891ed8d77d5
Implement nearest matching color search via cubic distance, and use it for
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
543 event["colorId"] = c_found |
59
a8941896c21c
Swap order of source name and event title in target events.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
544 event["summary"] = u"[{1}] {0}".format(event["summary"], calendar["gcm_id"]) |
21
f392d495c5b6
We need to extend a list, not append to it.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
545 src_events.extend(events) |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
546 if gcm_check_debug(4): |
11
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
547 gcm_dump_events(events) |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
548 |
fcdee7c04ed8
Implement fetching of source events.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
549 |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
550 ## Get current events |
84
16edb168daa1
No need to display target calendar ID twice.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
551 gcm_debug(3, u"Fetching current target calendar events.") |
17 | 552 result = service.events().list( |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
553 calendarId=cfg.dst_id, |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
554 singleEvents=True, |
32 | 555 showDeleted=True, |
556 ).execute() | |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
557 |
63 | 558 dst_events = gcm_generate_ids(result.get("items", []), "", "") |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
559 gcm_debug(3, u"Found {0} event(s).".format(len(dst_events))) |
58 | 560 |
13
dd240a7ad913
Fetch current events in destination calendar.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
561 |
27 | 562 ## Start merging events .. |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
563 gcm_debug(3, u"Re-merging events to target calendar ..") |
55 | 564 dst_ids = frozenset(map(lambda x: x["gcm_id"], dst_events)) |
565 src_ids = frozenset(map(lambda x: x["gcm_id"], src_events)) | |
27 | 566 |
81
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
567 evn_new = evn_updated = evn_unchanged = 0 |
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
568 |
27 | 569 for event in src_events: |
570 # Does the event exist already in the target? | |
55 | 571 if event["gcm_id"] in dst_ids: |
39
693db3f8cbe5
Begin importing event comparision stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
572 # Check if event NEEDS updating .. aka compare data |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
573 gcm_debug(4, u"Event {0} : {1} exists, checking ..".format(event["id"], event["gcm_id"])) |
53
ea62e0ed05ae
Initial implementation of event comparision amd generally more clever about
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
574 d_event = gcm_get_event_by_gcm_id(dst_events, event["gcm_id"]) |
55 | 575 if not gcm_compare_events(event, d_event): |
57 | 576 # Seems we need to update |
81
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
577 evn_updated += 1 |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
578 gcm_debug(4, u"Updating event {0} : {1}..".format(event["id"], event["gcm_id"])) |
39
693db3f8cbe5
Begin importing event comparision stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
579 try: |
55 | 580 event.pop("sequence", None) |
63 | 581 event.pop("id", None) |
582 event["iCalUID"] = event["gcm_id"] | |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
583 new_event = service.events().update(calendarId=cfg.dst_id, eventId=d_event["id"], body=event).execute() |
39
693db3f8cbe5
Begin importing event comparision stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
584 except Exception as e: |
65 | 585 gcm_fatal(u"Failed to update event {0}:\n\n{1}\n\nERROR: {2}\n".format(event["gcm_id"], event, str(e))) |
39
693db3f8cbe5
Begin importing event comparision stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
586 else: |
81
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
587 evn_unchanged += 1 |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
588 gcm_debug(4, u"No need to update event {0} : {1}.".format(event["id"], event["gcm_id"])) |
27 | 589 else: |
34 | 590 ## Event does not seem to exist. Insert new event. |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
591 gcm_debug(4, u"Inserting new event {0}".format(event["gcm_id"])) |
81
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
592 evn_new += 1 |
63 | 593 event.pop("id", None) |
594 event["iCalUID"] = event["gcm_id"] # Replace Google generated ID with our own | |
34 | 595 try: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
596 new_event = service.events().insert(calendarId=cfg.dst_id, body=event).execute() |
34 | 597 except Exception as e: |
65 | 598 gcm_fatal(u"Failed to insert new event:\n\n{0}\n\nERROR: {1}\n".format(event, str(e))) |
63 | 599 |
81
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
600 gcm_debug(3, "{0} new events, {1} updated, {2} unchanged.".format(evn_new, evn_updated, evn_unchanged)) |
ce02c0c00e64
Add debug information about how many events were new, updated or unchanged.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
601 |
34 | 602 |
603 ## Remove "stale" events | |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
604 gcm_debug(3, u"Purging stale events ..") |
85
da09a3b1edaa
Count and report number of purged events.
Matti Hamalainen <ccr@tnsp.org>
parents:
84
diff
changeset
|
605 evn_purged = 0 |
38
54405de302d0
Attempt to delete stale events. Still needs a check for already deleted
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
606 for event in dst_events: |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
607 gcm_debug(4, u"Checking event {0}".format(event["gcm_id"])) |
55 | 608 if not event["gcm_id"] in src_ids and event["status"] != u"cancelled": |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
609 gcm_debug(4, u"Deleting event {0}".format(event["gcm_id"])) |
85
da09a3b1edaa
Count and report number of purged events.
Matti Hamalainen <ccr@tnsp.org>
parents:
84
diff
changeset
|
610 evn_purged += 1 |
63 | 611 try: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
612 service.events().delete(calendarId=cfg.dst_id, eventId=event["id"]).execute() |
63 | 613 except Exception as e: |
65 | 614 gcm_fatal(u"Failed to delete stale event:\n{0}\n\nERROR: {1}\n".format(event, str(e))) |
38
54405de302d0
Attempt to delete stale events. Still needs a check for already deleted
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
615 |
85
da09a3b1edaa
Count and report number of purged events.
Matti Hamalainen <ccr@tnsp.org>
parents:
84
diff
changeset
|
616 gcm_debug(3, "{0} events purged.".format(evn_purged)) |
da09a3b1edaa
Count and report number of purged events.
Matti Hamalainen <ccr@tnsp.org>
parents:
84
diff
changeset
|
617 |
da09a3b1edaa
Count and report number of purged events.
Matti Hamalainen <ccr@tnsp.org>
parents:
84
diff
changeset
|
618 |
75 | 619 ## |
620 ## Finally, update the calendar name with timestamp | |
621 ## | |
73
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
622 t_time = time.localtime() |
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
623 t_str = time.strftime("%d.%m.%Y %H:%M", t_time) |
77
e5e7b6e9bd44
Implement debug levels in the main code.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
624 gcm_debug(3, u"Updating target calendar name timestamp {0}".format(t_str)) |
73
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
625 |
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
626 try: |
98
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
627 dst_calendar["summary"] = cfg.dst_name.format(t_str) |
fe3bfabf0b5f
Rename settings dest_* and source_* to dst_* and src_* respectively.
Matti Hamalainen <ccr@tnsp.org>
parents:
97
diff
changeset
|
628 new_calendar = service.calendars().update(calendarId=cfg.dst_id, body=dst_calendar).execute() |
73
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
629 except Exception as e: |
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
630 gcm_fatal(u"Failed to update target calendar:\n{0}\n\nERROR: {1}\n".format(dst_calendar, str(e))) |
b3f8621f1a25
Change how the target calendar settings work a bit. Also implement display
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
631 |
27 | 632 |
87
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
633 gcm_bench_end = time.time() |
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
634 gcm_bench_elapsed = gcm_bench_end - gcm_bench_start |
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
635 |
c3f8af496f46
Add "execution time elapsed" metering.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
636 gcm_debug(3, u"Finished. {0} seconds elapsed.".format(gcm_bench_elapsed)) |