Base Engine¶
This is the abstract base class implementation for all engine support functionality.
- class pcapkit.foundation.engines.engine.Engine(extractor)[source]¶
Bases:
EngineBase[_T],Generic[_T]Base class for engine support.
Example
Registration is opt-in. Pass keyword argument
engineat class definition to register the engine under that name:class MyEngine(Engine, engine='my_engine'): ...
Omit it and the subclass is not registered, which is how a class that is not meant to be selectable by name declines:
class MyMixin(Engine): # not registered ...
Such a class can still be registered later, on demand:
Extractor.register_engine('my_mixin', MyMixin)
See also
For more information on customisation and extension, please refer to Customisation & Extensions.
- property name: str¶
Engine name.
Note
This property is also available as a class variable. Its value can be set by
__engine_name__class attribute.
- property module: str¶
Engine module name.
Note
This property is also available as a class variable. Its value can be set by
__engine_module__class attribute.
- property registry: python:dict[str, ModuleDescriptor[EngineBase] | typing.Type[EngineBase]]¶
Mapping of engine names to engine classes.
Note
This property is only available as a class variable, since it is defined on
EngineMeta. It reads__engine__, the single table every engine registration lands in, so it is not a per-class mapping.
- _extractor¶
- classmethod unsupported_reason()¶
Why this engine cannot run in this environment, if it cannot.
Engines are normally gated by whether their third-party module imports, which
pcapkit.foundation.extraction.Extractor.import_test()decides. This hook is for the cases that question cannot answer – most often a dependency that installs cleanly and only fails when it is used, so the import test passes and the error escapes from the engine’s constructor as a hard failure instead of degrading to the default engine.The default is
None, i.e. always available; override it only where there is a real limitation, and return a short phrase naming the cause rather than merely refusing, since the string is shown to the user.- Return type:
- Returns:
A phrase describing the limitation, or
Nonewhen the engine is usable here.
See also
pcapkit.foundation.engines.pypcapfile.PyPCAPFileoverrides this, becausepypcapfileinstalls on Python 3.12 and newer and then raisesModuleNotFoundErroron first use.
- abstractmethod run()¶
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.
- abstractmethod read_frame()¶
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.
- Return type:
TypeVar(_T)
- close()¶
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.
- classmethod __init_subclass__(engine=None, *args, **kwargs)[source]¶
Initialise subclass.
This method is to be used for registering the engine class to
Extractorclass.- Parameters:
- Raises:
UnsupportedCall – If any unrecognised class keyword is given.
Registration is opt-in: the subclass is registered if and only if
engineis given. This is what lets a subclass decline registration rather than having to inheritEngineBaseto avoid it, and it matchesEnumSchema.__init_subclass__, which has guarded on its owncodekeyword all along.Note
__engine_name__is not an opt-in. It supplies thenamethe engine reports, which it does whether or not the engine is registered; only the keyword decides registration.Note
This keyword was
namewhen opt-in registration landed, and was renamed becausenamecannot be passed as a class keyword at all on Python 3.10:abc.ABCMeta.__new__()takesmcls,name,basesandnamespaceas positional-or-keyword parameters before 3.11, so a class keyword by any of those four names collides with one of them and the class statement raisesTypeErrorfrom the metaclass before this method is reached.engineis outside that set, so the documented registration path now works on every supported version. Measured on 3.10.21, 3.11.15 and 3.14.7; those four are the whole of theabc.ABCMeta.__new__()collision surface. Separately, and for an unrelated reason that holds on every version,metaclassis not usable as a class keyword either: aclassstatement consumes it to choose the metaclass, so it never reaches this method at all.See also
For more details, please refer to
pcapkit.foundation.extraction.Extractor.register_engine().
Internal Definitions¶
- class pcapkit.foundation.engines.engine.EngineBase(extractor)[source]¶
-
Base class for engine support.
Note
This class is for internal use only. For customisation, please use
Engineinstead.
- class pcapkit.foundation.engines.engine.EngineMeta(name, bases, namespace, /, **kwargs)[source]¶
-
Meta class to add dynamic support to
EngineBase.This meta class is used to generate necessary attributes for the
EngineBaseclass. It can be useful to reduce unnecessary registry calls and simplify the customisation process.