From 5d302a03a6ed9250b7a280a9bdd0c7e7a9bb2222 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Tue, 11 Jun 2024 20:21:19 +0200 Subject: [PATCH] make use of nominatim-api in db package optional --- packaging/nominatim-api/pyproject.toml | 3 +-- packaging/nominatim-core/pyproject.toml | 3 +-- packaging/nominatim-db/pyproject.toml | 3 +-- src/nominatim_db/cli.py | 32 ++++++++++++++++++------- src/nominatim_db/clicmd/__init__.py | 7 ------ src/nominatim_db/clicmd/admin.py | 7 +++++- src/nominatim_db/version.py | 2 +- 7 files changed, 33 insertions(+), 24 deletions(-) diff --git a/packaging/nominatim-api/pyproject.toml b/packaging/nominatim-api/pyproject.toml index 7e9230e4..0e3f36d9 100644 --- a/packaging/nominatim-api/pyproject.toml +++ b/packaging/nominatim-api/pyproject.toml @@ -30,9 +30,8 @@ requires = ["hatchling"] build-backend = "hatchling.build" [tool.hatch.version] -source = "code" path = "../../src/nominatim_api/version.py" -expression = "NOMINATIM_API_VERSION" +pattern = "NOMINATIM_API_VERSION = '(?P[^']+)'" [tool.hatch.build.targets.sdist.force-include] "../../src/nominatim_db" = "nominatim_api" diff --git a/packaging/nominatim-core/pyproject.toml b/packaging/nominatim-core/pyproject.toml index 931c857b..19e86cde 100644 --- a/packaging/nominatim-core/pyproject.toml +++ b/packaging/nominatim-core/pyproject.toml @@ -30,9 +30,8 @@ requires = ["hatchling"] build-backend = "hatchling.build" [tool.hatch.version] -source = "code" path = "../../src/nominatim_core/version.py" -expression = "NOMINATIM_CORE_VERSION" +pattern = "NOMINATIM_CORE_VERSION = '(?P[^']+)'" [tool.hatch.build.targets.sdist.force-include] "../../src/nominatim_core" = "nominatim_core" diff --git a/packaging/nominatim-db/pyproject.toml b/packaging/nominatim-db/pyproject.toml index 4cad2b8e..f7d0502b 100644 --- a/packaging/nominatim-db/pyproject.toml +++ b/packaging/nominatim-db/pyproject.toml @@ -30,9 +30,8 @@ requires = ["hatchling"] build-backend = "hatchling.build" [tool.hatch.version] -source = "code" path = "../../src/nominatim_db/version.py" -expression = "NOMINATIM_VERSION.release_version()" +pattern = "NOMINATIM_VERSION = parse_version.'(?P[^-]+)" [tool.hatch.build.targets.sdist.force-include] "../../src/nominatim_db" = "nominatim_db" diff --git a/src/nominatim_db/cli.py b/src/nominatim_db/cli.py index 746fbe32..8a85e0e2 100644 --- a/src/nominatim_db/cli.py +++ b/src/nominatim_db/cli.py @@ -207,15 +207,29 @@ def get_set_parser() -> CommandlineParser: parser.add_subcommand('admin', clicmd.AdminFuncs()) - parser.add_subcommand('export', clicmd.QueryExport()) - parser.add_subcommand('convert', clicmd.ConvertDB()) - parser.add_subcommand('serve', AdminServe()) - - parser.add_subcommand('search', clicmd.APISearch()) - parser.add_subcommand('reverse', clicmd.APIReverse()) - parser.add_subcommand('lookup', clicmd.APILookup()) - parser.add_subcommand('details', clicmd.APIDetails()) - parser.add_subcommand('status', clicmd.APIStatus()) + try: + exportcmd = importlib.import_module('nominatim_db.clicmd.export') + apicmd = importlib.import_module('nominatim_db.clicmd.api') + convertcmd = importlib.import_module('nominatim_db.clicmd.convert') + + parser.add_subcommand('export', exportcmd.QueryExport()) + parser.add_subcommand('convert', convertcmd.ConvertDB()) + parser.add_subcommand('serve', AdminServe()) + + parser.add_subcommand('search', apicmd.APISearch()) + parser.add_subcommand('reverse', apicmd.APIReverse()) + parser.add_subcommand('lookup', apicmd.APILookup()) + parser.add_subcommand('details', apicmd.APIDetails()) + parser.add_subcommand('status', apicmd.APIStatus()) + except ModuleNotFoundError as ex: + if not ex.name or 'nominatim_api' not in ex.name: # pylint: disable=E1135 + raise ex + + parser.parser.epilog = \ + '\n\nNominatim API package not found. The following commands are not available:'\ + '\n export, convert, serve, search, reverse, lookup, details, status'\ + "\n\nRun 'pip install nominatim-api' to install the package." + return parser diff --git a/src/nominatim_db/clicmd/__init__.py b/src/nominatim_db/clicmd/__init__.py index 0481a45e..725b4d6b 100644 --- a/src/nominatim_db/clicmd/__init__.py +++ b/src/nominatim_db/clicmd/__init__.py @@ -13,16 +13,9 @@ Subcommand definitions for the command-line tool. from .setup import SetupAll as SetupAll from .replication import UpdateReplication as UpdateReplication -from .api import (APISearch as APISearch, - APIReverse as APIReverse, - APILookup as APILookup, - APIDetails as APIDetails, - APIStatus as APIStatus) from .index import UpdateIndex as UpdateIndex from .refresh import UpdateRefresh as UpdateRefresh from .add_data import UpdateAddData as UpdateAddData from .admin import AdminFuncs as AdminFuncs from .freeze import SetupFreeze as SetupFreeze from .special_phrases import ImportSpecialPhrases as ImportSpecialPhrases -from .export import QueryExport as QueryExport -from .convert import ConvertDB as ConvertDB diff --git a/src/nominatim_db/clicmd/admin.py b/src/nominatim_db/clicmd/admin.py index 66dbbe99..7b97557d 100644 --- a/src/nominatim_db/clicmd/admin.py +++ b/src/nominatim_db/clicmd/admin.py @@ -11,7 +11,7 @@ import logging import argparse import random -import nominatim_api as napi +from nominatim_core.errors import UsageError from nominatim_core.db.connection import connect from .args import NominatimArgs @@ -95,6 +95,11 @@ class AdminFuncs: def _warm(self, args: NominatimArgs) -> int: + try: + import nominatim_api as napi + except ModuleNotFoundError as exp: + raise UsageError("Warming requires nominatim API. " + "Install with 'pip install nominatim-api'.") from exp LOG.warning('Warming database caches') api = napi.NominatimAPI(args.project_dir) diff --git a/src/nominatim_db/version.py b/src/nominatim_db/version.py index 525a0d99..34e3b432 100644 --- a/src/nominatim_db/version.py +++ b/src/nominatim_db/version.py @@ -15,7 +15,7 @@ from typing import Optional from nominatim_core.version import (NominatimVersion as NominatimVersion, parse_version as parse_version) -NOMINATIM_VERSION = NominatimVersion(4, 4, 99, 1) +NOMINATIM_VERSION = parse_version('4.4.99-1') POSTGRESQL_REQUIRED_VERSION = (9, 6) POSTGIS_REQUIRED_VERSION = (2, 2) -- 2.39.5