From 381dcaa0bcb6a71f62f7ccafd4c664a75651131f Mon Sep 17 00:00:00 2001 From: Christos Falas Date: Mon, 27 Jan 2025 16:16:40 +0000 Subject: [PATCH] [BROKEN] Add CLI option for option This is still slightly broken, as new processes are not able to access the configuration. --- where_fi/cli/globals.py | 18 ++++++++++++++++-- where_fi/config/__init__.py | 32 +++++++++++++++++++------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/where_fi/cli/globals.py b/where_fi/cli/globals.py index cf5f0b9..e45161c 100644 --- a/where_fi/cli/globals.py +++ b/where_fi/cli/globals.py @@ -1,18 +1,32 @@ from functools import partial from pathlib import Path +from typing import Annotated + +import typer from .. import collection +from .. import config as config_mod from ..collection import file, ingest csi_producer: collection.CSIProducer = collection.noop is_live = True -def main(from_file: Path | None = None) -> None: - global csi_producer, is_live +def main( + from_file: Annotated[ + Path | None, + typer.Option(help="Read CSI data from file. Uses live data if not specified"), + ] = None, + config: Annotated[ + Path, typer.Option(help="Specify path to config file", exists=True) + ] = Path("config.yaml"), +) -> None: + global csi_producer, is_live, config_path if from_file: csi_producer = partial(file.start_processing, file_path=from_file) is_live = False else: csi_producer = ingest.start_processing is_live = True + + config_mod.load(config) diff --git a/where_fi/config/__init__.py b/where_fi/config/__init__.py index 4bcf572..fc98236 100644 --- a/where_fi/config/__init__.py +++ b/where_fi/config/__init__.py @@ -1,5 +1,6 @@ import logging import sys +from pathlib import Path import pydantic import yaml @@ -8,17 +9,22 @@ from . import models logger = logging.getLogger(__name__) -try: - config = yaml.safe_load(open("config.yaml")) -except FileNotFoundError: - logger.error( - "No config.yaml file found. Make sure to copy the " - "config.example.yaml file to config.yaml" - ) - sys.exit(1) +config: models.Config | None = None -try: - config = models.Config(**config) -except pydantic.ValidationError as e: - logger.error(f"Invalid config.yaml file: {e}") - sys.exit(1) + +def load(path: Path) -> None: + global config + try: + config_yaml = yaml.safe_load(open(path)) + except FileNotFoundError: + logger.error( + "No config.yaml file found. Make sure to copy the " + "config.example.yaml file to config.yaml" + ) + sys.exit(1) + + try: + config = models.Config(**config_yaml) + except pydantic.ValidationError as e: + logger.error(f"Invalid config.yaml file: {e}") + sys.exit(1)