move flatten_config_list into config module

For general usage by other modules.
This commit is contained in:
Sarah Hoffmann 2021-10-04 11:56:54 +02:00
parent 19d4e047f6
commit 5a36559834
2 changed files with 24 additions and 19 deletions

View File

@ -12,6 +12,27 @@ from nominatim.errors import UsageError
LOG = logging.getLogger()
def flatten_config_list(content, section=''):
""" Flatten YAML configuration lists that contain include sections
which are lists themselves.
"""
if not content:
return []
if not isinstance(content, list):
raise UsageError(f"List expected in section '{section}'.")
output = []
for ele in content:
if isinstance(ele, list):
output.extend(flatten_config_list(ele, section))
else:
output.append(ele)
return output
class Configuration:
""" Load and manage the project configuration.

View File

@ -9,6 +9,7 @@ import re
from icu import Transliterator
from nominatim.config import flatten_config_list
from nominatim.db.properties import set_property, get_property
from nominatim.errors import UsageError
from nominatim.tokenizer.icu_name_processor import ICUNameProcessor
@ -22,23 +23,6 @@ DBCFG_IMPORT_TRANS_RULES = "tokenizer_import_transliteration"
DBCFG_IMPORT_ANALYSIS_RULES = "tokenizer_import_analysis_rules"
def _flatten_config_list(content):
if not content:
return []
if not isinstance(content, list):
raise UsageError("List expected in ICU configuration.")
output = []
for ele in content:
if isinstance(ele, list):
output.extend(_flatten_config_list(ele))
else:
output.append(ele)
return output
class VariantRule:
""" Saves a single variant expansion.
@ -158,7 +142,7 @@ class ICURuleLoader:
if content is None:
return ''
return ';'.join(_flatten_config_list(content)) + ';'
return ';'.join(flatten_config_list(content, section)) + ';'
def _parse_variant_list(self):
@ -169,7 +153,7 @@ class ICURuleLoader:
if not rules:
return
rules = _flatten_config_list(rules)
rules = flatten_config_list(rules, 'variants')
vmaker = _VariantMaker(self.normalization_rules)