]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/config.py
convert functon creation to python
[nominatim.git] / nominatim / config.py
index 911c7ddf127f68f438ab582c75fa38d5ed469dd8..3f75ce33eecb5e46839984a3df1f700ba0a51fc4 100644 (file)
@@ -20,15 +20,42 @@ class Configuration:
     """
 
     def __init__(self, project_dir, config_dir):
+        self.project_dir = project_dir
         self._config = dotenv_values(str((config_dir / 'env.defaults').resolve()))
         if project_dir is not None:
             self._config.update(dotenv_values(str((project_dir / '.env').resolve())))
 
+        # Add defaults for variables that are left empty to set the default.
+        # They may still be overwritten by environment variables.
+        if not self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG']:
+            self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG'] = \
+                str(config_dir / 'address-levels.json')
+
+
     def __getattr__(self, name):
         name = 'NOMINATIM_' + name
 
         return os.environ.get(name) or self._config[name]
 
+    def get_bool(self, name):
+        """ Return the given configuration parameters as a boolean.
+            Values of '1', 'yes' and 'true' are accepted as truthy values,
+            everything else is interpreted as false.
+        """
+        return self.__getattr__(name).lower() in ('1', 'yes', 'true')
+
+    def get_libpq_dsn(self):
+        """ Get configured database DSN converted into the key/value format
+            understood by libpq and psycopg.
+        """
+        dsn = self.DATABASE_DSN
+
+        if dsn.startswith('pgsql:'):
+            # Old PHP DSN format. Convert before returning.
+            return dsn[6:].replace(';', ' ')
+
+        return dsn
+
     def get_os_env(self):
         """ Return a copy of the OS environment with the Nominatim configuration
             merged in.