From: Sarah Hoffmann Date: Fri, 22 Oct 2021 14:31:33 +0000 (+0200) Subject: have ADDRESS_LEVEL_CONFIG use load_sub_configuration X-Git-Tag: v4.0.0~13^2~2 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/0ae8d7ac083983e1d7092fb5855c698acc5cb24c have ADDRESS_LEVEL_CONFIG use load_sub_configuration This means that relative paths now are looked up in the project directory. --- diff --git a/docs/customize/Settings.md b/docs/customize/Settings.md index f34f85b1..a71b2e1b 100644 --- a/docs/customize/Settings.md +++ b/docs/customize/Settings.md @@ -309,12 +309,15 @@ unset, Nominatim expects the data to be saved in the project directory. | -------------- | --------------------------------------------------- | | **Description:** | Configuration file for rank assignments | | **Format:** | path | -| **Default:** | _empty_ (use default settings) | +| **Default:** | address-levels.json | -The _address level config_ configures rank assignments for places. See +The _address level configuration_ defines the rank assignments for places. See [Place Ranking](Ranking.md) for a detailed explanation what rank assignments -are and what the configuration file must look like. The default configuration -can be found in the configuration directory as `address-levels.json`. +are and what the configuration file must look like. + +When a relative path is given, then the file is searched first relative to the +project directory and then in the global settings directory. + #### NOMINATIM_IMPORT_STYLE diff --git a/nominatim/clicmd/refresh.py b/nominatim/clicmd/refresh.py index e7d7d7ba..82a61f54 100644 --- a/nominatim/clicmd/refresh.py +++ b/nominatim/clicmd/refresh.py @@ -75,10 +75,9 @@ class UpdateRefresh: self._get_tokenizer(args.config).update_statistics() if args.address_levels: - cfg = Path(args.config.ADDRESS_LEVEL_CONFIG) - LOG.warning('Updating address levels from %s', cfg) + LOG.warning('Updating address levels') with connect(args.config.get_libpq_dsn()) as conn: - refresh.load_address_levels_from_file(conn, cfg) + refresh.load_address_levels_from_config(conn, args.config) if args.functions: LOG.warning('Create functions') diff --git a/nominatim/clicmd/setup.py b/nominatim/clicmd/setup.py index 5e43d446..27847920 100644 --- a/nominatim/clicmd/setup.py +++ b/nominatim/clicmd/setup.py @@ -150,7 +150,7 @@ class SetupAll: refresh.create_functions(conn, config, False, False) LOG.warning('Create tables') database_import.create_tables(conn, config, reverse_only=reverse_only) - refresh.load_address_levels_from_file(conn, Path(config.ADDRESS_LEVEL_CONFIG)) + refresh.load_address_levels_from_config(conn, config) LOG.warning('Create functions (2nd pass)') refresh.create_functions(conn, config, False, False) LOG.warning('Create table triggers') diff --git a/nominatim/config.py b/nominatim/config.py index 3ac8e33f..28f2fcc3 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -56,12 +56,6 @@ class Configuration: if project_dir is not None and (project_dir / '.env').is_file(): self._config.update(dotenv_values(str((project_dir / '.env').resolve()))) - # Add defaults for variables that are left empty to set the default. - # They may still be overwritten by environment variables. - if not self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG']: - self._config['NOMINATIM_ADDRESS_LEVEL_CONFIG'] = \ - str(config_dir / 'address-levels.json') - class _LibDirs: pass diff --git a/nominatim/tools/refresh.py b/nominatim/tools/refresh.py index 00ae5dc9..a6fe3d19 100644 --- a/nominatim/tools/refresh.py +++ b/nominatim/tools/refresh.py @@ -1,7 +1,6 @@ """ Functions for bringing auxiliary data in the database up-to-date. """ -import json import logging from textwrap import dedent @@ -58,12 +57,15 @@ def load_address_levels(conn, table, levels): conn.commit() -def load_address_levels_from_file(conn, config_file): - """ Replace the `address_levels` table with the contents of the config - file. + +def load_address_levels_from_config(conn, config): + """ Replace the `address_levels` table with the content as + defined in the given configuration. Uses the parameter + NOMINATIM_ADDRESS_LEVEL_CONFIG to determine the location of the + configuration file. """ - with config_file.open('r') as fdesc: - load_address_levels(conn, 'address_levels', json.load(fdesc)) + cfg = config.load_sub_configuration('', config='ADDRESS_LEVEL_CONFIG') + load_address_levels(conn, 'address_levels', cfg) def create_functions(conn, config, enable_diff_updates=True, enable_debug=False): diff --git a/settings/env.defaults b/settings/env.defaults index 2ece74dc..00f5569a 100644 --- a/settings/env.defaults +++ b/settings/env.defaults @@ -94,9 +94,7 @@ NOMINATIM_WIKIPEDIA_DATA_PATH= NOMINATIM_PHRASE_CONFIG= # Configuration file for rank assignments. -# When unset, the internal default settings from 'settings/address-levels.json' -# are used. -NOMINATIM_ADDRESS_LEVEL_CONFIG= +NOMINATIM_ADDRESS_LEVEL_CONFIG=address-levels.json # Configuration file for OSM data import. # This may either be the name of one of an internal style or point diff --git a/test/python/test_cli.py b/test/python/test_cli.py index 7e6bf99e..707be23b 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -186,7 +186,7 @@ class TestCliWithDb: mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'), mock_func_factory(nominatim.tools.database_import, 'create_search_indices'), mock_func_factory(nominatim.tools.country_info, 'create_country_names'), - mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'), + mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_config'), mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'), mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'), mock_func_factory(nominatim.tools.refresh, 'setup_website'), @@ -321,7 +321,7 @@ class TestCliWithDb: assert func.called == 1 @pytest.mark.parametrize("command,func", [ - ('address-levels', 'load_address_levels_from_file'), + ('address-levels', 'load_address_levels_from_config'), ('wiki-data', 'import_wikipedia_articles'), ('importance', 'recompute_importance'), ('website', 'setup_website'), diff --git a/test/python/test_tools_refresh_address_levels.py b/test/python/test_tools_refresh_address_levels.py index 2821222c..2c4ee24d 100644 --- a/test/python/test_tools_refresh_address_levels.py +++ b/test/python/test_tools_refresh_address_levels.py @@ -6,28 +6,31 @@ from pathlib import Path import pytest -from nominatim.tools.refresh import load_address_levels, load_address_levels_from_file +from nominatim.tools.refresh import load_address_levels, load_address_levels_from_config def test_load_ranks_def_config(temp_db_conn, temp_db_cursor, def_config): - load_address_levels_from_file(temp_db_conn, Path(def_config.ADDRESS_LEVEL_CONFIG)) + load_address_levels_from_config(temp_db_conn, def_config) assert temp_db_cursor.table_rows('address_levels') > 0 -def test_load_ranks_from_file(temp_db_conn, temp_db_cursor, tmp_path): - test_file = tmp_path / 'test_levels.json' +def test_load_ranks_from_project_dir(def_config, temp_db_conn, temp_db_cursor, + tmp_path): + test_file = tmp_path / 'address-levels.json' test_file.write_text('[{"tags":{"place":{"sea":2}}}]') + def_config.project_dir = tmp_path - load_address_levels_from_file(temp_db_conn, test_file) + load_address_levels_from_config(temp_db_conn, def_config) - assert temp_db_cursor.table_rows('address_levels') > 0 + assert temp_db_cursor.table_rows('address_levels') == 1 -def test_load_ranks_from_broken_file(temp_db_conn, tmp_path): - test_file = tmp_path / 'test_levels.json' +def test_load_ranks_from_broken_file(def_config, temp_db_conn, tmp_path): + test_file = tmp_path / 'address-levels.json' test_file.write_text('[{"tags":"place":{"sea":2}}}]') + def_config.project_dir = tmp_path with pytest.raises(json.decoder.JSONDecodeError): - load_address_levels_from_file(temp_db_conn, test_file) + load_address_levels_from_config(temp_db_conn, def_config) def test_load_ranks_country(temp_db_conn, temp_db_cursor):