]> git.openstreetmap.org Git - nominatim.git/commitdiff
have ADDRESS_LEVEL_CONFIG use load_sub_configuration
authorSarah Hoffmann <lonvia@denofr.de>
Fri, 22 Oct 2021 14:31:33 +0000 (16:31 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Fri, 22 Oct 2021 14:36:52 +0000 (16:36 +0200)
This means that relative paths now are looked up in the
project directory.

docs/customize/Settings.md
nominatim/clicmd/refresh.py
nominatim/clicmd/setup.py
nominatim/config.py
nominatim/tools/refresh.py
settings/env.defaults
test/python/test_cli.py
test/python/test_tools_refresh_address_levels.py

index f34f85b1f50b636ea735706f8d839c47aa6a5f17..a71b2e1b1f9fa66704e60db028ac39c31cbb5b1e 100644 (file)
@@ -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
 
index e7d7d7ba1d33cc032b3c0260a5ffc3d05f772344..82a61f54e56df3c9497aee4ef49e0150323c6d93 100644 (file)
@@ -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')
index 5e43d446a10fe71b7ffd12f63d87e28d4b6af2e4..27847920b80a434b1f4df0b1b4ac0b7b17ba5b24 100644 (file)
@@ -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')
index 3ac8e33fc65ffe5e3c3250c71c29a11feabb5686..28f2fcc3a2a7b89412346c0cbea13be58bc7908d 100644 (file)
@@ -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
 
index 00ae5dc95bb4c3c5fa0205e74ab49abf61b51e13..a6fe3d19ad808b5101e0f4ce702a75d9b8d8a2a0 100644 (file)
@@ -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):
index 2ece74dcc67e3b0dea7d85c95e9f629b75fc2e25..00f5569ae0894db7aa1179cd39bb635ad98042eb 100644 (file)
@@ -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
index 7e6bf99e4286c3c6430131a5e8a9ae81a53a80aa..707be23bce5c8b34571fa4dc92e0bd086fed9641 100644 (file)
@@ -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'),
index 2821222c5712183d962f8feb3cc72fffd558b055..2c4ee24defb46def96ca69b29043fb19ecfa90d8 100644 (file)
@@ -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):