66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
from pprint import pprint
|
|
import sys
|
|
import yaml
|
|
import glob
|
|
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 process_contact(file: Path, target_dir: Path, user: str):
|
|
# print(f"Reading {user} contact {file.name}")
|
|
|
|
card, events = addressbook.read_contact(str(file))
|
|
name = addressbook_utils.get_contact_name(card)
|
|
id = card.contents["uid"][0].value.strip()
|
|
|
|
if len(events):
|
|
print(f"Reading {user} contact {file.name}")
|
|
# pprint(card)
|
|
print(f"Writing contact \"{name}\" {len(events)} events to {target_dir}")
|
|
|
|
for file_to_delete in target_dir.glob(f"{id}-*.ics"):
|
|
print(f"Deleting old contact event {file_to_delete.name}")
|
|
file_to_delete.unlink()
|
|
|
|
for event in events:
|
|
evtid, evt = calendar.create_event(name, f"{id}-{event.id}", event)
|
|
print(f"Writing contact \"{name}\" event {evtid}")
|
|
with open(target_dir / f"{evtid}.ics", 'w') as f:
|
|
f.write(evt)
|
|
|
|
def handle_rescan(user: str):
|
|
print(f"Rescanning {user} addressbooks")
|
|
|
|
config = load_config()
|
|
|
|
user_config = config.calendars[user]
|
|
|
|
pprint(user_config)
|
|
|
|
base_path = Path(config.calendars[user].base.dir)
|
|
target_calendar = base_path / config.calendars[user].dates_target
|
|
addressbooks = user_config.rescan
|
|
|
|
for ab in addressbooks:
|
|
dir = base_path / ab.path
|
|
print(f"Rescanning {user} addressbook {dir}")
|
|
|
|
files = dir.glob("*.vcf")
|
|
for file in files:
|
|
process_contact(file, target_calendar, user)
|
|
|
|
def main():
|
|
user = sys.argv[1] if len(sys.argv) > 1 else "?"
|
|
handle_rescan(user)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|