__init__.py 3.0 KB

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