]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/config.py
Merge pull request #2757 from lonvia/filter-postcodes
[nominatim.git] / nominatim / tokenizer / sanitizers / config.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Configuration for Sanitizers.
9 """
10 from collections import UserDict
11 import re
12
13 from nominatim.errors import UsageError
14
15 class SanitizerConfig(UserDict):
16     """ Dictionary with configuration options for a sanitizer.
17
18         In addition to the usualy dictionary function, the class provides
19         accessors to standard sanatizer options that are used by many of the
20         sanitizers.
21     """
22
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.
29         """
30         values = self.data.get(param, None)
31
32         if values is None:
33             return None if default is None else list(default)
34
35         if isinstance(values, str):
36             return [values] if values else []
37
38         if not isinstance(values, (list, tuple)):
39             raise UsageError(f"Parameter '{param}' must be string or list of strings.")
40
41         if any(not isinstance(value, str) for value in values):
42             raise UsageError(f"Parameter '{param}' must be string or list of strings.")
43
44         return values
45
46
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.
52         """
53         value = self.data.get(param, default)
54
55         if not isinstance(value, bool):
56             raise UsageError(f"Parameter '{param}' must be a boolean value ('yes' or 'no'.")
57
58         return value
59
60
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.
68
69             The 'default' parameter defines the delimiter set to be used when
70             not explicitly configured.
71         """
72         delimiter_set = set(self.data.get('delimiters', default))
73         if not delimiter_set:
74             raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
75
76         return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
77
78
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.
83
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.
88         """
89         filters = self.get_string_list('filter-kind', default)
90
91         if not filters:
92             return lambda _: True
93
94         regexes = [re.compile(regex) for regex in filters]
95
96         return lambda name: any(regex.fullmatch(name.kind) for regex in regexes)