Base Engine¶
pcapkit.foundation.engines.engine defines the abstract base class of
every extraction engine.
- class pcapkit.foundation.engines.engine.Engine[_T](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)
Changed in version 1.3.0:
nameandmoduleare properties, read from__engine_name__and__engine_module__or else from the class, where they were abstract class methods each engine implemented. A subclass registers itself withExtractorat class definition.Changed in version 1.5.0: Registration is opt-in, under the
engineclass keyword, which replaces 1.4.x’snamewith no alias; 1.4.x registered every subclass, under itsname(its__engine_name__, else its class name) whennamewas omitted (#514).See also
For customisation and extension, see Customisation & Extensions.
- 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.
- Return type:
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 guards on its owncodekeyword the same way.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
The keyword is
enginerather thannamebecause, below Python 3.11,abc.ABCMeta.__new__takesmcls,name,basesandnamespaceas positional-or-keyword parameters, so a class keyword by any of those names makes the class statement raiseTypeErrorbefore this method is reached.metaclassis unusable on every version, since theclassstatement consumes it.See also
For more details, please refer to
pcapkit.foundation.extraction.Extractor.register_engine().
- class pcapkit.foundation.engines.engine.EngineBase[_T](extractor)[source]¶
-
Base class for engine support.
Note
This class is for internal use only. For customisation, please use
Engineinstead.- property name: str¶
Engine name.
Note
Also a class variable, set by
__engine_name__.
- property module: str¶
Engine module name.
Note
Also a class variable, set by
__engine_module__.
- property registry: dict[str, ModuleDescriptor[EngineBase] | Type[EngineBase]]¶
Mapping of engine names to engine classes.
Note
A class variable only, defined on
EngineMeta. It is not per-class: it reads__engine__, the one table every engine registration lands in.
- _extractor¶
Extractor instance this engine reads for.
Defaults
- __reassembly__: dict[str, ModuleDescriptor[Callable[..., Any]] | Callable[..., Any]] = {}¶
Reassembly adapters. For each reassembler this engine supports, under the name it is registered by in
Extractor.__reassembly__, thepcapkit.toolkitfunction that makes its input from a frame. An enabled reassembler with no entry is disabled at startup, with a warning, and an engine with none supports no reassembly.Added in version 1.7.0.
- __traceflow__: dict[str, ModuleDescriptor[Callable[..., Any]] | Callable[..., Any]] = {}¶
Flow tracing adapters. For each flow tracer this engine supports, under the name it is registered by in
Extractor.__traceflow__, thepcapkit.toolkitfunction that makes its input from a frame. An enabled flow tracer with no entry is dropped at startup, with a warning, and an engine with none supports no flow tracing.Added in version 1.7.0.
Properties
Availability
- classmethod unsupported_reason()[source]¶
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.
Reassembly
- classmethod register_reassembly(protocol, adapter)[source]¶
Register the adapter feeding a reassembler from this engine’s frames.
- Parameters:
protocol (
str) – name the reassembler is registered by inExtractor.__reassembly__, lower-casedadapter (
ModuleDescriptor[Callable[...,Any]] |Callable[...,Any]) – module descriptor or callable, taking a frame as this engine’s own adapters do
- Raises:
RegistryError – If
adapteris neither a module descriptor nor callable.- Warns:
RegistryWarning– If a different adapter is already registered underprotocol; it is overwritten.
The entry is written on
clsalone, so a subclass registering one leaves the engine it derives from as it was.Added in version 1.7.0.
- _setup_reassembly(*, parse=False)[source]¶
Disable each enabled reassembler this engine has no adapter for.
- Parameters:
parse (
bool) – whether this engine parses its frames with the extraction’s stops and context; as the extraction’s engine, it then hands them to each reassembler kept, whose reassembled payloads are parsed with them as the frames’ next layers are (#2933)- Warns:
AttributeWarning– For each reassembler disabled; once, naming them all, if this engine supports no reassembly, which then stays off.- Return type:
- _frame_keywords()[source]¶
The stops and context this engine parses its frames with, those set.
- Return type:
- Returns:
The extraction’s
layer,protocolandcontextkeywords, only those set, so ananalyzetaking no keywords, as a custom__protocol_type__’s may, is called as before when the extraction sets none;Noneunless this engine is the extraction’s, since a third-party one runs this one’srunfor the global header alone (Extractor.record_header) and parses no frame with them.
- _reassemble(timestamp, *args, **kwargs)[source]¶
Feed a frame to every enabled reassembler.
- Parameters:
Each reassembler’s clock is first advanced to
timestamp, so a buffer expires once the capture passes its deadline whether or not the frame is its protocol’s. The adapter’s record, or each of a tuple of them, is then fed in.
Flow Tracing
- classmethod register_traceflow(protocol, adapter)[source]¶
Register the adapter feeding a flow tracer from this engine’s frames.
- Parameters:
protocol (
str) – name the flow tracer is registered by inExtractor.__traceflow__, lower-casedadapter (
ModuleDescriptor[Callable[...,Any]] |Callable[...,Any]) – module descriptor or callable, taking a frame as this engine’s own adapters do
- Raises:
RegistryError – If
adapteris neither a module descriptor nor callable.- Warns:
RegistryWarning– If a different adapter is already registered underprotocol; it is overwritten.
The entry is written on
clsalone, so a subclass registering one leaves the engine it derives from as it was.Added in version 1.7.0.
- _setup_traceflow(*, parse=False)[source]¶
Drop each enabled flow tracer this engine has no adapter for.
- Parameters:
parse (
bool) – whether this engine parses its frames with the extraction’s stops and context; as the extraction’s engine, it then hands them to each flow tracer kept, whoseanalyzereassemblers parse their payloads with them as the frames’ next layers are (#2946)- Warns:
AttributeWarning– For each flow tracer dropped; once, naming them all, if this engine supports no flow tracing.- Return type:
Dropping the last flow tracer turns flow tracing off.
- _traceflow(*args, **kwargs)[source]¶
Feed a frame to every enabled flow tracer.
- Parameters:
Each flow tracer is fed its adapter’s record, or each of a tuple of them.
Data Models
- __call__()[source]¶
Start extraction.
This method will directly call
run()to start the extraction process.
Methods
- abstractmethod run()[source]¶
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.
Internal Definitions¶
- class pcapkit.foundation.engines.engine.EngineMeta[_T](name, bases, namespace, /, **kwargs)[source]¶
-
Meta class to add dynamic support to
EngineBase.It provides
EngineBase.name,EngineBase.moduleandEngineBase.registryas class properties, the first two read from__engine_name__and__engine_module__where a subclass sets them.