]> git.openstreetmap.org Git - nominatim.git/commitdiff
add get_str_list() for config
authorSarah Hoffmann <lonvia@denofr.de>
Sun, 29 May 2022 11:53:50 +0000 (13:53 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sun, 29 May 2022 11:53:50 +0000 (13:53 +0200)
Converts a config value written as a comma-sparated list into
a Python list of strings.

nominatim/clicmd/setup.py
nominatim/config.py
nominatim/tools/country_info.py
test/python/config/test_config.py
test/python/tools/test_country_info.py

index 7b5f379739e5f7269975970d05ef182cee403b4b..f0ec358bc50d4d4484c23338d323350c387360ac 100644 (file)
@@ -128,7 +128,7 @@ class SetupAll:
                                                   drop=args.no_updates)
             LOG.warning('Create search index for default country names.')
             country_info.create_country_names(conn, tokenizer,
-                                              args.config.LANGUAGES)
+                                              args.config.get_str_list('LANGUAGES'))
             if args.no_updates:
                 freeze.drop_update_tables(conn)
         tokenizer.finalize_import(args.config)
index ef2610793bad4a8e0ea4efca62e685d6c90fee5a..700af328d093dbf6f145966c21e9f4cb9a79a5f8 100644 (file)
@@ -99,6 +99,17 @@ class Configuration:
             raise UsageError("Configuration error.") from exp
 
 
+    def get_str_list(self, name):
+        """ Return the given configuration parameter as a list of strings.
+            The values are assumed to be given as a comma-sparated list and
+            will be stripped before returning them. On empty values None
+            is returned.
+        """
+        raw = self.__getattr__(name)
+
+        return [v.strip() for v in raw.split(',')] if raw else None
+
+
     def get_path(self, name):
         """ Return the given configuration parameter as a Path.
             If a relative path is configured, then the function converts this
index ed04c2d55433358e745a434dd48dd2f7fbe1584b..0ad001719e164f110afbf063f69f57711a78b42c 100644 (file)
@@ -131,9 +131,6 @@ def create_country_names(conn, tokenizer, languages=None):
         empty then only name translations for the given languages are added
         to the index.
     """
-    if languages:
-        languages = languages.split(',')
-
     def _include_key(key):
         return ':' not in key or not languages or \
                key[key.index(':') + 1:] in languages
index 9f9ca8807c7f9426928198ddb09429efb5ccbe1f..a9cbb48dd0ed9fb7bee323998aaa3d0e19fc2109 100644 (file)
@@ -173,6 +173,23 @@ def test_get_int_empty(make_config):
         config.get_int('DATABASE_MODULE_PATH')
 
 
+@pytest.mark.parametrize("value,outlist", [('sd', ['sd']),
+                                           ('dd,rr', ['dd', 'rr']),
+                                           (' a , b ', ['a', 'b'])])
+def test_get_str_list_success(make_config, monkeypatch, value, outlist):
+    config = make_config()
+
+    monkeypatch.setenv('NOMINATIM_MYLIST', value)
+
+    assert config.get_str_list('MYLIST') == outlist
+
+
+def test_get_str_list_empty(make_config):
+    config = make_config()
+
+    assert config.get_str_list('LANGUAGES') is None
+
+
 def test_get_path_empty(make_config):
     config = make_config()
 
index 3c20b3e020018ec5f259a5d91b7fcac8d284739d..3f00d54e1706d5003623164bb01e2e3065e4881c 100644 (file)
@@ -49,7 +49,7 @@ def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cur
     assert temp_db_cursor.table_rows('country_osm_grid') > 100
 
 
-@pytest.mark.parametrize("languages", (None, ' fr,en'))
+@pytest.mark.parametrize("languages", (None, ['fr', 'en']))
 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
                               table_factory, tokenizer_mock, languages, loaded_country):