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_delimiter(self, default=',;'):
48 """ Return the 'delimiter' parameter in the configuration as a
49 compiled regular expression that can be used to split the names on the
50 delimiters. The regular expression makes sure that the resulting names
51 are stripped and that repeated delimiters
52 are ignored but it will still create empty fields on occasion. The
53 code needs to filter those.
55 The 'default' parameter defines the delimiter set to be used when
56 not explicitly configured.
58 delimiter_set = set(self.data.get('delimiters', default))
60 raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
62 return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
65 def get_filter_kind(self, *default):
66 """ Return a filter function for the name kind from the 'filter-kind'
67 config parameter. The filter functions takes a name item and returns
68 True when the item passes the filter.
70 If the parameter is empty, the filter lets all items pass. If the
71 paramter is a string, it is interpreted as a single regular expression
72 that must match the full kind string. If the parameter is a list then
73 any of the regular expressions in the list must match to pass.
75 filters = self.get_string_list('filter-kind', default)
80 regexes = [re.compile(regex) for regex in filters]
82 return lambda name: any(regex.fullmatch(name.kind) for regex in regexes)