__init__.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # ical2vdir - convert .ics file to vdir directory
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import argparse
  18. import datetime
  19. import logging
  20. import os
  21. import shutil
  22. import pathlib
  23. import sys
  24. import tempfile
  25. import typing
  26. import icalendar
  27. _LOGGER = logging.getLogger(__name__)
  28. _VDIR_EVENT_FILE_EXTENSION = ".ics"
  29. def _event_prop_equal(prop_a: typing.Any, prop_b: typing.Any) -> bool:
  30. if isinstance(prop_a, list):
  31. return len(prop_a) == len(prop_b) and all(
  32. _event_prop_equal(*pair) for pair in zip(prop_a, prop_b)
  33. )
  34. if isinstance(prop_a, icalendar.prop.vDDDLists):
  35. # https://www.kanzaki.com/docs/ical/exdate.html
  36. return (
  37. isinstance(prop_b, icalendar.prop.vDDDLists)
  38. and len(prop_a.dts) == len(prop_b.dts)
  39. and all(_event_prop_equal(*pair) for pair in zip(prop_a.dts, prop_b.dts))
  40. and prop_a.params == prop_b.params
  41. )
  42. if isinstance(prop_a, (icalendar.prop.vDDDTypes, icalendar.prop.vCategory)):
  43. # pylint: disable=unidiomatic-typecheck
  44. return type(prop_a) == type(prop_b) and vars(prop_a) == vars(prop_b)
  45. return typing.cast(bool, prop_a == prop_b and prop_a.params == prop_b.params)
  46. def _events_equal(event_a: icalendar.cal.Event, event_b: icalendar.cal.Event) -> bool:
  47. for key, prop_a in event_a.items():
  48. if key == "DTSTAMP":
  49. continue
  50. try:
  51. prop_b = event_b[key]
  52. except KeyError:
  53. _LOGGER.debug("%s: new key %s", event_a["UID"], key)
  54. return False
  55. if not _event_prop_equal(prop_a, prop_b):
  56. _LOGGER.debug(
  57. "%s/%s: %r != %r",
  58. event_a["UID"],
  59. key,
  60. prop_a,
  61. prop_b,
  62. )
  63. return False
  64. return True
  65. def _datetime_basic_isoformat(dt_obj: datetime.datetime) -> str:
  66. # .isoformat() inserts unwanted separators
  67. return dt_obj.strftime("%Y%m%dT%H%M%S%z")
  68. def _event_vdir_filename(event: icalendar.cal.Event) -> str:
  69. # > An item should contain a UID property as described by the vCard and iCalendar standards.
  70. # > [...] The filename should have similar properties as the UID of the file content.
  71. # > However, there is no requirement for these two to be the same.
  72. # > Programs may choose to store additional metadata in that filename, [...]
  73. # https://vdirsyncer.readthedocs.io/en/stable/vdir.html#basic-structure
  74. output_filename = str(event["UID"])
  75. if "RECURRENCE-ID" in event:
  76. recurrence_id = event["RECURRENCE-ID"]
  77. assert isinstance(recurrence_id.dt, datetime.datetime), recurrence_id.dt
  78. output_filename += "." + _datetime_basic_isoformat(recurrence_id.dt)
  79. return output_filename + _VDIR_EVENT_FILE_EXTENSION
  80. def _write_event(event: icalendar.cal.Event, path: pathlib.Path) -> None:
  81. if path.is_dir():
  82. raise IsADirectoryError(path) # similar to os.rename
  83. # > Creating and modifying items or metadata files should happen atomically.
  84. # https://vdirsyncer.readthedocs.io/en/stable/vdir.html#writing-to-vdirs
  85. temp_fd, temp_path = tempfile.mkstemp(
  86. prefix="ical2vdir-", suffix=_VDIR_EVENT_FILE_EXTENSION
  87. )
  88. try:
  89. # > Content lines are delimited by a line break,
  90. # > which is a CRLF sequence [...]
  91. # https://tools.ietf.org/html/rfc5545#section-3.1
  92. os.write(temp_fd, event.to_ical())
  93. os.close(temp_fd)
  94. shutil.move(temp_path, path)
  95. finally:
  96. if os.path.exists(temp_path):
  97. os.unlink(temp_path)
  98. def _sync_event(
  99. event: icalendar.cal.Event, output_dir_path: pathlib.Path
  100. ) -> pathlib.Path:
  101. output_path = output_dir_path.joinpath(_event_vdir_filename(event))
  102. if not output_path.exists():
  103. _LOGGER.info("creating %s", output_path)
  104. _write_event(event, output_path)
  105. else:
  106. with output_path.open("rb") as current_file:
  107. current_event = icalendar.Event.from_ical(current_file.read())
  108. if _events_equal(event, current_event):
  109. _LOGGER.debug("%s is up to date", output_path)
  110. else:
  111. _LOGGER.info("updating %s", output_path)
  112. _write_event(event, output_path)
  113. return output_path
  114. def _main() -> None:
  115. # https://docs.python.org/3/library/logging.html#levels
  116. logging.basicConfig(
  117. format="%(message)s",
  118. # datefmt='%Y-%m-%dT%H:%M:%S%z',
  119. level=logging.INFO,
  120. )
  121. argparser = argparse.ArgumentParser(
  122. description="Convert iCalendar .ics file to vdir directory."
  123. " Reads from stdin."
  124. )
  125. argparser.add_argument(
  126. "-o",
  127. "--output",
  128. "--output-dir",
  129. default=os.getcwd(),
  130. type=pathlib.Path,
  131. metavar="path",
  132. dest="output_dir_path",
  133. help="Path to output directory (default: current workings dir)",
  134. )
  135. argparser.add_argument(
  136. "--delete",
  137. action="store_true",
  138. help="Delete events not in input from output directory.",
  139. )
  140. argparser.add_argument(
  141. "-s",
  142. "--silent",
  143. "-q",
  144. "--quiet",
  145. action="store_true",
  146. help="Reduce verbosity.",
  147. )
  148. argparser.add_argument(
  149. "-v",
  150. "--verbose",
  151. action="store_true",
  152. help="Increase verbosity",
  153. )
  154. args = argparser.parse_args()
  155. if args.verbose:
  156. logging.getLogger().setLevel(level=logging.DEBUG)
  157. elif args.silent:
  158. logging.getLogger().setLevel(level=logging.WARNING)
  159. calendar = icalendar.Calendar.from_ical(sys.stdin.read())
  160. _LOGGER.debug("%d subcomponents", len(calendar.subcomponents))
  161. extra_paths = set(
  162. path
  163. for path in args.output_dir_path.iterdir()
  164. if path.is_file() and path.name.endswith(_VDIR_EVENT_FILE_EXTENSION)
  165. )
  166. for component in calendar.subcomponents:
  167. if isinstance(component, icalendar.cal.Event):
  168. extra_paths.discard(
  169. _sync_event(event=component, output_dir_path=args.output_dir_path)
  170. )
  171. else:
  172. _LOGGER.debug("%s", component)
  173. _LOGGER.debug(
  174. "%d pre-existing items not in input: %s",
  175. len(extra_paths),
  176. ", ".join(p.name for p in extra_paths),
  177. )
  178. if args.delete:
  179. for path in extra_paths:
  180. _LOGGER.info("removing %s", path)
  181. path.unlink()