Skip to content

Commit b12cfd1

Browse files
committed
PEP8 + typing
1 parent b9a3902 commit b12cfd1

10 files changed

+97
-120
lines changed

src/pyshark/capture/capture.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
from packaging import version
99

10+
from pyshark.packet.packet import Packet
1011
from pyshark.tshark.tshark import get_process_path, get_tshark_display_filter_flag, \
1112
tshark_supports_json, TSharkVersionException, get_tshark_version, tshark_supports_duplicate_keys
1213
from pyshark.tshark.tshark_json import packet_from_json_packet
@@ -36,7 +37,7 @@ class StopCapture(Exception):
3637
pass
3738

3839

39-
class Capture(object):
40+
class Capture:
4041
"""Base class for packet captures."""
4142
DEFAULT_BATCH_SIZE = 2 ** 16
4243
SUMMARIES_BATCH_SIZE = 64
@@ -99,12 +100,12 @@ def __getitem__(self, item):
99100
def __len__(self):
100101
return len(self._packets)
101102

102-
def next(self):
103+
def next(self) -> Packet:
103104
return self.next_packet()
104105

105106
# Allows for child classes to call next() from super() without 2to3 "fixing"
106107
# the call
107-
def next_packet(self):
108+
def next_packet(self) -> Packet:
108109
if self._current_packet >= len(self._packets):
109110
raise StopIteration()
110111
cur_packet = self._packets[self._current_packet]

src/pyshark/capture/file_capture.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from pyshark.capture.capture import Capture
4+
from pyshark.packet.packet import Packet
45

56

67
class FileCapture(Capture):
@@ -50,7 +51,7 @@ def __init__(self, input_file=None, keep_packets=True, display_filter=None, only
5051
self.keep_packets = keep_packets
5152
self._packet_generator = self._packets_from_tshark_sync()
5253

53-
def next(self):
54+
def next(self) -> Packet:
5455
"""Returns the next packet in the cap.
5556
5657
If the capture's keep_packets flag is True, will also keep it in the internal packet list.

src/pyshark/capture/live_capture.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import asyncio
3-
import sys
43
from packaging import version
54

65
from pyshark.capture.capture import Capture
@@ -54,9 +53,7 @@ def __init__(self, interface=None, bpf_filter=None, display_filter=None, only_su
5453
self.interfaces = interface
5554

5655
def get_parameters(self, packet_count=None):
57-
"""
58-
Returns the special tshark parameters to be used according to the configuration of this class.
59-
"""
56+
"""Returns the special tshark parameters to be used according to the configuration of this class."""
6057
params = super(LiveCapture, self).get_parameters(packet_count=packet_count)
6158
# Read from STDIN
6259
params += ["-i", "-"]

src/pyshark/capture/live_ring_capture.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33

44
class LiveRingCapture(LiveCapture):
5-
"""
6-
Represents a live ringbuffer capture on a network interface.
7-
"""
5+
"""Represents a live ringbuffer capture on a network interface."""
86

97
def __init__(self, ring_file_size=1024, num_ring_files=1, ring_file_name='/tmp/pyshark.pcap', interface=None,
108
bpf_filter=None, display_filter=None, only_summaries=False, decryption_key=None,
@@ -42,9 +40,7 @@ def __init__(self, ring_file_size=1024, num_ring_files=1, ring_file_name='/tmp/p
4240
self.ring_file_name = ring_file_name
4341

4442
def get_parameters(self, packet_count=None):
45-
"""
46-
Returns the special tshark parameters to be used according to the configuration of this class.
47-
"""
4843
params = super(LiveRingCapture, self).get_parameters(packet_count=packet_count)
49-
params += ['-b', 'filesize:' + str(self.ring_file_size), '-b', 'files:' + str(self.num_ring_files), '-w', self.ring_file_name, '-P']
44+
params += ['-b', 'filesize:' + str(self.ring_file_size), '-b', 'files:' + str(self.num_ring_files),
45+
'-w', self.ring_file_name, '-P']
5046
return params

src/pyshark/capture/pipe_capture.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from pyshark.capture.capture import Capture
21
import os
32

3+
from pyshark.capture.capture import Capture
4+
5+
46
class PipeCapture(Capture):
57
def __init__(self, pipe, display_filter=None, only_summaries=False,
68
decryption_key=None, encryption_type='wpa-pwk', decode_as=None,
7-
disable_protocol=None, tshark_path=None, override_prefs=None, use_json=False, include_raw=False,
8-
eventloop=None, custom_parameters=None, debug=False):
9-
"""
10-
Receives a file-like and reads the packets from there (pcap format).
9+
disable_protocol=None, tshark_path=None, override_prefs=None, use_json=False,
10+
include_raw=False, eventloop=None, custom_parameters=None, debug=False):
11+
"""Receives a file-like and reads the packets from there (pcap format).
1112
1213
:param bpf_filter: BPF filter to use on packets.
1314
:param display_filter: Display (wireshark) filter to use.

src/pyshark/capture/remote_capture.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33

44
class RemoteCapture(LiveCapture):
5-
"""
6-
A capture which is performed on a remote machine which has an rpcapd service running.
7-
"""
5+
"""A capture which is performed on a remote machine which has an rpcapd service running."""
86

97
def __init__(self, remote_host, remote_interface, remote_port=2002, bpf_filter=None, only_summaries=False,
108
decryption_key=None, encryption_type='wpa-pwk', decode_as=None,

src/pyshark/packet/fields.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import binascii
2+
import typing
23

34
from pyshark.packet.common import Pickleable, SlotsPickleable
45

56

67
class LayerField(SlotsPickleable):
7-
"""
8-
Holds all data about a field of a layer, both its actual value and its name and nice representation.
9-
"""
8+
"""Holds all data about a field of a layer, both its actual value and its name and nice representation."""
9+
1010
# Note: We use this object with slots and not just a dict because
1111
# it's much more memory-efficient (cuts about a third of the memory).
1212
__slots__ = ['name', 'showname', 'raw_value', 'show', 'hide', 'pos', 'size', 'unmaskedvalue']
@@ -28,10 +28,8 @@ def __init__(self, name=None, showname=None, value=None, show=None, hide=None, p
2828
def __repr__(self):
2929
return '<LayerField %s: %s>' % (self.name, self.get_default_value())
3030

31-
def get_default_value(self):
32-
"""
33-
Gets the best 'value' string this field has.
34-
"""
31+
def get_default_value(self) -> str:
32+
"""Gets the best 'value' string this field has."""
3533
val = self.show
3634
if not val:
3735
val = self.raw_value
@@ -40,50 +38,47 @@ def get_default_value(self):
4038
return val
4139

4240
@property
43-
def showname_value(self):
44-
"""
45-
For fields which do not contain a normal value, we attempt to take their value from the showname.
46-
"""
41+
def showname_value(self) -> typing.Union[str, None]:
42+
"""The "pretty value" (as displayed by Wireshark) of the field."""
4743
if self.showname and ': ' in self.showname:
4844
return self.showname.split(': ', 1)[1]
45+
return None
4946

5047
@property
51-
def showname_key(self):
48+
def showname_key(self) -> typing.Union[str, None]:
49+
"""The "pretty name" (as displayed by Wireshark) of the field."""
5250
if self.showname and ': ' in self.showname:
5351
return self.showname.split(': ', 1)[0]
52+
return None
5453

5554
@property
56-
def binary_value(self):
57-
"""
58-
Converts this field to binary (assuming it's a binary string)
59-
"""
55+
def binary_value(self) -> bytes:
56+
"""Converts this field to binary (assuming it's a binary string)"""
6057
str_raw_value = str(self.raw_value)
6158
if len(str_raw_value) % 2 == 1:
6259
str_raw_value = '0' + str_raw_value
6360

6461
return binascii.unhexlify(str_raw_value)
6562

6663
@property
67-
def int_value(self):
68-
"""
69-
Returns the int value of this field (assuming it's an integer integer).
70-
"""
64+
def int_value(self) -> int:
65+
"""Returns the int value of this field (assuming it's represented as a decimal integer)."""
7166
return int(self.raw_value)
7267

7368
@property
74-
def hex_value(self):
75-
"""
76-
Returns the int value of this field if it's in base 16 (either as a normal number or in
77-
a "0xFFFF"-style hex value)
69+
def hex_value(self) -> int:
70+
"""Returns the int value of this field if it's in base 16
71+
72+
(either as a normal number or in a "0xFFFF"-style hex value)
7873
"""
7974
return int(self.raw_value, 16)
8075

8176
base16_value = hex_value
8277

8378

8479
class LayerFieldsContainer(str, Pickleable):
85-
"""
86-
An object which contains one or more fields (of the same name).
80+
"""An object which contains one or more fields (of the same name).
81+
8782
When accessing member, such as showname, raw_value, etc. the appropriate member of the main (first) field saved
8883
in this container will be shown.
8984
"""
@@ -102,23 +97,19 @@ def __dir__(self):
10297
def add_field(self, field):
10398
self.fields.append(field)
10499

100+
@property
101+
def all_fields(self):
102+
"""Returns all fields in a list, the main field followed by the alternate fields."""
103+
return self.fields
104+
105105
@property
106106
def main_field(self):
107107
return self.fields[0]
108108

109109
@property
110110
def alternate_fields(self):
111-
"""
112-
Return the alternate values of this field containers (non-main ones).
113-
"""
111+
"""Return the alternate values of this field containers (non-main ones)."""
114112
return self.fields[1:]
115113

116-
@property
117-
def all_fields(self):
118-
"""
119-
Returns all fields in a list, the main field followed by the alternate fields.
120-
"""
121-
return self.fields
122-
123114
def __getattr__(self, item):
124115
return getattr(self.main_field, item)

0 commit comments

Comments
 (0)