From f3a92236048f120786f6c33a54be160a44dd7e9b Mon Sep 17 00:00:00 2001 From: Piotr Szymajda Date: Sun, 14 Jul 2024 21:51:11 +0200 Subject: [PATCH] Gracefully handle missing configuration --- data_processor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data_processor.py b/data_processor.py index c93192c..167a040 100644 --- a/data_processor.py +++ b/data_processor.py @@ -1,3 +1,4 @@ +import os import csv import yaml @@ -11,7 +12,7 @@ from util import print_err, print_wrn, print_note RE_RETRIEVE_RATIO = 0.8 # 80% of cumulated energy sent to the grid - +CONFIG_FILE_PATH = "config.yml" # From python3.11 'StrEnum' can be used class DataTypes(str, Enum): @@ -190,7 +191,10 @@ def load_cache() -> list[DataPoint]: def load_config() -> dict[str, Any]: - with open("config.yml", 'r') as config_file: + if not os.path.isfile(CONFIG_FILE_PATH): + print_err(f"Configuration file ({CONFIG_FILE_PATH}) not found!") + + with open(CONFIG_FILE_PATH, 'r') as config_file: try: config = yaml.safe_load(config_file) except yaml.YAMLError as e: