#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2024 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Helper script for development to run nominatim from the source directory.
from nominatim_db import cli
-exit(cli.nominatim(module_dir=None, osm2pgsql_path=None))
+exit(cli.nominatim())
from nominatim_db import cli
-exit(cli.nominatim(osm2pgsql_path=None))
+exit(cli.nominatim())
#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2024 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Command-line interface to the Nominatim functions for import, update,
database administration and querying.
"""
-from typing import Optional, Any
+from typing import Optional, List, Mapping
import importlib
import logging
-import os
import sys
import argparse
import asyncio
parser.set_defaults(command=cmd)
cmd.add_args(parser)
- def run(self, **kwargs: Any) -> int:
+ def run(self, cli_args: Optional[List[str]],
+ environ: Optional[Mapping[str, str]]) -> int:
""" Parse the command line arguments of the program and execute the
appropriate subcommand.
"""
args = NominatimArgs()
try:
- self.parser.parse_args(args=kwargs.get('cli_args'), namespace=args)
+ self.parser.parse_args(args=cli_args, namespace=args)
except SystemExit:
return 1
args.project_dir = Path(args.project_dir).resolve()
- if 'cli_args' not in kwargs:
+ if cli_args is None:
logging.basicConfig(stream=sys.stderr,
format='%(asctime)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=max(4 - args.verbose, 1) * 10)
- args.config = Configuration(args.project_dir,
- environ=kwargs.get('environ', os.environ))
- args.config.set_libdirs(osm2pgsql=kwargs['osm2pgsql_path'])
+ args.config = Configuration(args.project_dir, environ=environ)
log = logging.getLogger()
log.warning('Using project directory: %s', str(args.project_dir))
try:
- ret = args.command.run(args)
-
- return ret
+ return args.command.run(args)
except UsageError as exception:
if log.isEnabledFor(logging.DEBUG):
raise # use Python's exception printing
return parser
-def nominatim(**kwargs: Any) -> int:
+def nominatim(cli_args: Optional[List[str]] = None,
+ environ: Optional[Mapping[str, str]] = None) -> int:
"""\
Command-line tools for importing, updating, administrating and
querying the Nominatim database.
+
+ 'cli_args' is a list of parameters for the command to run. If not given,
+ sys.args will be used.
+
+ 'environ' is the dictionary of environment variables containing the
+ Nominatim configuration. When None, the os.environ is inherited.
"""
- return get_set_parser().run(**kwargs)
+ return get_set_parser().run(cli_args=cli_args, environ=environ)
#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2024 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Provides custom functions over command-line arguments.
from the command line arguments. The resulting dict can be
further customized and then used in `run_osm2pgsql()`.
"""
- return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY or self.config.lib_dir.osm2pgsql,
+ return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY,
osm2pgsql_cache=self.osm2pgsql_cache or default_cache,
osm2pgsql_style=self.config.get_import_style_file(),
osm2pgsql_style_path=self.config.lib_dir.lua,
#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2024 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Nominatim configuration accessor.
self.project_dir = None
class _LibDirs:
- osm2pgsql: Path
sql = paths.SQLLIB_DIR
lua = paths.LUALIB_DIR
data = paths.DATA_DIR
#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2024 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Helper functions for executing external programs.
def _find_osm2pgsql_cmd(cmdline: Optional[str]) -> str:
- if cmdline is not None:
+ if cmdline:
return cmdline
in_path = shutil.which('osm2pgsql')
if self.website_dir is not None:
cmdline = list(cmdline) + ['--project-dir', self.website_dir.name]
- cli.nominatim(osm2pgsql_path=None,
- cli_args=cmdline,
+ cli.nominatim(cli_args=cmdline,
environ=self.test_env)
def copy_from_place(self, db):
def run_export(tmp_path, capsys):
def _exec(args):
cli_args = ['export', '--project-dir', str(tmp_path)] + args
- assert 0 == nominatim_db.cli.nominatim(osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
- cli_args=cli_args)
+ assert 0 == nominatim_db.cli.nominatim(cli_args=cli_args)
return capsys.readouterr().out.split('\r\n')
return _exec
@pytest.mark.parametrize('args', [['--search-only'], ['--reverse-only']])
def test_warm_all(tmp_path, args):
- assert 0 == nominatim_db.cli.nominatim(osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
- cli_args=['admin', '--project-dir', str(tmp_path),
+ assert 0 == nominatim_db.cli.nominatim(cli_args=['admin', '--project-dir', str(tmp_path),
'--warm'] + args)
Returns a function that can be called with the desired CLI arguments.
"""
def _call_nominatim(*args):
- return nominatim_db.cli.nominatim(osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
- cli_args=args)
+ return nominatim_db.cli.nominatim(cli_args=args)
return _call_nominatim
#
# This file is part of Nominatim. (https://nominatim.org)
#
-# Copyright (C) 2023 by the Nominatim developer community.
+# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Tests for replication command of command-line interface wrapper.
def test_replication_update_continuous_no_index(self):
assert self.call_nominatim('--no-index') == 1
- def test_replication_update_once_no_index(self, update_mock):
+ def test_replication_update_once_no_index(self, update_mock, monkeypatch):
+ monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'OSM2PGSQL NOT AVAILABLE')
assert self.call_nominatim('--once', '--no-index') == 0
assert str(update_mock.last_args[1]['osm2pgsql']).endswith('OSM2PGSQL NOT AVAILABLE')
@pytest.fixture
def def_config():
cfg = Configuration(None)
- cfg.set_libdirs(osm2pgsql=None)
return cfg
projdir = tmp_path / 'project'
projdir.mkdir()
cfg = Configuration(projdir)
- cfg.set_libdirs(osm2pgsql=None)
return cfg
def sql_preprocessor_cfg(tmp_path, table_factory, temp_db_with_extensions):
table_factory('country_name', 'partition INT', ((0, ), (1, ), (2, )))
cfg = Configuration(None)
- cfg.set_libdirs(osm2pgsql=None, sql=tmp_path)
+ cfg.set_libdirs(sql=tmp_path)
return cfg