|
13 | 13 | from sifter.grammar.comparator import Comparator |
14 | 14 |
|
15 | 15 |
|
16 | | -_HANDLERS_MAP: Dict[Text, Dict[Text, Union[bool, Type['Comparator'], Type['Rule']]]] = {} |
| 16 | +class ExtensionRegistry(): |
17 | 17 |
|
| 18 | + _HANDLERS_MAP: Dict[Text, Dict[Text, Union[bool, Type['Comparator'], Type['Rule']]]] = {} |
| 19 | + DEFAULT_EXTENSION = [ |
| 20 | + 'regex', |
| 21 | + 'comparator-i;ascii-casemap', |
| 22 | + 'comparator-i;octet', |
| 23 | + 'fileinto', |
| 24 | + ] |
18 | 25 |
|
19 | | -def register( |
20 | | - handler_type: Optional[Text], |
21 | | - handler_id: Optional[Text], |
22 | | - value: Union[bool, Type['Comparator'], Type['Rule']] |
23 | | -) -> None: |
24 | | - from sifter.grammar.rule import Rule |
25 | | - from sifter.grammar.comparator import Comparator |
26 | | - if not handler_type or not handler_id: |
27 | | - raise ValueError("handler_type and handler_id must not be None!") |
28 | | - if not isinstance(value, Rule) and not (isinstance(value, type) and issubclass(value, (Rule, Comparator))): |
29 | | - print(type(value)) |
30 | | - _HANDLERS_MAP.setdefault(handler_type, {})[handler_id] = value |
31 | | - else: |
32 | | - for entry_point in pkg_resources.iter_entry_points('sifter_extensions'): |
33 | | - if entry_point.name not in _HANDLERS_MAP: |
34 | | - ext_cls = entry_point.load() |
35 | | - _HANDLERS_MAP.setdefault(ext_cls.get_mapkey(), {})[ext_cls.get_identifier()] = ext_cls |
36 | | - _HANDLERS_MAP[entry_point.name] = entry_point.load() |
37 | | - |
| 26 | + def __init__(self) -> None: |
| 27 | + for extension_name in self.DEFAULT_EXTENSION: |
| 28 | + self.register_extension(extension_name) |
38 | 29 |
|
39 | | -def unregister(handler_type: Text, handler_id: Text) -> Optional[Union[bool, Type['Comparator'], Type['Rule']]]: |
40 | | - return _HANDLERS_MAP.get(handler_type, {}).pop(handler_id, None) |
41 | | - |
42 | | - |
43 | | -def get(handler_type: Text, handler_id: Text) -> Optional[Union[bool, Type['Comparator'], Type['Rule']]]: |
44 | | - return _HANDLERS_MAP.get(handler_type, {}).get(handler_id, None) |
| 30 | + for entry_point in pkg_resources.iter_entry_points('sifter_extensions'): |
| 31 | + self.register_handler(entry_point.load()) |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def register_extension(cls, extension_name): |
| 35 | + cls.register('extension', extension_name, True) |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def register_handler(cls, ext_cls): |
| 39 | + cls.register(ext_cls.handler_type(), ext_cls.handler_id(), ext_cls) |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def register( |
| 43 | + cls, |
| 44 | + handler_type: Optional[Text], |
| 45 | + handler_id: Optional[Text], |
| 46 | + value: Union[bool, Type['Comparator'], Type['Rule']] |
| 47 | + ) -> None: |
| 48 | + cls._HANDLERS_MAP.setdefault(handler_type, {})[handler_id] = value |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def unregister(cls, handler_type: Text, handler_id: Text) -> Optional[Union[bool, Type['Comparator'], Type['Rule']]]: |
| 52 | + return cls._HANDLERS_MAP.get(handler_type, {}).pop(handler_id, None) |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def get(cls, handler_type: Text, handler_id: Text) -> Optional[Union[bool, Type['Comparator'], Type['Rule']]]: |
| 56 | + return cls._HANDLERS_MAP.get(handler_type, {}).get(handler_id, None) |
0 commit comments