Skip to content

Commit 1249a4f

Browse files
committed
Codereview fixes
1 parent 0dc23cc commit 1249a4f

File tree

4 files changed

+48
-52
lines changed

4 files changed

+48
-52
lines changed

application.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
from abc import ABCMeta, abstractmethod
3-
from os.path import expanduser, sep
3+
from os.path import expanduser, join
44

55
from gui.window import ConnectionListWindow
66
from manager.config import Config, ConfigurationFile
@@ -18,12 +18,11 @@ def __init__(self):
1818

1919
@abstractmethod
2020
def run(self):
21-
pass
21+
raise NotImplementedError("Cannot run application because `run` method is not implemented")
2222

2323
@staticmethod
2424
def get_config_file_path() -> str:
25-
home = expanduser("~")
26-
return home + sep + '.sshManager.config.json'
25+
return join(expanduser("~"), '.sshManager.config.json')
2726

2827
@staticmethod
2928
def get_manager(config: Config):

gui/window.py

+35-32
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,84 @@ class ConnectionListWindow:
1616
VERTICAL_MARGIN = 5
1717
HORIZONTAL_MARGIN = 2
1818

19-
__command_ssh = None
20-
__command_edit = None
21-
22-
_listbox = None
23-
_frame = None
24-
_scrollable = None
25-
_list_store = Gtk.ListStore(str)
26-
2719
def __init__(
2820
self, manager: Manager,
2921
window_config: Window,
3022
command_ssh: AbstractCommand,
3123
command_edit: AbstractCommand
3224
):
25+
self._listbox = None
26+
self._frame = None
27+
self._scrollable = None
28+
self._btn_reload = None
29+
self._btn_settings = None
30+
self._list_store = Gtk.ListStore(str)
31+
3332
self.__command_ssh = command_ssh
3433
self.__command_edit = command_edit
3534
self._manager = manager
3635
self._window_config = window_config
3736

3837
def show(self):
39-
win = Gtk.Window()
40-
win.set_size_request(self._window_config.width, self._window_config.height)
41-
win.move(self._window_config.x, self._window_config.y)
42-
win.set_resizable(False)
38+
win = self.__create_window()
39+
40+
self.__setup_ui(win)
41+
self.__setup_ui_listeners(win)
4342

43+
win.show_all()
44+
Gtk.main()
45+
46+
def __setup_ui_listeners(self, win):
47+
self._btn_settings.connect("clicked", self.__btn_settings_click)
48+
self._btn_reload.connect("clicked", self.__btn_reload_click)
49+
win.connect("delete-event", Gtk.main_quit)
50+
51+
def __setup_ui(self, win):
4452
btn_setting = Gtk.Button("Settings")
4553
btn_setting.set_margin_right(self.HORIZONTAL_MARGIN)
4654
btn_reload = Gtk.Button("Reload")
47-
4855
btn_box = Gtk.HBox()
4956
btn_box.add(btn_setting)
5057
btn_box.add(btn_reload)
51-
5258
self._listbox = Gtk.TreeView(self._list_store)
5359
self._listbox.set_headers_visible(False)
54-
self._listbox.connect("row-activated", self.row_activated)
55-
60+
self._listbox.connect("row-activated", self.__row_activated)
5661
self._scrollable = Gtk.ScrolledWindow()
5762
self._scrollable.add(self._listbox)
58-
self._fill_listbox()
59-
63+
self.__fill_listbox()
6064
self._frame = Gtk.Frame()
6165
self._frame.add(self._scrollable)
6266
self._frame.set_border_width(1)
6367
self._frame.set_margin_bottom(self.VERTICAL_MARGIN)
64-
6568
vbox = Gtk.VBox()
6669
vbox.pack_start(self._frame, 1, 1, 1)
6770
vbox.pack_end(btn_box, 0, 0, 0)
68-
6971
vbox.set_margin_top(self.VERTICAL_MARGIN)
7072
vbox.set_margin_bottom(self.VERTICAL_MARGIN)
7173
vbox.set_margin_left(self.VERTICAL_MARGIN)
7274
vbox.set_margin_right(self.VERTICAL_MARGIN)
73-
74-
btn_setting.connect("clicked", self.btn_settings_click)
75-
btn_reload.connect("clicked", self.btn_reload_click)
76-
7775
win.add(vbox)
76+
return btn_reload, btn_setting
7877

78+
def __create_window(self):
79+
win = Gtk.Window()
80+
win.set_size_request(self._window_config.width, self._window_config.height)
81+
win.move(self._window_config.x, self._window_config.y)
82+
win.set_resizable(False)
7983
win.set_title("SshManager GTK")
80-
win.connect("delete-event", Gtk.main_quit)
81-
win.show_all()
82-
Gtk.main()
84+
85+
return win
8386

8487
# noinspection PyUnusedLocal
85-
def btn_settings_click(self, target):
88+
def __btn_settings_click(self, target):
8689
self.__command_edit.run(application.GtkApplication.get_config_file_path())
8790

8891
# noinspection PyUnusedLocal
89-
def btn_reload_click(self, target):
92+
def __btn_reload_click(self, target):
9093
self._manager = application.GtkApplication.create_default_manager()
91-
self._fill_listbox()
94+
self.__fill_listbox()
9295

93-
def _fill_listbox(self):
96+
def __fill_listbox(self):
9497
self._list_store.clear()
9598
connections = self._manager.get_connections()
9699
for connection in connections:
@@ -104,7 +107,7 @@ def _fill_listbox(self):
104107
self._listbox.show_all()
105108

106109
# noinspection PyUnusedLocal
107-
def row_activated(self, target: Gtk.TreeView, path: Gtk.TreePath, column: Gtk.TreeViewColumn):
110+
def __row_activated(self, target: Gtk.TreeView, path: Gtk.TreePath, column: Gtk.TreeViewColumn):
108111
i = path.get_indices()[0]
109112
connection = self._manager.get_connection(i)
110113
self.__command_ssh.run(connection, connection.args)

manager/command.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from abc import ABCMeta, abstractmethod
12
from os import system
23

34

45
class AbstractCommand:
6+
__metaclass__ = ABCMeta
7+
58
def __init__(self, args: list):
69
self._args = args
710

11+
@abstractmethod
812
def run(self, *args):
913
pass
1014

@@ -13,8 +17,8 @@ class EditCommand(AbstractCommand):
1317
def run(self, *args):
1418
super().run(*args)
1519
cmd = self._args
16-
file = args[0]
17-
command = " ".join(cmd).replace("$file", file)
20+
f = args[0]
21+
command = " ".join(cmd).replace("$file", f)
1822
system(command)
1923

2024

manager/config.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88

99
class Window:
10-
x = 0
11-
y = 0
12-
width = 0
13-
height = 0
14-
1510
def __init__(self, x: int, y: int, width: int, height: int):
1611
self.x = x
1712
self.y = y
@@ -32,10 +27,6 @@ def get_command(self, name: str) -> AbstractCommand:
3227

3328
class Connection:
3429
DEFAULT_PORT = 22
35-
label = ''
36-
host = ''
37-
port = 0
38-
args = []
3930

4031
def __init__(self, label: str, host: str, port: int, args: list):
4132
self.label = label
@@ -45,9 +36,10 @@ def __init__(self, label: str, host: str, port: int, args: list):
4536

4637

4738
class Config:
48-
_connections = []
49-
_window = None
50-
_commands = {}
39+
def __init__(self) -> None:
40+
self._connections = []
41+
self._window = None
42+
self._commands = {}
5143

5244
def add_connection(self, connection: Connection):
5345
self._connections.append(connection)
@@ -86,8 +78,6 @@ def get_contents(self):
8678

8779

8880
class ConfigurationFile(AbstractConfigurationFile):
89-
_file_path = ''
90-
9181
def __init__(self, file_path):
9282
super(ConfigurationFile, self).__init__()
9383
self._file_path = file_path

0 commit comments

Comments
 (0)