58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
"""Configuration helpers."""
|
|
|
|
import argparse
|
|
import datetime
|
|
import logging
|
|
import os
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
"""Dataclass holding CLI/config settings."""
|
|
|
|
matrix_homeserver: str
|
|
matrix_user: str
|
|
|
|
tls_instance: str
|
|
tls_user: str
|
|
|
|
credentials_dir: Path
|
|
latest_appointment: datetime.datetime
|
|
|
|
@classmethod
|
|
def from_args(cls):
|
|
"""Create a config instance from CLI arguments."""
|
|
parser = argparse.ArgumentParser(
|
|
description="Monitor TLSContact for new appointments and notify via matrix."
|
|
)
|
|
|
|
parser.add_argument("matrix_homeserver")
|
|
parser.add_argument("matrix_user")
|
|
|
|
parser.add_argument("tls_instance") # https://nl.tlscontact.com/cn
|
|
parser.add_argument("tls_user")
|
|
|
|
parser.add_argument("--credentials-dir", type=Path, default=None)
|
|
parser.add_argument("--latest-appointment", default=None)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.credentials_dir is None:
|
|
if os.getenv("CREDENTIALS_DIRECTORY"):
|
|
args.credentials_dir = Path(os.getenv("CREDENTIALS_DIRECTORY"))
|
|
else:
|
|
logger.critical("missing credentials directory")
|
|
sys.exit(1)
|
|
|
|
if args.latest_appointment is not None:
|
|
args.latest_appointment = datetime.datetime.strptime(
|
|
args.latest_appointment, "%Y-%m-%d"
|
|
)
|
|
|
|
return cls(**vars(args))
|