From: Sarah Hoffmann Date: Mon, 4 Oct 2021 09:56:54 +0000 (+0200) Subject: move flatten_config_list into config module X-Git-Tag: v4.0.0~22^2~11 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/5a3655983464b3739f7a4318dc002f07c770a43c move flatten_config_list into config module For general usage by other modules. --- diff --git a/nominatim/config.py b/nominatim/config.py index 64614bf1..f316280b 100644 --- a/nominatim/config.py +++ b/nominatim/config.py @@ -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. diff --git a/nominatim/tokenizer/icu_rule_loader.py b/nominatim/tokenizer/icu_rule_loader.py index 330179bb..7719f211 100644 --- a/nominatim/tokenizer/icu_rule_loader.py +++ b/nominatim/tokenizer/icu_rule_loader.py @@ -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)