From: Sarah Hoffmann Date: Thu, 21 Jan 2021 09:19:38 +0000 (+0100) Subject: cli: import python modules for commands on demand X-Git-Tag: v3.7.0~46^2~12 X-Git-Url: https://git.openstreetmap.org/nominatim.git/commitdiff_plain/e6d9485c4ae566896f4e9e474d7d3588bc7b4b25 cli: import python modules for commands on demand Given that only one command will be executed in the end, it is not necessary to import what amounts to the whole library. This becomes in particular important for update functions that have a dependency on pyosmium. The dependency can remain optional for people not using updates. --- diff --git a/nominatim/cli.py b/nominatim/cli.py index 6c110ce7..c558eb84 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -11,8 +11,6 @@ from pathlib import Path from .config import Configuration from .tools.exec_utils import run_legacy_script, run_api_script -from .indexer.indexer import Indexer - def _num_system_cpus(): try: cpus = len(os.sched_getaffinity(0)) @@ -320,6 +318,8 @@ class UpdateIndex: @staticmethod def run(args): + from .indexer.indexer import Indexer + indexer = Indexer(args.config.get_libpq_dsn(), args.threads or _num_system_cpus() or 1) diff --git a/test/python/test_cli.py b/test/python/test_cli.py index 92b42372..9ac62973 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -5,6 +5,7 @@ import psycopg2 import pytest import nominatim.cli +import nominatim.indexer.indexer def call_nominatim(*args): return nominatim.cli.nominatim(module_dir='build/module', @@ -87,9 +88,9 @@ def test_index_command(monkeypatch, temp_db, params, do_bnds, do_ranks): with conn.cursor() as cur: cur.execute("CREATE TABLE import_status (indexed bool)") bnd_mock = MockParamCapture() - monkeypatch.setattr(nominatim.cli.Indexer, 'index_boundaries', bnd_mock) + monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', bnd_mock) rank_mock = MockParamCapture() - monkeypatch.setattr(nominatim.cli.Indexer, 'index_by_rank', rank_mock) + monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', rank_mock) assert 0 == call_nominatim('index', *params)