]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/config.py
prepare 3.7.3 release
[nominatim.git] / nominatim / config.py
index 271d2d4d4ee20b0d37fde49de1429b5b9da7f0fc..6408b5e930c024d753709ad7d59e1b9c5ce7deae 100644 (file)
@@ -7,6 +7,8 @@ from pathlib import Path
 
 from dotenv import dotenv_values
 
+from .errors import UsageError
+
 LOG = logging.getLogger()
 
 class Configuration:
@@ -15,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
 
@@ -23,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.
@@ -40,7 +43,7 @@ class Configuration:
     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,7 +60,7 @@ class Configuration:
             return int(self.__getattr__(name))
         except ValueError:
             LOG.fatal("Invalid setting NOMINATIM_%s. Needs to be a number.", name)
-            raise
+            raise UsageError("Configuration error.")
 
 
     def get_libpq_dsn(self):
@@ -98,6 +101,6 @@ class Configuration:
             merged in.
         """
         env = dict(self._config)
-        env.update(os.environ)
+        env.update(self.environ)
 
         return env