__init__.py 3.4 KB

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