123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import argparse
- import datetime
- import logging
- import os
- import pathlib
- import sys
- import tempfile
- import icalendar
- _LOGGER = logging.getLogger(__name__)
- _VDIR_EVENT_FILE_EXTENSION = ".ics"
- def _event_prop_equal(prop_a, prop_b) -> bool:
- if isinstance(prop_a, list):
- return len(prop_a) == len(prop_b) and all(
- _event_prop_equal(*pair) for pair in zip(prop_a, prop_b)
- )
- if isinstance(prop_a, icalendar.prop.vDDDLists):
-
- return (
- isinstance(prop_b, icalendar.prop.vDDDLists)
- and len(prop_a.dts) == len(prop_b.dts)
- and all(_event_prop_equal(*pair) for pair in zip(prop_a.dts, prop_b.dts))
- and prop_a.params == prop_b.params
- )
- if isinstance(prop_a, (icalendar.prop.vDDDTypes, icalendar.prop.vCategory)):
-
- return type(prop_a) == type(prop_b) and vars(prop_a) == vars(prop_b)
- return prop_a == prop_b
- def _events_equal(event_a: icalendar.cal.Event, event_b: icalendar.cal.Event) -> bool:
- for key, prop_a in event_a.items():
- if key == "DTSTAMP":
- continue
- prop_b = event_b[key]
- if not _event_prop_equal(prop_a, prop_b):
- _LOGGER.debug(
- "%s/%s: %r != %r", event_a["UID"], key, prop_a, prop_b,
- )
- return False
- return True
- def _datetime_basic_isoformat(dt_obj: datetime.datetime) -> str:
-
- return dt_obj.strftime("%Y%m%dT%H%M%S%z")
- def _event_vdir_filename(event: icalendar.cal.Event) -> str:
-
-
-
-
-
- output_filename = str(event["UID"])
- if "RECURRENCE-ID" in event:
- recurrence_id = event["RECURRENCE-ID"]
- assert isinstance(recurrence_id.dt, datetime.datetime), recurrence_id.dt
- output_filename += "." + _datetime_basic_isoformat(recurrence_id.dt)
- return output_filename + _VDIR_EVENT_FILE_EXTENSION
- def _export_event(
- event: icalendar.cal.Event, output_dir_path: pathlib.Path
- ) -> pathlib.Path:
- temp_fd, temp_path = tempfile.mkstemp(
- prefix="ics2vdir-", suffix=_VDIR_EVENT_FILE_EXTENSION
- )
- os.write(temp_fd, event.to_ical())
- os.close(temp_fd)
- output_path = output_dir_path.joinpath(_event_vdir_filename(event))
- if not output_path.exists():
- _LOGGER.info("creating %s", output_path)
- os.rename(temp_path, output_path)
- else:
- with open(output_path, "rb") as current_file:
- current_event = icalendar.Event.from_ical(current_file.read())
- if _events_equal(event, current_event):
- _LOGGER.debug("%s is up to date", output_path)
- else:
- _LOGGER.info("updating %s", output_path)
- os.rename(temp_path, output_path)
- return output_path
- def _main():
-
- logging.basicConfig(
- format="%(message)s",
-
- level=logging.INFO,
- )
- argparser = argparse.ArgumentParser(
- description="Convert iCalendar .ics file to vdir directory."
- " Reads from stdin."
- )
- argparser.add_argument(
- "-o",
- "--output",
- "--output-dir",
- default=os.getcwd(),
- type=pathlib.Path,
- metavar="path",
- dest="output_dir_path",
- help="Path to output directory (default: current workings dir)",
- )
- argparser.add_argument(
- "--delete",
- action="store_true",
- help="Delete events not in input from output directory.",
- )
- argparser.add_argument(
- "-s",
- "--silent",
- "-q",
- "--quiet",
- action="store_true",
- help="Reduce verbosity.",
- )
- argparser.add_argument(
- "-v", "--verbose", action="store_true", help="Increase verbosity",
- )
- args = argparser.parse_args()
- if args.verbose:
- logging.getLogger().setLevel(level=logging.DEBUG)
- elif args.silent:
- logging.getLogger().setLevel(level=logging.WARNING)
- calendar = icalendar.Calendar.from_ical(sys.stdin.read())
- _LOGGER.debug("%d subcomponents", len(calendar.subcomponents))
- extra_paths = set(
- path
- for path in args.output_dir_path.iterdir()
- if path.is_file() and path.name.endswith(_VDIR_EVENT_FILE_EXTENSION)
- )
- for component in calendar.subcomponents:
- if isinstance(component, icalendar.cal.Event):
- extra_paths.discard(
- _export_event(event=component, output_dir_path=args.output_dir_path)
- )
- else:
- _LOGGER.debug("%s", component)
- _LOGGER.debug(
- "%d pre-existing items not in input: %s",
- len(extra_paths),
- ", ".join(p.name for p in extra_paths),
- )
- if args.delete:
- for path in extra_paths:
- _LOGGER.info("removing %s", path)
- path.unlink()
|