From: Sarah Hoffmann Date: Fri, 22 Oct 2021 15:32:51 +0000 (+0200) Subject: allow relative paths for flatnode file X-Git-Tag: v4.0.0~13^2 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/1098ab732f42cb1f36747e805119fd6ba91acf26?ds=inline;hp=-c allow relative paths for flatnode file --- 1098ab732f42cb1f36747e805119fd6ba91acf26 diff --git a/docs/customize/Settings.md b/docs/customize/Settings.md index 3a45e8fe..b056a78b 100644 --- a/docs/customize/Settings.md +++ b/docs/customize/Settings.md @@ -354,6 +354,9 @@ location for OSM nodes. For larger imports it can significantly speed up the import. When this option is unset, then osm2pgsql uses a PsotgreSQL table to store the locations. +When a relative path is given, then the flatnode file is created/searched +relative to the project directory. + !!! warning The flatnode file is not only used during the initial import but also diff --git a/nominatim/clicmd/args.py b/nominatim/clicmd/args.py index 694e6fc5..4e2c23a7 100644 --- a/nominatim/clicmd/args.py +++ b/nominatim/clicmd/args.py @@ -23,7 +23,7 @@ class NominatimArgs: osm2pgsql_style=self.config.get_import_style_file(), threads=self.threads or default_threads, dsn=self.config.get_libpq_dsn(), - flatnode_file=self.config.FLATNODE_FILE, + flatnode_file=str(self.config.get_path('FLATNODE_FILE')), tablespaces=dict(slim_data=self.config.TABLESPACE_OSM_DATA, slim_index=self.config.TABLESPACE_OSM_INDEX, main_data=self.config.TABLESPACE_PLACE_DATA, diff --git a/nominatim/clicmd/freeze.py b/nominatim/clicmd/freeze.py index 8a6c928e..41dd610e 100644 --- a/nominatim/clicmd/freeze.py +++ b/nominatim/clicmd/freeze.py @@ -31,6 +31,6 @@ class SetupFreeze: with connect(args.config.get_libpq_dsn()) as conn: freeze.drop_update_tables(conn) - freeze.drop_flatnode_file(args.config.FLATNODE_FILE) + freeze.drop_flatnode_file(str(args.config.get_path('FLATNODE_FILE'))) return 0 diff --git a/nominatim/config.py b/nominatim/config.py index 66375f6c..bc3556f3 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -93,6 +93,23 @@ class Configuration: raise UsageError("Configuration error.") from exp + def get_path(self, name): + """ Return the given configuration parameter as a Path. + If a relative path is configured, then the function converts this + into an absolute path with the project directory as root path. + If the configuration is unset, a falsy value is returned. + """ + value = self.__getattr__(name) + if value: + value = Path(value) + + if not value.is_absolute(): + value = self.project_dir / value + + value = value.resolve() + + return value + def get_libpq_dsn(self): """ Get configured database DSN converted into the key/value format understood by libpq and psycopg. diff --git a/test/python/test_config.py b/test/python/test_config.py index de7b9301..a71324f9 100644 --- a/test/python/test_config.py +++ b/test/python/test_config.py @@ -1,6 +1,7 @@ """ Test for loading dotenv configuration. """ +from pathlib import Path import pytest from nominatim.config import Configuration @@ -166,6 +167,33 @@ def test_get_int_empty(make_config): config.get_int('DATABASE_MODULE_PATH') +def test_get_path_empty(make_config): + config = make_config() + + assert config.DATABASE_MODULE_PATH == '' + assert not config.get_path('DATABASE_MODULE_PATH') + + +def test_get_path_absolute(make_config, monkeypatch): + config = make_config() + + monkeypatch.setenv('NOMINATIM_FOOBAR', '/dont/care') + result = config.get_path('FOOBAR') + + assert isinstance(result, Path) + assert str(result) == '/dont/care' + + +def test_get_path_relative(make_config, monkeypatch, tmp_path): + config = make_config(tmp_path) + + monkeypatch.setenv('NOMINATIM_FOOBAR', 'an/oyster') + result = config.get_path('FOOBAR') + + assert isinstance(result, Path) + assert str(result) == str(tmp_path / 'an/oyster') + + def test_get_import_style_intern(make_config, src_dir, monkeypatch): config = make_config()