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 collections import UserDict
13 from nominatim.errors import UsageError
15 class SanitizerConfig(UserDict):
16 """ Dictionary with configuration options for a sanitizer.
18 In addition to the usualy dictionary function, the class provides
19 accessors to standard sanatizer options that are used by many of the
23 def get_string_list(self, param, default=tuple()):
24 """ Extract a configuration parameter as a string list.
25 If the parameter value is a simple string, it is returned as a
26 one-item list. If the parameter value does not exist, the given
27 default is returned. If the parameter value is a list, it is checked
28 to contain only strings before being returned.
30 values = self.data.get(param, None)
33 return None if default is None else list(default)
35 if isinstance(values, str):
36 return [values] if values else []
38 if not isinstance(values, (list, tuple)):
39 raise UsageError(f"Parameter '{param}' must be string or list of strings.")
41 if any(not isinstance(value, str) for value in values):
42 raise UsageError(f"Parameter '{param}' must be string or list of strings.")
47 def get_bool(self, param, default=None):
48 """ Extract a configuration parameter as a boolean.
49 The parameter must be one of the yaml boolean values or an
50 user error will be raised. If `default` is given, then the parameter
51 may also be missing or empty.
53 value = self.data.get(param, default)
55 if not isinstance(value, bool):
56 raise UsageError(f"Parameter '{param}' must be a boolean value ('yes' or 'no'.")
61 def get_delimiter(self, default=',;'):
62 """ Return the 'delimiter' parameter in the configuration as a
63 compiled regular expression that can be used to split the names on the
64 delimiters. The regular expression makes sure that the resulting names
65 are stripped and that repeated delimiters
66 are ignored but it will still create empty fields on occasion. The
67 code needs to filter those.
69 The 'default' parameter defines the delimiter set to be used when
70 not explicitly configured.
72 delimiter_set = set(self.data.get('delimiters', default))
74 raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
76 return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
79 def get_filter_kind(self, *default):
80 """ Return a filter function for the name kind from the 'filter-kind'
81 config parameter. The filter functions takes a name item and returns
82 True when the item passes the filter.
84 If the parameter is empty, the filter lets all items pass. If the
85 paramter is a string, it is interpreted as a single regular expression
86 that must match the full kind string. If the parameter is a list then
87 any of the regular expressions in the list must match to pass.
89 filters = self.get_string_list('filter-kind', default)
94 regexes = [re.compile(regex) for regex in filters]
96 return lambda name: any(regex.fullmatch(name.kind) for regex in regexes)