diff --git a/notebooks/dev/debugging/config_routing.ipynb b/notebooks/dev/debugging/config_routing.ipynb index eb7ec0dc..38aa4c78 100644 --- a/notebooks/dev/debugging/config_routing.ipynb +++ b/notebooks/dev/debugging/config_routing.ipynb @@ -8,7 +8,13 @@ "outputs": [], "source": [ "import os\n", - "from resurfemg.data_connector.config import Config" + "import logging\n", + "from resurfemg.data_connector.config import Config\n", + "\n", + "logging.basicConfig(\n", + " level=logging.INFO,\n", + " format=\"%(message)s\"\n", + ")" ] }, { @@ -45,6 +51,16 @@ "config.get_directory('non_existing_key')" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca933411", + "metadata": {}, + "outputs": [], + "source": [ + "config.get_directory('simulated_data')" + ] + }, { "cell_type": "code", "execution_count": null, @@ -66,7 +82,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv_win312", + "display_name": ".venv_312_vogue (3.12.1)", "language": "python", "name": "python3" }, @@ -80,7 +96,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.6" + "version": "3.12.1" } }, "nbformat": 4, diff --git a/resurfemg/data_connector/config.py b/resurfemg/data_connector/config.py index c985f840..c9a3c48d 100644 --- a/resurfemg/data_connector/config.py +++ b/resurfemg/data_connector/config.py @@ -141,12 +141,10 @@ def __init__(self, location=None, configure=False, verbose=False, self.default_layout = self.get_default_layout() if configure: location = self.setup_config(location=location, force=force) - _path = self.load(location) + _path = self.load(location, verbose=verbose) self.parse_paths(_path) if configure: print(f'Created config. See and edit it at:\n {_path}\n') - elif verbose: - print(f'Loaded config from:\n {_path}\n') if verbose or configure: print('The following name-path combinations are configured:') self.print_config() @@ -252,7 +250,7 @@ def get_default_layout(self): } return default_layout - def load(self, location): + def load(self, location, verbose=False): """ This function loads the configuration file. If no location is specified it will try to load the configuration file from the default locations: @@ -270,16 +268,28 @@ def load(self, location): locations = ( [location] if location is not None else self.default_locations ) - + _failed_paths = [] for _path in locations: try: with open(_path) as f: self._raw = json.load(f) + if verbose: + msg = f'Loaded config file from:\n{_path}' + logging.info(msg) break except Exception as e: - logging.info( - 'Failed to load config file from %s: %s', _path, e) + if isinstance(e, FileNotFoundError): + _failed_paths.append(_path) + else: + raise e else: + msg = ( + 'Config file not found in any of the following locations:\n' + + '\n'.join(_failed_paths) + + '\n\n' + + self.usage() + ) + logging.error(msg) raise ValueError('Config file not found.' + self.usage()) return _path