1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Configuration for Sanitizers.
10 from typing import Sequence, Optional, Pattern, Callable, Any, TYPE_CHECKING
11 from collections import UserDict
14 from nominatim.errors import UsageError
16 # working around missing generics in Python < 3.8
17 # See https://github.com/python/typing/issues/60#issuecomment-869757075
19 _BaseUserDict = UserDict[str, Any]
21 _BaseUserDict = UserDict
23 class SanitizerConfig(_BaseUserDict):
24 """ The `SanitizerConfig` class is a read-only dictionary
25 with configuration options for the sanitizer.
26 In addition to the usual dictionary functions, the class provides
27 accessors to standard sanitizer options that are used by many of the
31 def get_string_list(self, param: str, default: Sequence[str] = tuple()) -> Sequence[str]:
32 """ Extract a configuration parameter as a string list.
35 param: Name of the configuration parameter.
36 default: Value to return, when the parameter is missing.
39 If the parameter value is a simple string, it is returned as a
40 one-item list. If the parameter value does not exist, the given
41 default is returned. If the parameter value is a list, it is
42 checked to contain only strings before being returned.
44 values = self.data.get(param, None)
47 return None if default is None else list(default)
49 if isinstance(values, str):
50 return [values] if values else []
52 if not isinstance(values, (list, tuple)):
53 raise UsageError(f"Parameter '{param}' must be string or list of strings.")
55 if any(not isinstance(value, str) for value in values):
56 raise UsageError(f"Parameter '{param}' must be string or list of strings.")
61 def get_bool(self, param: str, default: Optional[bool] = None) -> bool:
62 """ Extract a configuration parameter as a boolean.
65 param: Name of the configuration parameter. The parameter must
66 contain one of the yaml boolean values or an
67 UsageError will be raised.
68 default: Value to return, when the parameter is missing.
69 When set to `None`, the parameter must be defined.
72 Boolean value of the given parameter.
74 value = self.data.get(param, default)
76 if not isinstance(value, bool):
77 raise UsageError(f"Parameter '{param}' must be a boolean value ('yes' or 'no'.")
82 def get_delimiter(self, default: str = ',;') -> Pattern[str]:
83 """ Return the 'delimiters' parameter in the configuration as a
84 compiled regular expression that can be used to split strings on
88 default: Delimiters to be used when 'delimiters' parameter
89 is not explicitly configured.
92 A regular expression pattern which can be used to
93 split a string. The regular expression makes sure that the
94 resulting names are stripped and that repeated delimiters
95 are ignored. It may still create empty fields on occasion. The
96 code needs to filter those.
98 delimiter_set = set(self.data.get('delimiters', default))
100 raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
102 return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
105 def get_filter_kind(self, *default: str) -> Callable[[str], bool]:
106 """ Return a filter function for the name kind from the 'filter-kind'
109 If the 'filter-kind' parameter is empty, the filter lets all items
110 pass. If the parameter is a string, it is interpreted as a single
111 regular expression that must match the full kind string.
112 If the parameter is a list then
113 any of the regular expressions in the list must match to pass.
116 default: Filters to be used, when the 'filter-kind' parameter
117 is not specified. If omitted then the default is to
121 A filter function which takes a name string and returns
122 True when the item passes the filter.
124 filters = self.get_string_list('filter-kind', default)
127 return lambda _: True
129 regexes = [re.compile(regex) for regex in filters]
131 return lambda name: any(regex.fullmatch(name) for regex in regexes)