|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Foundation Registries |
| 3 | +=========================== |
| 4 | +
|
| 5 | +.. module:: pcapkit.foundation.registry.foundation |
| 6 | +
|
| 7 | +This module provides the foundation registries for :mod:`pcapkit`. |
| 8 | +
|
| 9 | +""" |
| 10 | +import importlib |
| 11 | + |
| 12 | +from dictdumper import Dumper |
| 13 | + |
| 14 | +from pcapkit.foundation.engines import Engine |
| 15 | +from pcapkit.foundation.extraction import Extractor |
| 16 | +from pcapkit.foundation.traceflow import TraceFlow |
| 17 | +from pcapkit.utilities.exceptions import RegistryError |
| 18 | +from pcapkit.utilities.logging import logger |
| 19 | + |
| 20 | +__all__ = [ |
| 21 | + 'register_extractor_engine', |
| 22 | + |
| 23 | + 'register_dumper', |
| 24 | + 'register_extractor_dumper', 'register_traceflow_dumper', |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +############################################################################### |
| 29 | +# Engine Registries |
| 30 | +############################################################################### |
| 31 | + |
| 32 | + |
| 33 | +# NOTE: pcapkit.foundation.extraction.Extractor.__engine__ |
| 34 | +def register_extractor_engine(engine: 'str', module: 'str', class_: 'str') -> 'None': # pylint: disable=redefined-builtin |
| 35 | + r"""Registered a new engine class. |
| 36 | +
|
| 37 | + Notes: |
| 38 | + The full qualified class name of the new engine class |
| 39 | + should be as ``{module}.{class_}``. |
| 40 | +
|
| 41 | + The function will register the given engine class to the |
| 42 | + :data:`pcapkit.foundation.extraction.Extractor.__engine__` registry. |
| 43 | +
|
| 44 | + Arguments: |
| 45 | + engine: engine name |
| 46 | + module: module name |
| 47 | + class\_: class name |
| 48 | +
|
| 49 | + """ |
| 50 | + engine_cls = getattr(importlib.import_module(module), class_) |
| 51 | + if not issubclass(engine_cls, Engine): |
| 52 | + raise RegistryError('engine must be a Engine subclass') |
| 53 | + |
| 54 | + Extractor.register_engine(engine, module, class_) |
| 55 | + logger.info('registered extractor engine: %s', engine) |
| 56 | + |
| 57 | + |
| 58 | +############################################################################### |
| 59 | +# Dumper Registries |
| 60 | +############################################################################### |
| 61 | + |
| 62 | + |
| 63 | +def register_dumper(format: 'str', module: 'str', class_: 'str', ext: 'str') -> 'None': # pylint: disable=redefined-builtin |
| 64 | + r"""Registered a new dumper class. |
| 65 | +
|
| 66 | + Notes: |
| 67 | + The full qualified class name of the new dumper class |
| 68 | + should be as ``{module}.{class_}``. |
| 69 | +
|
| 70 | + The function will register the given dumper class to the |
| 71 | + :data:`pcapkit.foundation.traceflow.TraceFlow.__output__` and |
| 72 | + :data:`pcapkit.foundation.extraction.Extractor.__output__` registry. |
| 73 | +
|
| 74 | + See Also: |
| 75 | + * :func:`pcapkit.foundation.registry.register_extractor_dumper` |
| 76 | + * :func:`pcapkit.foundation.registry.register_traceflow` |
| 77 | +
|
| 78 | + Arguments: |
| 79 | + format: format name |
| 80 | + module: module name |
| 81 | + class\_: class name |
| 82 | + ext: file extension |
| 83 | +
|
| 84 | + """ |
| 85 | + dumper = getattr(importlib.import_module(module), class_) |
| 86 | + if not issubclass(dumper, Dumper): |
| 87 | + raise RegistryError('dumper must be a Dumper subclass') |
| 88 | + |
| 89 | + Extractor.register_dumper(format, module, class_, ext) |
| 90 | + TraceFlow.register(format, module, class_, ext) |
| 91 | + logger.info('registered output format: %s', dumper.__name__) |
| 92 | + |
| 93 | + |
| 94 | +# NOTE: pcapkit.foundation.extraction.Extractor.__output__ |
| 95 | +def register_extractor_dumper(format: 'str', module: 'str', class_: 'str', ext: 'str') -> 'None': # pylint: disable=redefined-builtin |
| 96 | + r"""Registered a new dumper class. |
| 97 | +
|
| 98 | + Notes: |
| 99 | + The full qualified class name of the new dumper class |
| 100 | + should be as ``{module}.{class_}``. |
| 101 | +
|
| 102 | + The function will register the given dumper class to the |
| 103 | + :data:`pcapkit.foundation.extraction.Extractor.__output__` registry. |
| 104 | +
|
| 105 | + Arguments: |
| 106 | + format: format name |
| 107 | + module: module name |
| 108 | + class\_: class name |
| 109 | + ext: file extension |
| 110 | +
|
| 111 | + """ |
| 112 | + dumper = getattr(importlib.import_module(module), class_) |
| 113 | + if not issubclass(dumper, Dumper): |
| 114 | + raise RegistryError('dumper must be a Dumper subclass') |
| 115 | + |
| 116 | + Extractor.register_dumper(format, module, class_, ext) |
| 117 | + logger.info('registered extractor output dumper: %s', format) |
| 118 | + |
| 119 | + |
| 120 | +# NOTE: pcapkit.foundation.traceflow.traceflow.TraceFlow.__output__ |
| 121 | +def register_traceflow_dumper(format: 'str', module: 'str', class_: 'str', ext: 'str') -> 'None': # pylint: disable=redefined-builtin |
| 122 | + r"""Registered a new dumper class. |
| 123 | +
|
| 124 | + Notes: |
| 125 | + The full qualified class name of the new dumper class |
| 126 | + should be as ``{module}.{class_}``. |
| 127 | +
|
| 128 | + The function will register the given dumper class to the |
| 129 | + :data:`pcapkit.foundation.traceflow.traceflow.TraceFlow.__output__` registry. |
| 130 | +
|
| 131 | + Arguments: |
| 132 | + format: format name |
| 133 | + module: module name |
| 134 | + class\_: class name |
| 135 | + ext: file extension |
| 136 | +
|
| 137 | + """ |
| 138 | + dumper = getattr(importlib.import_module(module), class_) |
| 139 | + if not issubclass(dumper, Dumper): |
| 140 | + raise RegistryError('dumper must be a Dumper subclass') |
| 141 | + |
| 142 | + TraceFlow.register(format, module, class_, ext) |
| 143 | + logger.info('registered traceflow output: %s', format) |
0 commit comments