# -*- coding: utf-8 -*-"""Base Class================.. module:: pcapkit.foundation.engines.engineThis is the abstract base class implementation forall engine support functionality."""importabcfromtypingimportTYPE_CHECKING,Generic,TypeVar,cast__all__=['Engine']ifTYPE_CHECKING:fromtypingimportAny,Optionalfrompcapkit.foundation.extractionimportExtractor_T=TypeVar('_T')
[docs]classEngineMeta(abc.ABCMeta,Generic[_T]):"""Meta class to add dynamic support to :class:`EngineBase`. This meta class is used to generate necessary attributes for the :class:`EngineBase` class. It can be useful to reduce unnecessary registry calls and simplify the customisation process. """ifTYPE_CHECKING:#: Engine name.__engine_name__:'str'#: Engine module name.__engine_module__:'str'@propertydefname(cls)->'str':"""Engine name."""ifhasattr(cls,'__engine_name__'):returncls.__engine_name__returncls.__name__@propertydefmodule(cls)->'str':"""Engine module name."""ifhasattr(cls,'__engine_module__'):returncls.__engine_module__returncls.__module__
[docs]classEngineBase(Generic[_T],metaclass=EngineMeta):"""Base class for engine support. Args: extractor: :class:`~pcapkit.foundation.extraction.Extractor` instance. Note: This class is for internal use only. For customisation, please use :class:`Engine` instead. """ifTYPE_CHECKING:#: Engine name.__engine_name__:'str'#: Engine module name.__engine_module__:'str'########################################################################### Properties.##########################################################################@propertydefname(self)->'str':"""Engine name. Note: This property is not available as a class attribute. """ifhasattr(self,'__engine_name__'):returnself.__engine_name__returntype(self).name# type: ignore[return-value]@propertydefmodule(self)->'str':"""Engine module name. Note: This property is not available as a class attribute. """ifhasattr(self,'__engine_module__'):returnself.__engine_module__returntype(self).module# type: ignore[return-value]@propertydefextractor(self)->'Extractor':"""Extractor instance."""returnself._extractor########################################################################### Data models.##########################################################################def__init__(self,extractor:'Extractor')->'None':self._extractor=extractordef__call__(self)->'None':"""Start extraction. This method will directly call :meth:`run` to start the extraction process. """self.run()########################################################################### Methods.##########################################################################@abc.abstractmethoddefrun(self)->'None':"""Start extraction. This method is the entry point for file extraction. It is to be used for preparing the extraction process, such as parsing the file header and setting up the extraction engines. """@abc.abstractmethoddefread_frame(self)->'_T':"""Read frame. This method is to be used for reading a frame from the file. It is to read a frame from the file using the prepared engine instance and return the parsed frame. """defclose(self)->'None':"""Close engine. This method is to be used for closing the engine instance. It is to close the engine instance after the extraction process is finished. """
[docs]classEngine(EngineBase[_T],Generic[_T]):"""Base class for engine support. Example: Use keyword argument ``name`` to specify the engine name at class definition: .. code-block:: python class MyEngine(Engine, name='my_engine'): ... Args: extractor: :class:`~pcapkit.foundation.extraction.Extractor` instance. """
[docs]def__init_subclass__(cls,/,name:'Optional[str]'=None,*args:'Any',**kwargs:'Any')->'None':"""Initialise subclass. This method is to be used for registering the engine class to :class:`~pcapkit.foundation.extraction.Extractor` class. Args: name: Engine name, default to class name. *args: Arbitrary positional arguments. **kwargs: Arbitrary keyword arguments. See Also: For more details, please refer to :meth:`pcapkit.foundation.extraction.Extractor.register_engine`. """ifnameisNone:name=cast('str',cls.name)frompcapkit.foundation.extractionimportExtractorExtractor.register_engine(name.lower(),cls)returnsuper().__init_subclass__()