__init__.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # ics2vdir - 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 pathlib
  22. import sys
  23. import tempfile
  24. import icalendar
  25. _LOGGER = logging.getLogger(__name__)
  26. def _event_prop_equal(prop_a, prop_b) -> bool:
  27. if isinstance(prop_a, list):
  28. return len(prop_a) == len(prop_b) and all(
  29. _event_prop_equal(*pair) for pair in zip(prop_a, prop_b)
  30. )
  31. if isinstance(prop_a, icalendar.prop.vDDDLists):
  32. # https://www.kanzaki.com/docs/ical/exdate.html
  33. return (
  34. isinstance(prop_b, icalendar.prop.vDDDLists)
  35. and len(prop_a.dts) == len(prop_b.dts)
  36. and all(_event_prop_equal(*pair) for pair in zip(prop_a.dts, prop_b.dts))
  37. and prop_a.params == prop_b.params
  38. )
  39. if isinstance(prop_a, (icalendar.prop.vDDDTypes, icalendar.prop.vCategory)):
  40. # pylint: disable=unidiomatic-typecheck
  41. return type(prop_a) == type(prop_b) and vars(prop_a) == vars(prop_b)
  42. return prop_a == prop_b
  43. def _events_equal(event_a: icalendar.cal.Event, event_b: icalendar.cal.Event) -> bool:
  44. for key, prop_a in event_a.items():
  45. if key == "DTSTAMP":
  46. continue
  47. prop_b = event_b[key]
  48. if not _event_prop_equal(prop_a, prop_b):
  49. _LOGGER.debug(
  50. "%s/%s: %r != %r", event_a["UID"], key, prop_a, prop_b,
  51. )
  52. return False
  53. return True
  54. def _datetime_basic_isoformat(dt_obj: datetime.datetime) -> str:
  55. # .isoformat() inserts unwanted separators
  56. return dt_obj.strftime("%Y%m%dT%H%M%S%z")
  57. def _event_vdir_filename(event: icalendar.cal.Event) -> str:
  58. # > An item should contain a UID property as described by the vCard and iCalendar standards.
  59. # > [...] The filename should have similar properties as the UID of the file content.
  60. # > However, there is no requirement for these two to be the same.
  61. # > Programs may choose to store additional metadata in that filename, [...]
  62. # https://vdirsyncer.readthedocs.io/en/stable/vdir.html#basic-structure
  63. output_filename = str(event["UID"])
  64. if "RECURRENCE-ID" in event:
  65. recurrence_id = event["RECURRENCE-ID"]
  66. assert isinstance(recurrence_id.dt, datetime.datetime), recurrence_id.dt
  67. output_filename += "." + _datetime_basic_isoformat(recurrence_id.dt)
  68. return output_filename + ".ics"
  69. def _export_event(event: icalendar.cal.Event, output_dir_path: pathlib.Path) -> None:
  70. temp_fd, temp_path = tempfile.mkstemp(prefix="ics2vdir-", suffix=".ics")
  71. os.write(temp_fd, event.to_ical())
  72. os.close(temp_fd)
  73. output_path = output_dir_path.joinpath(_event_vdir_filename(event))
  74. if not output_path.exists():
  75. _LOGGER.info("creating %s", output_path)
  76. os.rename(temp_path, output_path)
  77. else:
  78. with open(output_path, "rb") as current_file:
  79. current_event = icalendar.Event.from_ical(current_file.read())
  80. if _events_equal(event, current_event):
  81. _LOGGER.debug("%s is up to date", output_path)
  82. else:
  83. _LOGGER.info("updating %s", output_path)
  84. os.rename(temp_path, output_path)
  85. def _main():
  86. # https://docs.python.org/3/library/logging.html#levels
  87. logging.basicConfig(
  88. format="%(message)s",
  89. # datefmt='%Y-%m-%dT%H:%M:%S%z',
  90. level=logging.INFO,
  91. )
  92. argparser = argparse.ArgumentParser(
  93. description="Convert iCalendar .ics file to vdir directory."
  94. " Reads from stdin."
  95. )
  96. argparser.add_argument(
  97. "-o",
  98. "--output",
  99. "--output-dir",
  100. default=os.getcwd(),
  101. type=pathlib.Path,
  102. metavar="path",
  103. dest="output_dir_path",
  104. help="Path to output directory (default: current workings dir)",
  105. )
  106. argparser.add_argument(
  107. "-s",
  108. "--silent",
  109. "-q",
  110. "--quiet",
  111. action="store_true",
  112. help="Reduce verbosity.",
  113. )
  114. argparser.add_argument(
  115. "-v", "--verbose", action="store_true", help="Increase verbosity",
  116. )
  117. args = argparser.parse_args()
  118. if args.verbose:
  119. logging.getLogger().setLevel(level=logging.DEBUG)
  120. elif args.silent:
  121. logging.getLogger().setLevel(level=logging.WARNING)
  122. calendar = icalendar.Calendar.from_ical(sys.stdin.read())
  123. _LOGGER.debug("%d subcomponents", len(calendar.subcomponents))
  124. for component in calendar.subcomponents:
  125. if isinstance(component, icalendar.cal.Event):
  126. _export_event(event=component, output_dir_path=args.output_dir_path)
  127. else:
  128. _LOGGER.debug("%s", component)