Skip to content

Commit bcf8748

Browse files
committed
refactoring and migrating to python-3.5
1 parent ef31cd8 commit bcf8748

19 files changed

+300
-14
lines changed

Application.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from os.path import expanduser, sep
2+
3+
from ssh_manager.ConfigurationFile import ConfigurationFile
4+
from ssh_manager.JsonConfigParser import JsonConfigParser
5+
from ssh_manager.Manager import Manager
6+
from ssh_manager.config.Config import Config
7+
from ssh_manager.interfaces.AbstractApplication import AbstractApplication
8+
from ui.ConnectionListWindow import ConnectionListWindow
9+
10+
11+
class Application(AbstractApplication):
12+
def run(self):
13+
config_file_path = self.get_config_file_path()
14+
config = self.get_config(config_file_path)
15+
manager = self.get_manager(config)
16+
17+
window = ConnectionListWindow(manager, config.get_window())
18+
window.show()
19+
20+
@staticmethod
21+
def get_config_file_path() -> str:
22+
home = expanduser("~")
23+
return home + sep + '_______sshManager.config.json'
24+
25+
@staticmethod
26+
def get_manager(config: Config):
27+
connections = config.get_connections()
28+
manager = Manager(connections)
29+
30+
return manager
31+
32+
@staticmethod
33+
def get_config(file_path: str) -> Config:
34+
file = ConfigurationFile(file_path)
35+
parser = JsonConfigParser()
36+
config = parser.parse(file)
37+
38+
return config
39+
40+
@staticmethod
41+
def create_default_manager():
42+
config = Application.get_config(Application.get_config_file_path())
43+
manager = Application.get_manager(config)
44+
45+
return manager

app.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
#!/usr/bin/python
2-
# simple.py
1+
#!/usr/bin/python3.5
32

4-
import sys
5-
from PyQt4 import QtGui
3+
from Application import Application
64

7-
from SshManager import SshManager
8-
9-
app = QtGui.QApplication(sys.argv)
10-
11-
sshManager = SshManager()
12-
sshManager.show()
13-
14-
sys.exit(app.exec_())
5+
app = Application()
6+
app.run()

requirements.txt

Whitespace-only changes.

config-dist.json resources/config-dist.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"-x",
66
"ssh"
77
],
8-
"edit": ["xdg-open"]
8+
"edit": [
9+
"xdg-open"
10+
]
911
},
1012
"window": {
1113
"x": 300,
@@ -23,7 +25,8 @@
2325
"host": "localhost",
2426
"label": "Localhost with default port",
2527
"args": [
26-
"-l", "my_username"
28+
"-l",
29+
"my_username"
2730
]
2831
}
2932
]

screenshot.png

-5.82 KB
Loading

ssh_manager/ConfigurationFile.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from os.path import exists, dirname
2+
from shutil import copy
3+
4+
from ssh_manager.interfaces.AbstractConfigurationFile import AbstractConfigurationFile
5+
6+
7+
class ConfigurationFile(AbstractConfigurationFile):
8+
_file_path = ''
9+
10+
def __init__(self, file_path):
11+
super(ConfigurationFile, self).__init__()
12+
self._file_path = file_path
13+
self._create_if_not_exists()
14+
15+
def get_contents(self):
16+
with open(self._file_path, 'r') as opened_file:
17+
return opened_file.read()
18+
19+
def _create_if_not_exists(self):
20+
if exists(self._file_path):
21+
return
22+
dist_file = dirname(__file__) + '/../resources/config-dist.json'
23+
copy(dist_file, self._file_path)

ssh_manager/Connection.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Connection:
2+
DEFAULT_PORT = 22
3+
label = ''
4+
host = ''
5+
port = 0
6+
7+
def __init__(self, label: str, host: str, port: int):
8+
self.label = label
9+
self.host = host
10+
self.port = port

ssh_manager/JsonConfigParser.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from json import loads
2+
3+
from ssh_manager import ConfigurationFile
4+
from ssh_manager.Connection import Connection
5+
from ssh_manager.config.Config import Config
6+
from ssh_manager.config.Window import Window
7+
from ssh_manager.interfaces.AbstractConfigParser import AbstractConfigParser
8+
9+
10+
class JsonConfigParser(AbstractConfigParser):
11+
def parse(self, file: ConfigurationFile) -> Config:
12+
config = Config()
13+
json_object = loads(file.get_contents())
14+
15+
self._parse_connections(json_object, config)
16+
self._parse_window(json_object, config)
17+
18+
return config
19+
20+
@staticmethod
21+
def _parse_connections(json_object: dict, config: Config):
22+
config.set_connections([])
23+
for _connection in json_object['connections']:
24+
if 'port' not in _connection:
25+
_connection['port'] = Connection.DEFAULT_PORT
26+
connection = Connection(_connection['label'], _connection['host'], _connection['port'])
27+
config.add_connection(connection)
28+
29+
@staticmethod
30+
def _parse_window(json_object: dict, config: Config):
31+
_window = json_object['window']
32+
window = Window(_window['x'], _window['y'], _window['width'], _window['height'])
33+
config.set_window(window)

ssh_manager/Manager.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from ssh_manager.Connection import Connection
2+
3+
4+
class Manager:
5+
_connections = []
6+
7+
def __init__(self, connections: [Connection]):
8+
self._connections = connections
9+
10+
def get_connection(self, index: int) -> Connection:
11+
return self._connections[index]
12+
13+
def get_connections(self):
14+
return self._connections

ssh_manager/__init__.py

Whitespace-only changes.

ssh_manager/config/Config.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from ssh_manager.Connection import Connection
2+
from ssh_manager.config.Window import Window
3+
4+
5+
class Config:
6+
_connections = []
7+
_window = None
8+
9+
def add_connection(self, connection: Connection):
10+
self._connections.append(connection)
11+
12+
def set_window(self, window: Window):
13+
self._window = window
14+
15+
def get_connections(self):
16+
return self._connections
17+
18+
def get_window(self) -> Window:
19+
return self._window
20+
21+
def set_connections(self, connections):
22+
self._connections = connections

ssh_manager/config/Window.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Window:
2+
x = 0
3+
y = 0
4+
width = 0
5+
height = 0
6+
7+
def __init__(self, x: int, y: int, width: int, height: int):
8+
self.x = x
9+
self.y = y
10+
self.width = width
11+
self.height = height
12+
pass

ssh_manager/config/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
4+
class AbstractApplication:
5+
__metaclass__ = ABCMeta
6+
7+
@abstractmethod
8+
def run(self):
9+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import abstractmethod, ABCMeta
2+
3+
from ssh_manager import ConfigurationFile
4+
5+
6+
class AbstractConfigParser:
7+
__metaclass__ = ABCMeta
8+
9+
@abstractmethod
10+
def parse(self, file: ConfigurationFile):
11+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
4+
class AbstractConfigurationFile:
5+
__metaclass__ = ABCMeta
6+
7+
def __init__(self):
8+
pass
9+
10+
@abstractmethod
11+
def get_contents(self):
12+
pass

ssh_manager/interfaces/__init__.py

Whitespace-only changes.

ui/ConnectionListWindow.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from os import system
2+
3+
from gi import require_version
4+
5+
require_version('Gdk', '3.0')
6+
require_version('Gtk', '3.0')
7+
8+
from gi.repository import Gtk
9+
10+
import Application
11+
from ssh_manager.Manager import Manager
12+
from ssh_manager.config.Window import Window
13+
14+
15+
class ConnectionListWindow:
16+
VERTICAL_MARGIN = 5
17+
HORIZONTAL_MARGIN = 2
18+
_listbox = None
19+
_frame = None
20+
_scrollable = None
21+
_list_store = Gtk.ListStore(str)
22+
23+
def __init__(self, manager: Manager, window_config: Window):
24+
self._manager = manager
25+
self._window_config = window_config
26+
27+
def show(self):
28+
win = Gtk.Window()
29+
win.set_size_request(self._window_config.width, self._window_config.height)
30+
win.move(self._window_config.x, self._window_config.y)
31+
win.set_resizable(False)
32+
33+
btn_setting = Gtk.Button("Settings")
34+
btn_setting.set_margin_right(self.HORIZONTAL_MARGIN)
35+
btn_reload = Gtk.Button("Reload")
36+
37+
btn_box = Gtk.HBox()
38+
btn_box.add(btn_setting)
39+
btn_box.add(btn_reload)
40+
41+
self._listbox = Gtk.TreeView(self._list_store)
42+
self._listbox.set_headers_visible(False)
43+
self._listbox.connect("row-activated", self.row_activated)
44+
45+
self._scrollable = Gtk.ScrolledWindow()
46+
self._scrollable.add(self._listbox)
47+
self._fill_listbox()
48+
49+
self._frame = Gtk.Frame()
50+
self._frame.add(self._scrollable)
51+
self._frame.set_border_width(1)
52+
self._frame.set_margin_bottom(self.VERTICAL_MARGIN)
53+
54+
vbox = Gtk.VBox()
55+
vbox.pack_start(self._frame, 1, 1, 1)
56+
vbox.pack_end(btn_box, 0, 0, 0)
57+
58+
vbox.set_margin_top(self.VERTICAL_MARGIN)
59+
vbox.set_margin_bottom(self.VERTICAL_MARGIN)
60+
vbox.set_margin_left(self.VERTICAL_MARGIN)
61+
vbox.set_margin_right(self.VERTICAL_MARGIN)
62+
63+
btn_setting.connect("clicked", self.btn_settings_click)
64+
btn_reload.connect("clicked", self.btn_reload_click)
65+
66+
win.add(vbox)
67+
68+
win.set_title("SshManager GTK")
69+
win.connect("delete-event", Gtk.main_quit)
70+
win.show_all()
71+
Gtk.main()
72+
73+
# noinspection PyUnusedLocal
74+
@staticmethod
75+
def btn_settings_click(target):
76+
system("xdg-open " + Application.Application.get_config_file_path())
77+
78+
# noinspection PyUnusedLocal
79+
def btn_reload_click(self, target):
80+
self._manager = Application.Application.create_default_manager()
81+
self._fill_listbox()
82+
83+
def _fill_listbox(self):
84+
self._list_store.clear()
85+
connections = self._manager.get_connections()
86+
for connection in connections:
87+
self._list_store.append([connection.label])
88+
89+
renderer = Gtk.CellRendererText()
90+
column = self._listbox.get_column(0)
91+
if not column:
92+
column = Gtk.TreeViewColumn(None, renderer, text=0)
93+
self._listbox.append_column(column)
94+
self._listbox.show_all()
95+
96+
# noinspection PyUnusedLocal
97+
def row_activated(self, target: Gtk.TreeView, path: Gtk.TreePath, column: Gtk.TreeViewColumn):
98+
i = path.get_indices()[0]
99+
connection = self._manager.get_connection(i)
100+
system("gnome-terminal -x ssh {} -p {}".format(connection.host, connection.port))

ui/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)