-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from lonvia/more-cmdline-options
Make location of config file configurable on the command line
- Loading branch information
Showing
20 changed files
with
143 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
include src/nominatim_data_analyser/rules_specifications/*.yaml | ||
include src/nominatim_data_analyser/config/default.yaml | ||
include src/nominatim_data_analyser/default_config.yaml | ||
include src/nominatim_data_analyser/py.typed | ||
recursive-include contrib * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
from .logger.logger import LOG | ||
|
||
|
||
class Config: | ||
values: dict[str, str] = {} | ||
|
||
|
||
def load_config(config_file: Path | None) -> None: | ||
""" | ||
Load the YAML config file into the | ||
config global variable. | ||
""" | ||
Config.values.clear() | ||
|
||
# First load the default settings. | ||
_get_config_file_contents(Path(__file__, '..', 'default_config.yaml').resolve()) | ||
|
||
# Then overwrite with potential custom settings. | ||
if config_file is not None and config_file.is_file(): | ||
LOG.info(f"Loading config from {config_file}.") | ||
_get_config_file_contents(config_file) | ||
|
||
|
||
def _get_config_file_contents(config_file: Path) -> None: | ||
with config_file.open('r') as file: | ||
try: | ||
contents = yaml.safe_load(file) | ||
except yaml.YAMLError as exc: | ||
LOG.error(f"Error while loading the config file: {exc}") | ||
raise | ||
|
||
if not isinstance(contents, dict): | ||
raise RuntimeError('Error in config file, expected key-value entries.') | ||
|
||
for k, v in contents.items(): | ||
if not isinstance(k, str): | ||
raise RuntimeError(f"Error in config file, non-string key {k}.") | ||
Config.values[k] = str(v) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Data source name for the database connection. | ||
Dsn: 'dbname=nominatim' | ||
|
||
# Path to the folder where rules data are stored (geojson, vector tiles, etc). | ||
RulesFolderPath: 'qa-data' | ||
|
||
# Prefix path of the web URL to access the rules data (ex: https://nominatim.org/QA-data). | ||
WebPrefixPath: '' | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
|
||
from nominatim_data_analyser.config import Config | ||
from pathlib import Path | ||
import pytest | ||
import yaml | ||
|
||
def test_load_default_config(config: Config) -> None: | ||
from nominatim_data_analyser.config import Config, load_config | ||
|
||
def test_load_default_config() -> None: | ||
""" | ||
Test the load_config() method. The default config should be | ||
returned because no config.yaml file is present in the | ||
default_config folder used as the config_folder_path. | ||
""" | ||
config.load_config(Path(__file__).parent / 'default_config') | ||
assert config.values == {'Dsn': 'default_dsn'} | ||
load_config(None) | ||
assert Config.values['Dsn'] == 'dbname=nominatim' | ||
assert Config.values['RulesFolderPath'] == 'qa-data' | ||
|
||
def test_load_custom_config(config: Config) -> None: | ||
def test_load_custom_config(tmp_path) -> None: | ||
""" | ||
Test the load_config() method. The custom config should be | ||
returned because one config.yaml file is present in the | ||
custom_config folder used as the config_folder_path. | ||
""" | ||
config.load_config(Path(__file__).parent / 'custom_config') | ||
assert config.values == {'Dsn': 'custom_dsn'} | ||
cfgfile = tmp_path / 'myconfig.yaml' | ||
cfgfile.write_text("Dsn: 'custom_dsn'") | ||
|
||
load_config(cfgfile) | ||
|
||
def test_load_broken_config(config: Config) -> None: | ||
assert Config.values['Dsn'] == 'custom_dsn' | ||
assert Config.values['RulesFolderPath'] == 'qa-data' | ||
|
||
def test_load_broken_config(tmp_path) -> None: | ||
""" | ||
Test the load_config() method. A YAMLError exception should | ||
be raised as the config file has a wrong syntax. | ||
""" | ||
cfgfile = tmp_path / 'myconfig.yaml' | ||
cfgfile.write_text(">>>>>>>>Dsn: 'custom_dsn'") | ||
|
||
with pytest.raises(yaml.YAMLError): | ||
config.load_config(Path(__file__).parent / 'broken_config') | ||
load_config(cfgfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.