|
1 | 1 | import re
|
2 | 2 |
|
3 | 3 | from .base import ResponseMicroService
|
| 4 | +from ..exception import SATOSAError |
4 | 5 |
|
5 | 6 |
|
6 | 7 | class AddStaticAttributes(ResponseMicroService):
|
@@ -40,17 +41,27 @@ def process(self, context, data):
|
40 | 41 | def _apply_requester_filters(self, attributes, provider_filters, requester):
|
41 | 42 | # apply default requester filters
|
42 | 43 | default_requester_filters = provider_filters.get("", {})
|
43 |
| - self._apply_filter(attributes, default_requester_filters) |
| 44 | + self._apply_filters(attributes, default_requester_filters) |
44 | 45 |
|
45 | 46 | # apply requester specific filters
|
46 | 47 | requester_filters = provider_filters.get(requester, {})
|
47 |
| - self._apply_filter(attributes, requester_filters) |
48 |
| - |
49 |
| - def _apply_filter(self, attributes, attribute_filters): |
50 |
| - for attribute_name, attribute_filter in attribute_filters.items(): |
51 |
| - regex = re.compile(attribute_filter) |
52 |
| - if attribute_name == "": # default filter for all attributes |
53 |
| - for attribute, values in attributes.items(): |
54 |
| - attributes[attribute] = list(filter(regex.search, attributes[attribute])) |
55 |
| - elif attribute_name in attributes: |
56 |
| - attributes[attribute_name] = list(filter(regex.search, attributes[attribute_name])) |
| 48 | + self._apply_filters(attributes, requester_filters) |
| 49 | + |
| 50 | + def _apply_filters(self, attributes, attribute_filters): |
| 51 | + for attribute_name, attribute_filters in attribute_filters.items(): |
| 52 | + if type(attribute_filters) == str: |
| 53 | + # convert simple notation to filter list |
| 54 | + attribute_filters = {'regexp': attribute_filters} |
| 55 | + |
| 56 | + for filter_type, filter_value in attribute_filters.items(): |
| 57 | + |
| 58 | + if filter_type == "regexp": |
| 59 | + filter_func = re.compile(filter_value).search |
| 60 | + else: |
| 61 | + raise SATOSAError("Unknown filter type") |
| 62 | + |
| 63 | + if attribute_name == "": # default filter for all attributes |
| 64 | + for attribute, values in attributes.items(): |
| 65 | + attributes[attribute] = list(filter(filter_func, attributes[attribute])) |
| 66 | + elif attribute_name in attributes: |
| 67 | + attributes[attribute_name] = list(filter(filter_func, attributes[attribute_name])) |
0 commit comments