62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
from pprint import pprint
|
|
import sys
|
|
import yaml
|
|
from box import Box
|
|
from pathlib import Path
|
|
|
|
from contactdates import calendar, addressbook, addressbook_utils
|
|
|
|
def load_config():
|
|
CONFIG_PATH = Path(__file__).parent / "config.yaml"
|
|
|
|
with open(CONFIG_PATH) as f:
|
|
return Box(yaml.safe_load(f))
|
|
|
|
def handle_contact_change(action: str, path: str, user: str):
|
|
config = load_config()
|
|
|
|
base_path = Path(config.calendars[user].base.dir)
|
|
target_calendar = config.calendars[user].dates_target
|
|
|
|
dir = base_path / target_calendar
|
|
|
|
print(f"Reading contact {path}")
|
|
|
|
card, events = addressbook.read_contact(path)
|
|
name = addressbook_utils.get_contact_name(card)
|
|
id = card.contents["uid"][0].value.strip()
|
|
|
|
|
|
print(f"Writing contact \"{name}\" {len(events)} events to {dir}")
|
|
|
|
for event in events:
|
|
evtid, evt = calendar.create_event(name, f"{id}-{event.id}", event)
|
|
print(f"Writing contact \"{name}\" event {evtid}")
|
|
with open(dir / f"{evtid}.ics", 'w') as f:
|
|
f.write(evt)
|
|
|
|
def handle_change():
|
|
user = sys.argv[1] if len(sys.argv) > 1 else "?"
|
|
path = sys.argv[2] if len(sys.argv) > 2 else "?"
|
|
request = sys.argv[3] if len(sys.argv) > 3 else "?"
|
|
|
|
# Distinguish contacts (CardDAV) vs calendar (CalDAV) by path
|
|
if ".vcf" in path:
|
|
kind = "CONTACT"
|
|
elif ".ics" in path:
|
|
kind = "EVENT"
|
|
else:
|
|
kind = "COLLECTION"
|
|
|
|
print(f"{request} {user} {kind} {path}")
|
|
|
|
if kind == "CONTACT":
|
|
handle_contact_change(request, path, user)
|
|
|
|
def main():
|
|
handle_change()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|