From cca514a2bad1a8a95c2b1bab12a3e96b4e220b74 Mon Sep 17 00:00:00 2001 From: Pim Nelissen Date: Fri, 20 Feb 2026 11:47:38 +0100 Subject: [PATCH] add MissingSubKeyError for config loading. update main.py for --config flag --- src/pg_rad/exceptions/exceptions.py | 11 ++++++++ src/pg_rad/main.py | 40 ++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/pg_rad/exceptions/exceptions.py b/src/pg_rad/exceptions/exceptions.py index dc8c3eb..37355aa 100644 --- a/src/pg_rad/exceptions/exceptions.py +++ b/src/pg_rad/exceptions/exceptions.py @@ -12,3 +12,14 @@ class InvalidCSVError(DataLoadError): class OutOfBoundsError(Exception): """Raised when an object is attempted to be placed out of bounds.""" + + +class MissingNestedKeyError(Exception): + """Raised when a nested key is missing in the config.""" + def __init__(self, key, subkey=None): + if subkey: + self.message = f"Missing key in {key}: {subkey}" + else: + self.message = f"Missing key: {key}" + + super().__init__(self.message) diff --git a/src/pg_rad/main.py b/src/pg_rad/main.py index 7d24b6f..2929cbc 100644 --- a/src/pg_rad/main.py +++ b/src/pg_rad/main.py @@ -1,8 +1,14 @@ import argparse +import logging +import sys -from pg_rad.logger import setup_logger -from pg_rad.landscape import LandscapeDirector -from pg_rad.plotting import LandscapeSlicePlotter +from pandas.errors import ParserError +from yaml import YAMLError + +from pg_rad.exceptions.exceptions import MissingNestedKeyError +from pg_rad.logger.logger import setup_logger +from pg_rad.landscape.director import LandscapeDirector +from pg_rad.plotting.landscape_plotter import LandscapeSlicePlotter def main(): @@ -10,7 +16,10 @@ def main(): prog="pg-rad", description="Primary Gamma RADiation landscape tool" ) - + parser.add_argument( + "--config", + help="Build from a config file." + ) parser.add_argument( "--test", action="store_true", @@ -29,12 +38,35 @@ def main(): args = parser.parse_args() setup_logger(args.loglevel) + logger = logging.getLogger(__name__) if args.test: landscape = LandscapeDirector().build_test_landscape() plotter = LandscapeSlicePlotter() plotter.plot(landscape, save=args.saveplot) + elif args.config: + try: + landscape = LandscapeDirector.build_from_config(args.config) + plotter = LandscapeSlicePlotter() + plotter.plot(landscape, save=args.saveplot) + except ( + MissingNestedKeyError, + KeyError, + YAMLError + ): + logger.critical( + "The provided config file is invalid. " + "Check the log above. You can consult the documentation for " + "an explanation of how to define a config file." + ) + sys.exit(1) + except ( + FileNotFoundError, + ParserError + ): + sys.exit(1) + if __name__ == "__main__": main()