72 lines
1.6 KiB
Python
Executable File
72 lines
1.6 KiB
Python
Executable File
#!/bin/python
|
|
|
|
# Iterate over all files passed as arguments and create an calendar entry for each of them.
|
|
# Directories and non-yaml (must end with `.yaml` or `.yml`) are ignored.
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
import yaml
|
|
from datetime import datetime
|
|
import subprocess
|
|
|
|
CALENDAR = "TheAlt"
|
|
|
|
if len(sys.argv) <= 1:
|
|
print("Provide some file to extract the event from.")
|
|
sys.exit(1)
|
|
|
|
cmds = []
|
|
|
|
for e in sys.argv[1:]:
|
|
e = Path(e)
|
|
|
|
if e.is_dir() or e.suffix.lower() not in [".yaml", ".yml"]:
|
|
continue
|
|
|
|
assert e.is_file()
|
|
|
|
with open(e, "r") as f:
|
|
content = yaml.safe_load(f)
|
|
|
|
fileName = e.stem
|
|
|
|
eventDate = datetime.strptime(fileName[0:10], "%Y-%m-%d").strftime("%d/%m/%Y")
|
|
|
|
calendar = CALENDAR
|
|
location = content["location"]
|
|
categories = ""
|
|
startDate = eventDate
|
|
startTime = content["startTime"]
|
|
endTime = content["endTime"]
|
|
title = fileName[11:]
|
|
|
|
cmd = [
|
|
"khal",
|
|
"new",
|
|
f"--calendar={calendar}",
|
|
f"--location='{location}'",
|
|
f"--categories='{categories}'",
|
|
startDate,
|
|
startTime,
|
|
endTime,
|
|
title,
|
|
]
|
|
|
|
cmds.append(cmd)
|
|
|
|
while True:
|
|
commandList = "\n".join([" ".join(c) for c in cmds]) ## Prepare for printing
|
|
choice = input(
|
|
"Do you want to execute the following commands? YES/NO:\n" + commandList + "\n"
|
|
).lower()
|
|
if choice in ["yes", "y"]:
|
|
for c in cmds:
|
|
subprocess.call(c)
|
|
print("Done")
|
|
sys.exit(0)
|
|
elif choice in ["no", "n"]:
|
|
print("Noting to do. Exiting.")
|
|
sys.exit(0)
|
|
else:
|
|
print("Please respond with 'yes' or 'no'\n")
|