X-Git-Url: https://git.openstreetmap.org/nominatim.git/blobdiff_plain/e629a175ed0a1c54398622251f56d56baeef768f..e42349c963bfc1ef5f2db880804d7cbeff5a564f:/nominatim/config.py diff --git a/nominatim/config.py b/nominatim/config.py index 4de2052e..a8436440 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -7,7 +7,7 @@ from pathlib import Path from dotenv import dotenv_values -from .errors import UsageError +from nominatim.errors import UsageError LOG = logging.getLogger() @@ -17,7 +17,7 @@ class Configuration: Nominatim uses dotenv to configure the software. Configuration options are resolved in the following order: - * from the OS environment + * from the OS environment (or the dirctionary given in `environ` * from the .env file in the project directory of the installation * from the default installation in the configuration directory @@ -25,11 +25,12 @@ class Configuration: avoid conflicts with other environment variables. """ - def __init__(self, project_dir, config_dir): + def __init__(self, project_dir, config_dir, environ=None): + self.environ = environ or os.environ self.project_dir = project_dir self.config_dir = config_dir self._config = dotenv_values(str((config_dir / 'env.defaults').resolve())) - if project_dir is not None: + if project_dir is not None and (project_dir / '.env').is_file(): self._config.update(dotenv_values(str((project_dir / '.env').resolve()))) # Add defaults for variables that are left empty to set the default. @@ -38,11 +39,21 @@ class Configuration: self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG'] = \ str(config_dir / 'address-levels.json') + class _LibDirs: + pass + + self.lib_dir = _LibDirs() + + def set_libdirs(self, **kwargs): + """ Set paths to library functions and data. + """ + for key, value in kwargs.items(): + setattr(self.lib_dir, key, Path(value).resolve()) def __getattr__(self, name): name = 'NOMINATIM_' + name - return os.environ.get(name) or self._config[name] + return self.environ.get(name) or self._config[name] def get_bool(self, name): """ Return the given configuration parameter as a boolean. @@ -57,9 +68,9 @@ class Configuration: """ try: return int(self.__getattr__(name)) - except ValueError: + except ValueError as exp: LOG.fatal("Invalid setting NOMINATIM_%s. Needs to be a number.", name) - raise UsageError("Configuration error.") + raise UsageError("Configuration error.") from exp def get_libpq_dsn(self): @@ -100,6 +111,6 @@ class Configuration: merged in. """ env = dict(self._config) - env.update(os.environ) + env.update(self.environ) return env