Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions notebooks/dev/debugging/config_routing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
")"
]
},
{
Expand Down Expand Up @@ -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,
Expand All @@ -66,7 +82,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv_win312",
"display_name": ".venv_312_vogue (3.12.1)",
"language": "python",
"name": "python3"
},
Expand All @@ -80,7 +96,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
24 changes: 17 additions & 7 deletions resurfemg/data_connector/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down