1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Nominatim configuration accessor.
10 from typing import Dict, Any, List, Mapping, Optional
15 from pathlib import Path
19 from dotenv import dotenv_values
22 from psycopg2.extensions import parse_dsn
23 except ModuleNotFoundError:
24 from psycopg.conninfo import conninfo_to_dict as parse_dsn # type: ignore[assignment]
26 from .typing import StrPath
27 from .errors import UsageError
30 LOG = logging.getLogger()
31 CONFIG_CACHE : Dict[str, Any] = {}
33 def flatten_config_list(content: Any, section: str = '') -> List[Any]:
34 """ Flatten YAML configuration lists that contain include sections
35 which are lists themselves.
40 if not isinstance(content, list):
41 raise UsageError(f"List expected in section '{section}'.")
45 if isinstance(ele, list):
46 output.extend(flatten_config_list(ele, section))
54 """ This class wraps access to the configuration settings
55 for the Nominatim instance in use.
57 All Nominatim configuration options are prefixed with 'NOMINATIM_' to
58 avoid conflicts with other environment variables. All settings can
59 be accessed as properties of the class under the same name as the
60 setting but with the `NOMINATIM_` prefix removed. In addition, there
61 are accessor functions that convert the setting values to types
65 def __init__(self, project_dir: Optional[Path],
66 environ: Optional[Mapping[str, str]] = None) -> None:
67 self.environ = environ or os.environ
68 self.project_dir = project_dir
69 self.config_dir = paths.CONFIG_DIR
70 self._config = dotenv_values(str(self.config_dir / 'env.defaults'))
71 if self.project_dir is not None and (self.project_dir / '.env').is_file():
72 self.project_dir = self.project_dir.resolve()
73 self._config.update(dotenv_values(str(self.project_dir / '.env')))
78 php = paths.PHPLIB_DIR
79 sql = paths.SQLLIB_DIR
82 self.lib_dir = _LibDirs()
83 self._private_plugins: Dict[str, object] = {}
86 def set_libdirs(self, **kwargs: StrPath) -> None:
87 """ Set paths to library functions and data.
89 for key, value in kwargs.items():
90 setattr(self.lib_dir, key, None if value is None else Path(value))
93 def __getattr__(self, name: str) -> str:
94 name = 'NOMINATIM_' + name
96 if name in self.environ:
97 return self.environ[name]
99 return self._config[name] or ''
102 def get_bool(self, name: str) -> bool:
103 """ Return the given configuration parameter as a boolean.
106 name: Name of the configuration parameter with the NOMINATIM_
110 `True` for values of '1', 'yes' and 'true', `False` otherwise.
112 return getattr(self, name).lower() in ('1', 'yes', 'true')
115 def get_int(self, name: str) -> int:
116 """ Return the given configuration parameter as an int.
119 name: Name of the configuration parameter with the NOMINATIM_
123 The configuration value converted to int.
126 ValueError: when the value is not a number.
129 return int(getattr(self, name))
130 except ValueError as exp:
131 LOG.fatal("Invalid setting NOMINATIM_%s. Needs to be a number.", name)
132 raise UsageError("Configuration error.") from exp
135 def get_str_list(self, name: str) -> Optional[List[str]]:
136 """ Return the given configuration parameter as a list of strings.
137 The values are assumed to be given as a comma-sparated list and
138 will be stripped before returning them.
141 name: Name of the configuration parameter with the NOMINATIM_
145 (List[str]): The comma-split parameter as a list. The
146 elements are stripped of leading and final spaces before
148 (None): The configuration parameter was unset or empty.
150 raw = getattr(self, name)
152 return [v.strip() for v in raw.split(',')] if raw else None
155 def get_path(self, name: str) -> Optional[Path]:
156 """ Return the given configuration parameter as a Path.
159 name: Name of the configuration parameter with the NOMINATIM_
163 (Path): A Path object of the parameter value.
164 If a relative path is configured, then the function converts this
165 into an absolute path with the project directory as root path.
166 (None): The configuration parameter was unset or empty.
168 value = getattr(self, name)
172 cfgpath = Path(value)
174 if not cfgpath.is_absolute():
175 assert self.project_dir is not None
176 cfgpath = self.project_dir / cfgpath
178 return cfgpath.resolve()
181 def get_libpq_dsn(self) -> str:
182 """ Get configured database DSN converted into the key/value format
183 understood by libpq and psycopg.
185 dsn = self.DATABASE_DSN
187 def quote_param(param: str) -> str:
188 key, val = param.split('=')
189 val = val.replace('\\', '\\\\').replace("'", "\\'")
191 val = "'" + val + "'"
192 return key + '=' + val
194 if dsn.startswith('pgsql:'):
195 # Old PHP DSN format. Convert before returning.
196 return ' '.join([quote_param(p) for p in dsn[6:].split(';')])
201 def get_database_params(self) -> Mapping[str, str]:
202 """ Get the configured parameters for the database connection
205 dsn = self.DATABASE_DSN
207 if dsn.startswith('pgsql:'):
208 return dict((p.split('=', 1) for p in dsn[6:].split(';')))
210 return parse_dsn(dsn)
213 def get_import_style_file(self) -> Path:
214 """ Return the import style file as a path object. Translates the
215 name of the standard styles automatically into a file in the
218 style = getattr(self, 'IMPORT_STYLE')
220 if style in ('admin', 'street', 'address', 'full', 'extratags'):
221 return self.config_dir / f'import-{style}.lua'
223 return self.find_config_file('', 'IMPORT_STYLE')
226 def get_os_env(self) -> Dict[str, str]:
227 """ Return a copy of the OS environment with the Nominatim configuration
230 env = {k: v for k, v in self._config.items() if v is not None}
231 env.update(self.environ)
236 def load_sub_configuration(self, filename: StrPath,
237 config: Optional[str] = None) -> Any:
238 """ Load additional configuration from a file. `filename` is the name
239 of the configuration file. The file is first searched in the
240 project directory and then in the global settings directory.
242 If `config` is set, then the name of the configuration file can
243 be additionally given through a .env configuration option. When
244 the option is set, then the file will be exclusively loaded as set:
245 if the name is an absolute path, the file name is taken as is,
246 if the name is relative, it is taken to be relative to the
249 The format of the file is determined from the filename suffix.
250 Currently only files with extension '.yaml' are supported.
252 YAML files support a special '!include' construct. When the
253 directive is given, the value is taken to be a filename, the file
254 is loaded using this function and added at the position in the
257 configfile = self.find_config_file(filename, config)
259 if str(configfile) in CONFIG_CACHE:
260 return CONFIG_CACHE[str(configfile)]
262 if configfile.suffix in ('.yaml', '.yml'):
263 result = self._load_from_yaml(configfile)
264 elif configfile.suffix == '.json':
265 with configfile.open('r', encoding='utf-8') as cfg:
266 result = json.load(cfg)
268 raise UsageError(f"Config file '{configfile}' has unknown format.")
270 CONFIG_CACHE[str(configfile)] = result
274 def load_plugin_module(self, module_name: str, internal_path: str) -> Any:
275 """ Load a Python module as a plugin.
277 The module_name may have three variants:
279 * A name without any '.' is assumed to be an internal module
280 and will be searched relative to `internal_path`.
281 * If the name ends in `.py`, module_name is assumed to be a
282 file name relative to the project directory.
283 * Any other name is assumed to be an absolute module name.
285 In either of the variants the module name must start with a letter.
287 if not module_name or not module_name[0].isidentifier():
288 raise UsageError(f'Invalid module name {module_name}')
290 if '.' not in module_name:
291 module_name = module_name.replace('-', '_')
292 full_module = f'{internal_path}.{module_name}'
293 return sys.modules.get(full_module) or importlib.import_module(full_module)
295 if module_name.endswith('.py'):
296 if self.project_dir is None or not (self.project_dir / module_name).exists():
297 raise UsageError(f"Cannot find module '{module_name}' in project directory.")
299 if module_name in self._private_plugins:
300 return self._private_plugins[module_name]
302 file_path = str(self.project_dir / module_name)
303 spec = importlib.util.spec_from_file_location(module_name, file_path)
305 module = importlib.util.module_from_spec(spec)
306 # Do not add to global modules because there is no standard
307 # module name that Python can resolve.
308 self._private_plugins[module_name] = module
309 assert spec.loader is not None
310 spec.loader.exec_module(module)
314 return sys.modules.get(module_name) or importlib.import_module(module_name)
317 def find_config_file(self, filename: StrPath,
318 config: Optional[str] = None) -> Path:
319 """ Resolve the location of a configuration file given a filename and
320 an optional configuration option with the file name.
321 Raises a UsageError when the file cannot be found or is not
324 if config is not None:
325 cfg_value = getattr(self, config)
327 cfg_filename = Path(cfg_value)
329 if cfg_filename.is_absolute():
330 cfg_filename = cfg_filename.resolve()
332 if not cfg_filename.is_file():
333 LOG.fatal("Cannot find config file '%s'.", cfg_filename)
334 raise UsageError("Config file not found.")
338 filename = cfg_filename
341 search_paths = [self.project_dir, self.config_dir]
342 for path in search_paths:
343 if path is not None and (path / filename).is_file():
344 return path / filename
346 LOG.fatal("Configuration file '%s' not found.\nDirectories searched: %s",
347 filename, search_paths)
348 raise UsageError("Config file not found.")
351 def _load_from_yaml(self, cfgfile: Path) -> Any:
352 """ Load a YAML configuration file. This installs a special handler that
353 allows to include other YAML files using the '!include' operator.
355 yaml.add_constructor('!include', self._yaml_include_representer,
356 Loader=yaml.SafeLoader)
357 return yaml.safe_load(cfgfile.read_text(encoding='utf-8'))
360 def _yaml_include_representer(self, loader: Any, node: yaml.Node) -> Any:
361 """ Handler for the '!include' operator in YAML files.
363 When the filename is relative, then the file is first searched in the
364 project directory and then in the global settings directory.
366 fname = loader.construct_scalar(node)
368 if Path(fname).is_absolute():
369 configfile = Path(fname)
371 configfile = self.find_config_file(loader.construct_scalar(node))
373 if configfile.suffix != '.yaml':
374 LOG.fatal("Format error while reading '%s': only YAML format supported.",
376 raise UsageError("Cannot handle config file format.")
378 return yaml.safe_load(configfile.read_text(encoding='utf-8'))