167
167
if TYPE_CHECKING :
168
168
from decimal import Decimal
169
169
from typing import IO , Any , Callable , DefaultDict , Optional , Type , Union
170
+ from enum import IntEnum as StdlibEnum
171
+ from aenum import IntEnum as AenumEnum
170
172
171
173
from mypy_extensions import DefaultArg , KwArg , NamedArg
172
174
from typing_extensions import Literal
@@ -250,7 +252,7 @@ class PCAPNG(Protocol[Data_PCAPNG, Schema_PCAPNG],
250
252
"""PCAP-NG file block extractor.
251
253
252
254
The class currently supports parsing of the following protocols, which are
253
- registered in the :attr:`self.__proto__ <pcapkit.protocols.misc.pcap.frame.Frame .__proto__>`
255
+ registered in the :attr:`self.__proto__ <pcapkit.protocols.misc.pcapng.PCAPNG .__proto__>`
254
256
attribute:
255
257
256
258
.. list-table::
@@ -792,16 +794,53 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True,
792
794
return block
793
795
794
796
def make (self ,
797
+ type : 'Enum_BlockType | StdlibEnum | AenumEnum | str | int' = Enum_BlockType .Simple_Packet_Block ,
798
+ type_default : 'Optional[int]' = None ,
799
+ type_namespace : 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None , # pylint: disable=line-too-long
800
+ type_reversed : 'bool' = False ,
801
+ block : 'bytes | Data_PCAPNG | Schema_BlockType | dict[str, Any]' = b'' ,
795
802
** kwargs : 'Any' ) -> 'Schema_PCAPNG' :
796
- """Make frame packet data.
803
+ """Make PCAP-NG block data.
797
804
798
805
Args:
806
+ type: Block type.
807
+ type_default: Default block type.
808
+ type_namespace: Block type namespace.
809
+ type_reversed: Whether to reverse block type namespace.
810
+ block: Block data.
799
811
**kwargs: Arbitrary keyword arguments.
800
812
801
813
Returns:
802
814
Constructed packet data.
803
815
804
816
"""
817
+ type_val = self ._make_index (type , type_default , namespace = type_namespace , # type: ignore[call-overload]
818
+ reversed = type_reversed , pack = False )
819
+
820
+ if isinstance (block , bytes ):
821
+ block_val = block # type: bytes | Schema_BlockType
822
+ elif isinstance (block , (dict , Data_PCAPNG )):
823
+ name = self .__block__ [type_val ]
824
+ if isinstance (name , str ):
825
+ meth_name = f'_make_block_{ name } '
826
+ meth = cast ('BlockConstructor' ,
827
+ getattr (self , meth_name , self ._make_block_unknown ))
828
+ else :
829
+ meth = name [1 ]
830
+
831
+ if isinstance (block , dict ):
832
+ block_val = meth (type_val , ** block )
833
+ else :
834
+ block_val = meth (type_val , block )
835
+ elif isinstance (block , Schema ):
836
+ block_val = block
837
+ else :
838
+ raise ProtocolError (f'PCAP-NG: [Type { type_val } ] invalid format' )
839
+
840
+ return Schema_PCAPNG (
841
+ type = type_val ,
842
+ block = block_val ,
843
+ )
805
844
806
845
##########################################################################
807
846
# Data models.
@@ -833,7 +872,7 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
833
872
For construction argument, please refer to :meth:`make`.
834
873
835
874
"""
836
- #: int: frame index number
875
+ #: int: Block index number.
837
876
self ._fnum = num
838
877
#: pcapkit.foundation.engins.pcapng.Context: Context of the PCAP-NG file.
839
878
self ._ctx = ctx
@@ -860,15 +899,15 @@ def __length_hint__(self) -> 'Literal[12]':
860
899
# NOTE: This is a hack to make the ``__index__`` method work both as a
861
900
# class method and an instance method.
862
901
def __index__ (self : 'Optional[PCAPNG]' = None ) -> 'int' : # type: ignore[override]
863
- """Index of the frame .
902
+ """Index of the block .
864
903
865
904
Args:
866
905
self: :class:`PCAPNG` object or :obj:`None`.
867
906
868
907
Returns:
869
- If the object is initiated, i.e. :attr:`self._fnum <pcapkit.protocols.misc.pcap.frame.Frame ._fnum>`
908
+ If the object is initiated, i.e. :attr:`self._fnum <pcapkit.protocols.misc.pcapng.PCAPNG ._fnum>`
870
909
exists, and is of a packet block (EPB, ISB or Packet), returns the
871
- frame index number of itself; else raises :exc:`UnsupportedCall`.
910
+ block index number of itself; else raises :exc:`UnsupportedCall`.
872
911
873
912
Raises:
874
913
UnsupportedCall: This protocol has no registry entry.
0 commit comments