__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 logging
  19. import os
  20. import pathlib
  21. import sys
  22. import tempfile
  23. import icalendar
  24. _LOGGER = logging.getLogger(__name__)
  25. def _event_prop_equal(prop_a, prop_b) -> bool:
  26. if isinstance(prop_a, icalendar.prop.vDDDLists):
  27. # https://www.kanzaki.com/docs/ical/exdate.html
  28. return (
  29. all(_event_prop_equal(*pair) for pair in zip(prop_a.dts, prop_b.dts))
  30. and len(prop_a.dts) == len(prop_b.dts)
  31. and prop_a.params == prop_b.params
  32. )
  33. if isinstance(prop_a, (icalendar.prop.vDDDTypes, icalendar.prop.vCategory)):
  34. # pylint: disable=unidiomatic-typecheck
  35. return type(prop_a) == type(prop_b) and vars(prop_a) == vars(prop_b)
  36. return prop_a == prop_b
  37. def _events_equal(event_a: icalendar.cal.Event, event_b: icalendar.cal.Event) -> bool:
  38. for key, prop_a in event_a.items():
  39. if key == "DTSTAMP":
  40. continue
  41. prop_b = event_b[key]
  42. if not _event_prop_equal(prop_a, prop_b):
  43. _LOGGER.debug(
  44. "%s/%s: %r != %r", event_a["UID"], key, prop_a, prop_b,
  45. )
  46. return False
  47. return True
  48. def _export_event(event: icalendar.cal.Event, output_dir_path: pathlib.Path) -> None:
  49. temp_fd, temp_path = tempfile.mkstemp(prefix="ics2vdir-", suffix=".ics")
  50. os.write(temp_fd, event.to_ical())
  51. os.close(temp_fd)
  52. output_path = output_dir_path.joinpath("{}.ics".format(event["UID"]))
  53. if not output_path.exists():
  54. _LOGGER.info("creating %s", output_path)
  55. os.rename(temp_path, output_path)
  56. else:
  57. with open(output_path, "rb") as current_file:
  58. current_event = icalendar.Event.from_ical(current_file.read())
  59. if _events_equal(event, current_event):
  60. _LOGGER.debug("%s is up to date", output_path)
  61. else:
  62. _LOGGER.info("updating %s", output_path)
  63. os.rename(temp_path, output_path)
  64. def _main():
  65. logging.basicConfig(
  66. format="%(message)s",
  67. # datefmt='%Y-%m-%dT%H:%M:%S%z',
  68. level=logging.INFO,
  69. )
  70. argparser = argparse.ArgumentParser(
  71. description="Convert iCalendar .ics file to vdir directory."
  72. " Reads from stdin."
  73. )
  74. argparser.add_argument(
  75. "-o",
  76. "--output",
  77. "--output-dir",
  78. default=os.getcwd(),
  79. type=pathlib.Path,
  80. metavar="path",
  81. dest="output_dir_path",
  82. help="Path to output directory (default: current workings dir)",
  83. )
  84. args = argparser.parse_args()
  85. calendar = icalendar.Calendar.from_ical(sys.stdin.read())
  86. _LOGGER.debug("%d subcomponents", len(calendar.subcomponents))
  87. for component in calendar.subcomponents:
  88. if isinstance(component, icalendar.cal.Event):
  89. _export_event(event=component, output_dir_path=args.output_dir_path)
  90. else:
  91. _LOGGER.debug("%s", component)