2 Nominatim configuration accessor.
6 from dotenv import dotenv_values
9 """ Load and manage the project configuration.
11 Nominatim uses dotenv to configure the software. Configuration options
12 are resolved in the following order:
14 * from the OS environment
15 * from the .env file in the project directory of the installation
16 * from the default installation in the configuration directory
18 All Nominatim configuration options are prefixed with 'NOMINATIM_' to
19 avoid conflicts with other environment variables.
22 def __init__(self, project_dir, config_dir):
23 self.project_dir = project_dir
24 self._config = dotenv_values(str((config_dir / 'env.defaults').resolve()))
25 if project_dir is not None:
26 self._config.update(dotenv_values(str((project_dir / '.env').resolve())))
28 # Add defaults for variables that are left empty to set the default.
29 # They may still be overwritten by environment variables.
30 if not self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG']:
31 self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG'] = \
32 str(config_dir / 'address-levels.json')
35 def __getattr__(self, name):
36 name = 'NOMINATIM_' + name
38 return os.environ.get(name) or self._config[name]
40 def get_bool(self, name):
41 """ Return the given configuration parameters as a boolean.
42 Values of '1', 'yes' and 'true' are accepted as truthy values,
43 everything else is interpreted as false.
45 return self.__getattr__(name).lower() in ('1', 'yes', 'true')
47 def get_libpq_dsn(self):
48 """ Get configured database DSN converted into the key/value format
49 understood by libpq and psycopg.
51 dsn = self.DATABASE_DSN
53 if dsn.startswith('pgsql:'):
54 # Old PHP DSN format. Convert before returning.
55 return dsn[6:].replace(';', ' ')
60 """ Return a copy of the OS environment with the Nominatim configuration
63 env = dict(self._config)
64 env.update(os.environ)