2 Nominatim configuration accessor.
6 from pathlib import Path
8 from dotenv import dotenv_values
10 LOG = logging.getLogger()
13 """ Load and manage the project configuration.
15 Nominatim uses dotenv to configure the software. Configuration options
16 are resolved in the following order:
18 * from the OS environment
19 * from the .env file in the project directory of the installation
20 * from the default installation in the configuration directory
22 All Nominatim configuration options are prefixed with 'NOMINATIM_' to
23 avoid conflicts with other environment variables.
26 def __init__(self, project_dir, config_dir):
27 self.project_dir = project_dir
28 self.config_dir = config_dir
29 self._config = dotenv_values(str((config_dir / 'env.defaults').resolve()))
30 if project_dir is not None:
31 self._config.update(dotenv_values(str((project_dir / '.env').resolve())))
33 # Add defaults for variables that are left empty to set the default.
34 # They may still be overwritten by environment variables.
35 if not self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG']:
36 self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG'] = \
37 str(config_dir / 'address-levels.json')
40 def __getattr__(self, name):
41 name = 'NOMINATIM_' + name
43 return os.environ.get(name) or self._config[name]
45 def get_bool(self, name):
46 """ Return the given configuration parameter as a boolean.
47 Values of '1', 'yes' and 'true' are accepted as truthy values,
48 everything else is interpreted as false.
50 return self.__getattr__(name).lower() in ('1', 'yes', 'true')
53 def get_int(self, name):
54 """ Return the given configuration parameter as an int.
57 return int(self.__getattr__(name))
59 LOG.fatal("Invalid setting NOMINATIM_%s. Needs to be a number.", name)
63 def get_libpq_dsn(self):
64 """ Get configured database DSN converted into the key/value format
65 understood by libpq and psycopg.
67 dsn = self.DATABASE_DSN
69 def quote_param(param):
70 key, val = param.split('=')
71 val = val.replace('\\', '\\\\').replace("'", "\\'")
74 return key + '=' + val
76 if dsn.startswith('pgsql:'):
77 # Old PHP DSN format. Convert before returning.
78 return ' '.join([quote_param(p) for p in dsn[6:].split(';')])
83 def get_import_style_file(self):
84 """ Return the import style file as a path object. Translates the
85 name of the standard styles automatically into a file in the
88 style = self.__getattr__('IMPORT_STYLE')
90 if style in ('admin', 'street', 'address', 'full', 'extratags'):
91 return self.config_dir / 'import-{}.style'.format(style)
97 """ Return a copy of the OS environment with the Nominatim configuration
100 env = dict(self._config)
101 env.update(os.environ)