Skip to content

Commit 9966567

Browse files
committed
New distribution [0.15.3]
* bugfix for #55 (ARP parsing issue) * added logging interation (as `pcapkit` logger)
1 parent c749bbd commit 9966567

File tree

18 files changed

+211
-28
lines changed

18 files changed

+211
-28
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
"nofile",
179179
"noindex",
180180
"nonr",
181+
"nosec",
181182
"nrec",
182183
"nsec",
183184
"nseq",

Pipfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ beautifulsoup4 = {extras = ["html5lib"],version = "*"}
2929
requests = {extras = ["socks"],version = "*"}
3030
ipython = "*"
3131
autopep8 = "*"
32+
bandit = "*"
33+
mypy = "*"
3234
pylint = "*"
3335
sphinx = "*"
34-
sphinxcontrib-napoleon = "*"
3536
doc8 = "*"
3637
rstcheck = "*"
3738

Pipfile.lock

Lines changed: 110 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/sphinx/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
author = 'Jarry Shaw'
2323

2424
# The full version, including alpha/beta/rc tags
25-
release = '0.15.2'
25+
release = '0.15.3'
2626

2727

2828
# -- General configuration ---------------------------------------------------

doc/sphinx/source/utilities/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ several user-refined exceptions and validations.
1414

1515
decorators
1616
exceptions
17+
logging
1718
validations
1819
warnings
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Logging System
2+
==============
3+
4+
:mod:`pcapkit.utilities.logging` contains naïve integration
5+
of the Python logging system, i.e. a :class:`logging.Logger`
6+
instance as :data:`~pcapkit.utilities.logging.logger`.
7+
8+
.. autodata:: pcapkit.utilities.logging.logger

pcapkit/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pcapkit.interface import JSON, PLIST, TREE
1616

1717
#: version number
18-
__version__ = '0.15.2'
18+
__version__ = '0.15.3'
1919

2020

2121
def get_parser():

pcapkit/corekit/protochain.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def __repr__(self):
386386
ProtoChain(<class 'pcapkit.protocols.link.ethernet.Ethernet'>, ...)
387387
388388
"""
389-
return f"ProtoChain({', '.join(self.__proto__.data)})"
389+
return f"ProtoChain({', '.join(map(lambda p: p.__name__, self.__proto__.data))})"
390390

391391
def __str__(self):
392392
"""Returns formatted hex representation of source data stream.
@@ -412,3 +412,15 @@ def __contains__(self, name):
412412
413413
"""
414414
return (name in self.__proto__) or (name in self.__alias__)
415+
416+
def __getitem__(self, index):
417+
"""Subscription (``getitem``) support.
418+
419+
Args:
420+
index (int): Indexing key.
421+
422+
Returns:
423+
str: Protocol alias at such index.
424+
425+
"""
426+
return self.__alias__[index]

pcapkit/foundation/extraction.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from pcapkit.utilities.compat import pathlib
3232
from pcapkit.utilities.exceptions import (CallableError, FileNotFound, FormatError, IterableError,
3333
UnsupportedCall, stacklevel)
34+
from pcapkit.utilities.logging import logger
3435
from pcapkit.utilities.warnings import (AttributeWarning, DPKTWarning, EngineWarning, FormatWarning,
3536
LayerWarning, ProtocolWarning)
3637

@@ -889,6 +890,7 @@ def _default_read_frame(self, *, frame=None, mpkit=None):
889890
self._frnum += 1
890891

891892
# verbose output
893+
logger.info(f'Frame {self._frnum:>3d}: {frame.protochain}') # pylint: disable=logging-fstring-interpolation
892894
if self._flag_v:
893895
if self._vfunc is NotImplemented:
894896
print(f' - Frame {self._frnum:>3d}: {frame.protochain}')
@@ -1000,6 +1002,7 @@ def _scapy_read_frame(self):
10001002
# verbose output
10011003
self._frnum += 1
10021004
self._proto = packet2chain(packet)
1005+
logger.info(f'Frame {self._frnum:>3d}: {self._proto}') # pylint: disable=logging-fstring-interpolation
10031006
if self._flag_v:
10041007
if self._vfunc is NotImplemented:
10051008
print(f' - Frame {self._frnum:>3d}: {self._proto}')
@@ -1116,6 +1119,7 @@ def _dpkt_read_frame(self):
11161119
# verbose output
11171120
self._frnum += 1
11181121
self._proto = packet2chain(packet)
1122+
logger.info(f'Frame {self._frnum:>3d}: {self._proto}') # pylint: disable=logging-fstring-interpolation
11191123
if self._flag_v:
11201124
if self._vfunc is NotImplemented:
11211125
print(f' - Frame {self._frnum:>3d}: {self._proto}')
@@ -1230,6 +1234,7 @@ def _pyshark_read_frame(self):
12301234
# verbose output
12311235
self._frnum = int(packet.number)
12321236
self._proto = packet.frame_info.protocols
1237+
logger.info(f'Frame {self._frnum:>3d}: {self._proto}') # pylint: disable=logging-fstring-interpolation
12331238
if self._flag_v:
12341239
if self._vfunc is NotImplemented:
12351240
print(f' - Frame {self._frnum:>3d}: {self._proto}')

pcapkit/protocols/link/arp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def read(self, length=None, **kwargs): # pylint: disable=unused-argument
163163
self._name = 'Address Resolution Protocol'
164164

165165
_htype = HRD.get(_hwty)
166-
if re.match(r'.*Ethernet.*', _htype, re.IGNORECASE):
166+
if re.match(r'.*Ethernet.*', _htype.name, re.IGNORECASE):
167167
_ptype = ETHERTYPE.get(_ptty)
168168
else:
169169
_ptype = f'Unknown [{_ptty}]'

pcapkit/protocols/protocol.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from pcapkit.utilities.decorators import beholder, seekset
3333
from pcapkit.utilities.exceptions import (ProtocolNotFound, ProtocolNotImplemented, ProtocolUnbound,
3434
StructError)
35+
from pcapkit.utilities.logging import logger
3536

3637
__all__ = ['Protocol']
3738

@@ -122,7 +123,7 @@ def protocol(self):
122123
:rtype: Optional[str]
123124
"""
124125
with contextlib.suppress(IndexError):
125-
return self._protos[1]
126+
return self._protos[0]
126127

127128
# protocol chain of current instance
128129
@property
@@ -266,6 +267,8 @@ def __init__(self, file=None, length=None, **kwargs):
266267
**kwargs: Arbitrary keyword arguments.
267268
268269
"""
270+
logger.debug(type(self).__name__)
271+
269272
#: bool: If the object is initiated after parsing errors.
270273
self._onerror = kwargs.pop('_error', False)
271274
#: str: Parse packet until such layer.

pcapkit/utilities/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from pcapkit.utilities.compat import * # pylint: disable=redefined-builtin
1313
from pcapkit.utilities.decorators import *
1414
from pcapkit.utilities.exceptions import *
15+
from pcapkit.utilities.logging import *
1516
from pcapkit.utilities.validations import *
1617
from pcapkit.utilities.warnings import *
1718

18-
__all__ = ['seekset_ng', 'beholder_ng']
19+
__all__ = ['logger', 'warn', 'stacklevel',
20+
'seekset_ng', 'beholder_ng']

pcapkit/utilities/decorators.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import os
1313
import traceback
1414

15+
from pcapkit.utilities.logging import logger
16+
1517
###############################################################################
1618
# from pcapkit.foundation.analysis import analyse
1719
# from pcapkit.protocols.raw import Raw
@@ -109,11 +111,14 @@ def behold(self, proto, length, *args, **kwargs):
109111
seek_cur = self._file.tell()
110112
try:
111113
return func(proto, length, *args, **kwargs)
112-
except Exception:
114+
except Exception as exc:
113115
from pcapkit.protocols.raw import Raw # pylint: disable=import-outside-toplevel
114116
error = traceback.format_exc(limit=1).strip().split(os.linesep)[-1]
115117
# error = traceback.format_exc()
116118

119+
# log error
120+
logger.error(error, exc_info=exc)
121+
117122
self._file.seek(seek_cur, os.SEEK_SET)
118123
next_ = Raw(io.BytesIO(self._read_fileng(length)), length, error=error)
119124
return next_
@@ -145,12 +150,15 @@ def behold(file, length, *args, **kwargs):
145150
seek_cur = file.tell()
146151
try:
147152
return func(file, length, *args, **kwargs)
148-
except Exception:
153+
except Exception as exc:
149154
# from pcapkit.foundation.analysis import analyse
150155
from pcapkit.protocols.raw import Raw # pylint: disable=import-outside-toplevel
151156
error = traceback.format_exc(limit=1).strip().split(os.linesep)[-1]
152157
# error = traceback.format_exc()
153158

159+
# log error
160+
logger.error(error, exc_info=exc)
161+
154162
file.seek(seek_cur, os.SEEK_SET)
155163

156164
# raw = Raw(file, length, error=str(error))

0 commit comments

Comments
 (0)