]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/config.py
add type hints for sanitizers
[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 typing import Sequence, Optional, Pattern, Callable, Any, TYPE_CHECKING
11 from collections import UserDict
12 import re
13
14 from nominatim.errors import UsageError
15
16 # working around missing generics in Python < 3.8
17 # See https://github.com/python/typing/issues/60#issuecomment-869757075
18 if TYPE_CHECKING:
19     _BaseUserDict = UserDict[str, Any]
20 else:
21     _BaseUserDict = UserDict
22
23 class SanitizerConfig(_BaseUserDict):
24     """ Dictionary with configuration options for a sanitizer.
25
26         In addition to the usual dictionary function, the class provides
27         accessors to standard sanatizer options that are used by many of the
28         sanitizers.
29     """
30
31     def get_string_list(self, param: str, default: Sequence[str] = tuple()) -> Sequence[str]:
32         """ Extract a configuration parameter as a string list.
33             If the parameter value is a simple string, it is returned as a
34             one-item list. If the parameter value does not exist, the given
35             default is returned. If the parameter value is a list, it is checked
36             to contain only strings before being returned.
37         """
38         values = self.data.get(param, None)
39
40         if values is None:
41             return None if default is None else list(default)
42
43         if isinstance(values, str):
44             return [values] if values else []
45
46         if not isinstance(values, (list, tuple)):
47             raise UsageError(f"Parameter '{param}' must be string or list of strings.")
48
49         if any(not isinstance(value, str) for value in values):
50             raise UsageError(f"Parameter '{param}' must be string or list of strings.")
51
52         return values
53
54
55     def get_bool(self, param: str, default: Optional[bool] = None) -> bool:
56         """ Extract a configuration parameter as a boolean.
57             The parameter must be one of the yaml boolean values or an
58             user error will be raised. If `default` is given, then the parameter
59             may also be missing or empty.
60         """
61         value = self.data.get(param, default)
62
63         if not isinstance(value, bool):
64             raise UsageError(f"Parameter '{param}' must be a boolean value ('yes' or 'no'.")
65
66         return value
67
68
69     def get_delimiter(self, default: str = ',;') -> Pattern[str]:
70         """ Return the 'delimiter' parameter in the configuration as a
71             compiled regular expression that can be used to split the names on the
72             delimiters. The regular expression makes sure that the resulting names
73             are stripped and that repeated delimiters
74             are ignored but it will still create empty fields on occasion. The
75             code needs to filter those.
76
77             The 'default' parameter defines the delimiter set to be used when
78             not explicitly configured.
79         """
80         delimiter_set = set(self.data.get('delimiters', default))
81         if not delimiter_set:
82             raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
83
84         return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
85
86
87     def get_filter_kind(self, *default: str) -> Callable[[str], bool]:
88         """ Return a filter function for the name kind from the 'filter-kind'
89             config parameter. The filter functions takes a name item and returns
90             True when the item passes the filter.
91
92             If the parameter is empty, the filter lets all items pass. If the
93             paramter is a string, it is interpreted as a single regular expression
94             that must match the full kind string. If the parameter is a list then
95             any of the regular expressions in the list must match to pass.
96         """
97         filters = self.get_string_list('filter-kind', default)
98
99         if not filters:
100             return lambda _: True
101
102         regexes = [re.compile(regex) for regex in filters]
103
104         return lambda name: any(regex.fullmatch(name) for regex in regexes)