Mercurial > hg > gcmultimerge
comparison multimerge.py @ 139:23fc7cd1cd53
Py3 cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 28 Feb 2022 18:17:20 +0200 |
parents | d3135eff1bab |
children | 65d4da73e558 |
comparison
equal
deleted
inserted
replaced
138:d3135eff1bab | 139:23fc7cd1cd53 |
---|---|
147 for event in events: | 147 for event in events: |
148 if show == None or show(event): | 148 if show == None or show(event): |
149 ev_start = event["start"].get("dateTime", event["start"].get("date")) if "start" in event else "?" | 149 ev_start = event["start"].get("dateTime", event["start"].get("date")) if "start" in event else "?" |
150 ev_end = event["end"].get("dateTime", event["end"].get("date")) if "end" in event else "?" | 150 ev_end = event["end"].get("dateTime", event["end"].get("date")) if "end" in event else "?" |
151 summary = event["summary"] if "summary" in event else "?" | 151 summary = event["summary"] if "summary" in event else "?" |
152 status = "*" if event["status"] != u"cancelled" else "!" | 152 status = "*" if event["status"] != "cancelled" else "!" |
153 gcm_print("[{0}] {1:25} - {2:25} : {3} [{4}] [{5}]".format(status, ev_start, ev_end, summary, event["iCalUID"], event["id"])) | 153 gcm_print("[{0}] {1:25} - {2:25} : {3} [{4}] [{5}]".format(status, ev_start, ev_end, summary, event["iCalUID"], event["id"])) |
154 | 154 |
155 | 155 |
156 ## Generate gcm IDs for given list of events | 156 ## Generate gcm IDs for given list of events |
157 def gcm_generate_ids(events, calendar_id, sep, field): | 157 def gcm_generate_ids(events, calendar_id, sep, field): |
339 | 339 |
340 def is_email_state(self, mvalue): | 340 def is_email_state(self, mvalue): |
341 if not self.is_str(mvalue): | 341 if not self.is_str(mvalue): |
342 return False | 342 return False |
343 else: | 343 else: |
344 return mvalue.lower() in [u"off", "sendmail", "smtp"] | 344 return mvalue.lower() in ["off", "sendmail", "smtp"] |
345 | 345 |
346 def trans_email_state(self, mvalue): | 346 def trans_email_state(self, mvalue): |
347 return mvalue.lower() | 347 return mvalue.lower() |
348 | 348 |
349 def is_filename(self, mvalue): | 349 def is_filename(self, mvalue): |
540 src_re = re.compile(cfg.src_regex, re.UNICODE) | 540 src_re = re.compile(cfg.src_regex, re.UNICODE) |
541 dst_re = re.compile(cfg.dst_regex, re.UNICODE) | 541 dst_re = re.compile(cfg.dst_regex, re.UNICODE) |
542 src_calendars = [] | 542 src_calendars = [] |
543 dst_calendar = None | 543 dst_calendar = None |
544 for calendar in calendars: | 544 for calendar in calendars: |
545 if u"summary" in calendar: | 545 if "summary" in calendar: |
546 # Find destination calendar ID if not set | 546 # Find destination calendar ID if not set |
547 if not cfg.dst_id and dst_re.match(calendar["summary"]): | 547 if not cfg.dst_id and dst_re.match(calendar["summary"]): |
548 cfg.mset("dst_id", calendar["id"]) | 548 cfg.mset("dst_id", calendar["id"]) |
549 dst_calendar = calendar | 549 dst_calendar = calendar |
550 elif cfg.dst_id and calendar["id"] == cfg.dst_id: | 550 elif cfg.dst_id and calendar["id"] == cfg.dst_id: |
551 dst_calendar = calendar | 551 dst_calendar = calendar |
552 | 552 |
553 # If summary or summaryOverride match the regexp, add calendar | 553 # If summary or summaryOverride match the regexp, add calendar |
554 mre = src_re.match(calendar["summary"]) | 554 mre = src_re.match(calendar["summary"]) |
555 if not mre and u"summaryOverride" in calendar: | 555 if not mre and "summaryOverride" in calendar: |
556 mre = src_re.match(calendar["summaryOverride"]) | 556 mre = src_re.match(calendar["summaryOverride"]) |
557 calendar["summary"] = calendar["summaryOverride"] | 557 calendar["summary"] = calendar["summaryOverride"] |
558 | 558 |
559 if mre: | 559 if mre: |
560 calendar["gcm_title"] = mre.group(cfg.src_regmap[0]) | 560 calendar["gcm_title"] = mre.group(cfg.src_regmap[0]) |
597 # Fetch and add events, if any, to main source events list | 597 # Fetch and add events, if any, to main source events list |
598 events = gcm_generate_ids(gcm_fetch_events(calendar["id"], False), calendar["id"], "___", "id") | 598 events = gcm_generate_ids(gcm_fetch_events(calendar["id"], False), calendar["id"], "___", "id") |
599 if events: | 599 if events: |
600 for event in events: | 600 for event in events: |
601 # Set summary and color for existing events | 601 # Set summary and color for existing events |
602 if event["status"] != u"cancelled": | 602 if event["status"] != "cancelled": |
603 if c_found != None: | 603 if c_found != None: |
604 event["colorId"] = c_found | 604 event["colorId"] = c_found |
605 if "summary" in event: | 605 if "summary" in event: |
606 event["summary"] = u"[{1}] {0}".format(event["summary"], calendar["gcm_id"]) | 606 event["summary"] = "[{1}] {0}".format(event["summary"], calendar["gcm_id"]) |
607 else: | 607 else: |
608 event["summary"] = u"[?] {0}".format(calendar["gcm_id"]) | 608 event["summary"] = "[?] {0}".format(calendar["gcm_id"]) |
609 | 609 |
610 # Add to list of source events | 610 # Add to list of source events |
611 src_events.extend(events) | 611 src_events.extend(events) |
612 if gcm_check_debug(4): | 612 if gcm_check_debug(4): |
613 gcm_dump_events(events, (lambda ev: ev["status"] != u"cancelled")) | 613 gcm_dump_events(events, (lambda ev: ev["status"] != "cancelled")) |
614 | 614 |
615 | 615 |
616 ## Fetch current events from the target | 616 ## Fetch current events from the target |
617 gcm_debug(3, "Fetching current target calendar events.") | 617 gcm_debug(3, "Fetching current target calendar events.") |
618 dst_events = gcm_generate_ids(gcm_fetch_events(cfg.dst_id, True), "", "", "iCalUID") | 618 dst_events = gcm_generate_ids(gcm_fetch_events(cfg.dst_id, True), "", "", "iCalUID") |
645 except Exception as e: | 645 except Exception as e: |
646 gcm_fatal("Failed to update event {0} [{1}]:\n\n{2}\n\nERROR: {3}\n".format(event["id"], event["gcm_id"], event, str(e))) | 646 gcm_fatal("Failed to update event {0} [{1}]:\n\n{2}\n\nERROR: {3}\n".format(event["id"], event["gcm_id"], event, str(e))) |
647 else: | 647 else: |
648 evn_unchanged += 1 | 648 evn_unchanged += 1 |
649 gcm_debug(4, "No need to update event {0} [{1}]".format(event["id"], event["gcm_id"])) | 649 gcm_debug(4, "No need to update event {0} [{1}]".format(event["id"], event["gcm_id"])) |
650 elif event["status"] not in [u"cancelled", "confirmed"]: | 650 elif event["status"] not in ["cancelled", "confirmed"]: |
651 # Event does not seem to exist. Insert new event. | 651 # Event does not seem to exist. Insert new event. |
652 gcm_debug(4, "Inserting new event {0} [{1}]".format(event["id"], event["gcm_id"])) | 652 gcm_debug(4, "Inserting new event {0} [{1}]".format(event["id"], event["gcm_id"])) |
653 # Remove original id field, otherwise it will clash | 653 # Remove original id field, otherwise it will clash |
654 event.pop("id", None) | 654 event.pop("id", None) |
655 event["iCalUID"] = event["gcm_id"] # Replace Google generated ID with our own | 655 event["iCalUID"] = event["gcm_id"] # Replace Google generated ID with our own |
665 ## Remove "stale" events | 665 ## Remove "stale" events |
666 gcm_debug(3, "Purging stale events ..") | 666 gcm_debug(3, "Purging stale events ..") |
667 evn_purged = 0 | 667 evn_purged = 0 |
668 for event in dst_events: | 668 for event in dst_events: |
669 gcm_debug(4, "Checking event {0}".format(event["gcm_id"])) | 669 gcm_debug(4, "Checking event {0}".format(event["gcm_id"])) |
670 if not event["gcm_id"] in src_ids and event["status"] != u"cancelled": | 670 if not event["gcm_id"] in src_ids and event["status"] != "cancelled": |
671 gcm_debug(4, "Deleting event {0} [{1}]".format(event["id"], event["gcm_id"])) | 671 gcm_debug(4, "Deleting event {0} [{1}]".format(event["id"], event["gcm_id"])) |
672 evn_purged += 1 | 672 evn_purged += 1 |
673 try: | 673 try: |
674 service.events().delete(calendarId=cfg.dst_id, eventId=event["id"]).execute() | 674 service.events().delete(calendarId=cfg.dst_id, eventId=event["id"]).execute() |
675 except Exception as e: | 675 except Exception as e: |