comparison multimerge.py @ 73:b3f8621f1a25

Change how the target calendar settings work a bit. Also implement display of latest update timestamp in the target calendar name.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 12 Jul 2016 11:44:25 +0300
parents 1ab40033bb87
children ee05430f1a4d
comparison
equal deleted inserted replaced
72:1ab40033bb87 73:b3f8621f1a25
11 import sys 11 import sys
12 import signal 12 import signal
13 import re 13 import re
14 import codecs 14 import codecs
15 import math 15 import math
16 #import time 16 import time
17 #import datetime 17 #import datetime
18 18
19 import smtplib 19 import smtplib
20 from email.mime.text import MIMEText 20 from email.mime.text import MIMEText
21 21
343 343
344 cfg.mdef("source_regex", True, cfg.is_string, None, u"^R:\s*(.*?)\s*\(\s*(.+?)\s*\)\s*$") 344 cfg.mdef("source_regex", True, cfg.is_string, None, u"^R:\s*(.*?)\s*\(\s*(.+?)\s*\)\s*$")
345 cfg.mdef("source_regmap", False, cfg.is_list, cfg.trans_list, [1, 2]) 345 cfg.mdef("source_regmap", False, cfg.is_list, cfg.trans_list, [1, 2])
346 cfg.mdef("source_regmap_len", False, None, None, len(cfg.source_regmap)) 346 cfg.mdef("source_regmap_len", False, None, None, len(cfg.source_regmap))
347 347
348 cfg.mdef("dest_name", True, cfg.is_string, None, u"Raahen kansainvälisyystoiminta") 348 cfg.mdef("dest_name", True, cfg.is_string, None, u"Raahen kansainvälisyystoiminta [{0}]")
349 cfg.mdef("dest_regex", True, cfg.is_string, None, u"^Raahen kansainvälisyystoiminta")
349 cfg.mdef("dest_id", True, cfg.is_string, None, None) 350 cfg.mdef("dest_id", True, cfg.is_string, None, None)
350 351
351 cfg.mdef("noauth_local_webserver", False, None, None, True) 352 cfg.mdef("noauth_local_webserver", False, None, None, True)
352 #cfg.mdef("auth_host_name", False, None, None, "localhost") 353 #cfg.mdef("auth_host_name", False, None, None, "localhost")
353 #cfg.mdef("auth_host_port", False, None, None, [8080, 8090]) 354 #cfg.mdef("auth_host_port", False, None, None, [8080, 8090])
404 cfg.source_regmap = map(lambda x: int(x), cfg.source_regmap) 405 cfg.source_regmap = map(lambda x: int(x), cfg.source_regmap)
405 except Exception as e: 406 except Exception as e:
406 gcm_fatal(u"Invalid source_regmap: {0}".format(str(e))) 407 gcm_fatal(u"Invalid source_regmap: {0}".format(str(e)))
407 408
408 409
409 if not cfg.dest_name and not cfg.dest_id: 410 if not cfg.dest_regex and not cfg.dest_id:
410 gcm_fatal(u"Target calendar ID or name required, but not set.") 411 gcm_fatal(u"Target calendar ID or name required, but not set.")
411 412
412
413 if cfg.dest_name:
414 cfg.mset("dest_name", cfg.mget("dest_name").strip())
415 413
416 414
417 ## Initialize and authorize API connection 415 ## Initialize and authorize API connection
418 credentials = gcm_get_credentials(cfg) 416 credentials = gcm_get_credentials(cfg)
419 http = credentials.authorize(httplib2.Http()) 417 http = credentials.authorize(httplib2.Http())
443 gcm_debug(u"{0} calendars total found.".format(len(calendars))) 441 gcm_debug(u"{0} calendars total found.".format(len(calendars)))
444 442
445 443
446 ## Filter desired SOURCE calendars based on specified regexp 444 ## Filter desired SOURCE calendars based on specified regexp
447 src_re = re.compile(cfg.source_regex, re.UNICODE) 445 src_re = re.compile(cfg.source_regex, re.UNICODE)
446 dst_re = re.compile(cfg.dest_regex, re.UNICODE)
448 src_calendars = [] 447 src_calendars = []
449 dst_calendar = None 448 dst_calendar = None
450 for calendar in calendars: 449 for calendar in calendars:
451 if u"summary" in calendar: 450 if u"summary" in calendar:
452 # Find destination calendar ID if not set 451 # Find destination calendar ID if not set
453 if not cfg.dest_id and cfg.dest_name == calendar["summary"].strip(): 452 if not cfg.dest_id and dst_re.match(calendar["summary"]):
454 cfg.mset("dest_id", calendar["id"]) 453 cfg.mset("dest_id", calendar["id"])
455 dst_calendar = calendar 454 dst_calendar = calendar
456 455
457 # If summary or summaryOverride match the regexp, add calendar 456 # If summary or summaryOverride match the regexp, add calendar
458 mre = src_re.match(calendar["summary"]) 457 mre = src_re.match(calendar["summary"])
573 try: 572 try:
574 service.events().delete(calendarId=cfg.dest_id, eventId=event["id"]).execute() 573 service.events().delete(calendarId=cfg.dest_id, eventId=event["id"]).execute()
575 except Exception as e: 574 except Exception as e:
576 gcm_fatal(u"Failed to delete stale event:\n{0}\n\nERROR: {1}\n".format(event, str(e))) 575 gcm_fatal(u"Failed to delete stale event:\n{0}\n\nERROR: {1}\n".format(event, str(e)))
577 576
577 ## Update the target calendar name with timestamp
578 t_time = time.localtime()
579 t_str = time.strftime("%d.%m.%Y %H:%M", t_time)
580 gcm_debug(u"Updating target calendar name timestamp {0}".format(t_str))
581
582 try:
583 dst_calendar["summary"] = cfg.dest_name.format(t_str)
584 new_calendar = service.calendars().update(calendarId=cfg.dest_id, body=dst_calendar).execute()
585 except Exception as e:
586 gcm_fatal(u"Failed to update target calendar:\n{0}\n\nERROR: {1}\n".format(dst_calendar, str(e)))
587
578 588
579 gcm_debug(u"Finished.") 589 gcm_debug(u"Finished.")