Skip to content

Commit

Permalink
Fix flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
akorb committed Apr 20, 2020
1 parent fce3164 commit a3bd9db
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
3 changes: 2 additions & 1 deletion scapy/tools/packet_viewer/columns_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(

# Last the fields of the specified cls
self.columns += [
PacketListColumn(field.name, max(10, len(field.name) + 1), lambda p, name=field.name: p.getfieldval(name))
PacketListColumn(field.name, max(10, len(field.name) + 1),
lambda p, name=field.name: p.getfieldval(name))
for field in cls.fields_desc]

self._format_string = self._create_format_string()
Expand Down
3 changes: 2 additions & 1 deletion scapy/tools/packet_viewer/command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def execute_command(
self.main_window.quit()
else:
valid_commands = ["quit", "pause", "continue"]
self.set_unfocused_state(edit="Error: Invalid command. Choose from: " + ", ".join(valid_commands))
self.set_unfocused_state("Error: Invalid command. Choose from: " +
", ".join(valid_commands))

def keypress(self, size, key):
if key == "enter":
Expand Down
7 changes: 5 additions & 2 deletions scapy/tools/packet_viewer/details_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
class DetailsView(LineBox):
def __init__(self, close_details_func):
self.visible = False
close_btn = AttrMap(Button("Close details (press this button or click c)", close_details_func), "green")
close_btn = Button("Close details (press this button or click c)",
close_details_func)
close_btn = AttrMap(close_btn, "green")
self.close_btn_widget = (close_btn, ("pack", None))
self.detail_text = Text("")
self.hex_text = Text("", align="right")
col = Columns([("pack", self.detail_text), self.hex_text], dividechars=4)
col = Columns([("pack", self.detail_text), self.hex_text],
dividechars=4)
super(DetailsView, self).__init__(Filler(col, "top"))

def update(
Expand Down
21 changes: 14 additions & 7 deletions scapy/tools/packet_viewer/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from scapy.packet import Packet, Raw
from scapy.sendrecv import AsyncSniffer
from scapy.supersocket import SuperSocket
from scapy.tools.packet_viewer.columns_manager import ColumnsManager, PacketListColumn
from scapy.tools.packet_viewer.command_line_interface import CommandLineInterface
from scapy.tools.packet_viewer.columns_manager import ColumnsManager, \
PacketListColumn
from scapy.tools.packet_viewer.command_line_interface import \
CommandLineInterface
from scapy.tools.packet_viewer.details_view import DetailsView
from scapy.tools.packet_viewer.packet_list_view import PacketListView
from scapy.tools.packet_viewer.pop_ups import show_exit_pop_up
Expand Down Expand Up @@ -38,7 +40,8 @@ def __init__(self, socket, # type: SuperSocket
super(MainWindow, self).__init__(
body=Pile([self.packet_view,
("pack", AttrMap(Text("Active"), "green"))]),
header=AttrMap(Text(" " + cm.get_header_string()), "packet_view_header"),
header=AttrMap(Text(" " + cm.get_header_string()),
"packet_view_header"),
footer=CommandLineInterface(self)
)

Expand All @@ -55,16 +58,19 @@ def pause_packet_sniffer(self):
return

self.sniffer.stop(False)
self.body.contents[STATUS_INDEX] = (AttrMap(Text("Paused"), "red"), ("pack", None))
self.body.contents[STATUS_INDEX] = (AttrMap(Text("Paused"), "red"),
("pack", None))
self.sniffer_is_running = False

def continue_packet_sniffer(self):
if not self.sniffer_is_running:
self.sniffer.start()
self.body.contents[STATUS_INDEX] = (AttrMap(Text("Active"), "green"), ("pack", None))
text = AttrMap(Text("Active"), "green")
self.body.contents[STATUS_INDEX] = (text, ("pack", None))
self.sniffer_is_running = True
else:
self.footer.set_unfocused_state(edit="Error: Cannot continue sniffer since it is not paused.")
msg = "Error: Cannot continue sniffer since it is not paused."
self.footer.set_unfocused_state(edit=msg)

def quit(self):
show_exit_pop_up(self)
Expand All @@ -77,7 +83,8 @@ def show_details(
dv_with_options = (self.details_view, ("weight", 0.3))
if self.details_view.visible:
self.body.contents[DETAIL_VIEW_INDEX] = dv_with_options
self.body.contents[DETAIL_CLOSE_BUTTON_INDEX] = self.details_view.close_btn_widget
self.body.contents[DETAIL_CLOSE_BUTTON_INDEX] = \
self.details_view.close_btn_widget
else:
self.body.contents.append(dv_with_options)
self.body.contents.append(self.details_view.close_btn_widget)
Expand Down
31 changes: 16 additions & 15 deletions scapy/tools/packet_viewer/packet_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class PacketListView(ListBox):
"""
Lists all the packets which have been sniffed so far. Is part of the packet_view.
Lists all the packets which have been sniffed so far.
"""

def __init__(self, main_window, cm):
Expand All @@ -21,7 +21,8 @@ def __init__(self, main_window, cm):
self.main_window = main_window
self.cm = cm
body = SimpleFocusListWalker([])
# registers `self.on_focus_change` as a callback method, whenever the list is modified
# registers `self.on_focus_change` as a callback method,
# whenever the list is modified
connect_signal(body, "modified", self.on_focus_change)
super(PacketListView, self).__init__(body)

Expand All @@ -31,8 +32,10 @@ def add_packet(
# type: (...) -> None
"""
Creates and appends a Packet widget to the end of the list.
The cursor in front of the packet content is colored in the default background color.
This way, it is invisible and only the cursor in front of the packet in focus is colored.
The cursor in front of the packet content is colored
in the default background color.
This way, it is invisible and only the cursor
in front of the packet in focus is colored.
:param packet: packet, which is passed on from the sniffer
:type packet: Packet
Expand All @@ -42,15 +45,18 @@ def add_packet(
text = self.cm.format(packet)
gui_packet = SelectableText(packet, [("cursor", u">> "), text])

self.body.append(AttrMap(gui_packet, {"cursor": "cursor_unfocused"}, {"cursor": "cursor_focused"}))
self.body.append(AttrMap(gui_packet,
{"cursor": "cursor_unfocused"},
{"cursor": "cursor_focused"}))
if self.main_window.main_loop:
self.main_window.main_loop.draw_screen()

def open_packet_details(
self, is_update=False # type: bool
):
"""
Gets the packet, currently in focus and creates or updates an existing view with the packets details.
Gets the packet currently in focus and
creates or updates a view with the packet details.
"""
if self.focus is None:
return
Expand Down Expand Up @@ -81,7 +87,8 @@ def keypress(self, size, key):
def mouse_event(self, size, event, button, col, row, focus):
"""
Handles mouse events.
Unhandled mouse events are being passed on to the keypress method of the super class.
Unhandled mouse events are being passed on
to the keypress method of the super class.
"""

# Translate mouse scrolling to up and down keys
Expand All @@ -94,14 +101,8 @@ def mouse_event(self, size, event, button, col, row, focus):
return

self.main_window.footer.set_unfocused_state()
super(PacketListView, self).mouse_event(size, event, button, col, row, focus)
super(PacketListView, self).mouse_event(size, event, button,
col, row, focus)

def on_focus_change(self):
"""
Whenever the focus changes from one packet widget to another,
the cursor of the packet widget previously in focus is colored in the default background color
and the cursor in front of the packet now in focus is colored in green, so it is visible.
:return: None
"""

self.open_packet_details(is_update=True)
12 changes: 8 additions & 4 deletions scapy/tools/packet_viewer/pop_ups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from urwid import Text, AttrMap, Button, LineBox, Overlay, ListBox, SimpleFocusListWalker, ExitMainLoop
from urwid import Text, AttrMap, Button, LineBox, Overlay, ListBox, \
SimpleFocusListWalker, ExitMainLoop

from scapy.error import Scapy_Exception

Expand All @@ -13,7 +14,8 @@ def delete_overlay(_self):
ok_btn = AttrMap(Button("OK", delete_overlay), "green")

prompt = LineBox(ListBox(SimpleFocusListWalker([info, ok_btn])))
overlay = Overlay(prompt, loop.widget.base_widget, "center", 30, "middle", 8, 16, 8)
overlay = Overlay(prompt, loop.widget.base_widget, "center",
30, "middle", 8, 16, 8)
loop.widget = overlay
loop.draw_screen()

Expand All @@ -37,6 +39,8 @@ def exit_loop(_self):
question = Text(("default_bold", "Really quit?"), "center")
yes_btn = AttrMap(Button("Yes", exit_loop), "red")
no_btn = AttrMap(Button("No", delete_overlay), "green")
prompt = LineBox(ListBox(SimpleFocusListWalker([question, no_btn, yes_btn])))
overlay = Overlay(prompt, main_window.main_loop.widget.base_widget, "center", 20, "middle", 8, 16, 8)
listbox = ListBox(SimpleFocusListWalker([question, no_btn, yes_btn]))
linebox = LineBox(listbox)
overlay = Overlay(linebox, main_window.main_loop.widget.base_widget,
"center", 20, "middle", 8, 16, 8)
main_window.main_loop.widget = overlay
16 changes: 9 additions & 7 deletions scapy/tools/packet_viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


def viewer(
socket, # type: SuperSocket
columns=None, # type: Optional[List[PacketListColumn]]
basecls=None, # type: Packet_metaclass
**kwargs
socket, # type: SuperSocket
columns=None, # type: Optional[List[PacketListColumn]]
basecls=None, # type: Packet_metaclass
**kwargs
):
palette = [
("default", "light gray", "black"),
Expand All @@ -38,13 +38,15 @@ def viewer(

def get_isotp_preset():
# type: (...) -> Dict[str, Union[List[PacketListColumn], Callable]]
return {"columns": [PacketListColumn("SRC", 6, lambda p: format(p.src, "03X")),
PacketListColumn("DST", 6, lambda p: format(p.dst, "03X")), ]}
return {"columns":
[PacketListColumn("SRC", 6, lambda p: format(p.src, "03X")),
PacketListColumn("DST", 6, lambda p: format(p.dst, "03X")), ]}


# TODO: This show Identifier(integer?) and ID(hex)
def get_can_preset():
# type: (...) -> Dict[str, Union[List[PacketListColumn], Callable]]
return {
"columns": [PacketListColumn("ID", 8, lambda p: format(p.identifier, "03X"))],
"columns": [
PacketListColumn("ID", 8, lambda p: format(p.identifier, "03X"))],
}

0 comments on commit a3bd9db

Please sign in to comment.