Skip to content

Commit 2202a2f

Browse files
committed
move commands to separate classes, update README, fix config file path, update default config file
1 parent bcf8748 commit 2202a2f

14 files changed

+131
-18
lines changed

Application.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def run(self):
1414
config = self.get_config(config_file_path)
1515
manager = self.get_manager(config)
1616

17-
window = ConnectionListWindow(manager, config.get_window())
17+
window = ConnectionListWindow(manager, config.get_window(), config.get_ssh_command(), config.get_edit_command())
1818
window.show()
1919

2020
@staticmethod
2121
def get_config_file_path() -> str:
2222
home = expanduser("~")
23-
return home + sep + '_______sshManager.config.json'
23+
return home + sep + '.sshManager.config.json'
2424

2525
@staticmethod
2626
def get_manager(config: Config):

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Ubuntu Desktop (GUI) ssh manager
2-
GUI ssh manager for Ubuntu 16.04 (not tested on below versions)
2+
GUI ssh manager for Linux (Ubuntu)
33

44
![alt tag](https://raw.githubusercontent.com/Doka-NT/ssh-manager/master/screenshot.png)
55
# Requirements
6-
- PyQt4
6+
- python 3.5+
77

88
# Run
99
```bash
10-
python app.py
10+
python3.5 app.py
1111
```
1212

1313
# Config
14-
config.json file will be created after first run.
15-
Edit it as you need.
14+
Application can be configured by json configuration file.
15+
All connections defines in section connections, like provided examples.

resources/config-dist.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
"ssh": [
44
"gnome-terminal",
55
"-x",
6-
"ssh"
6+
"ssh",
7+
"$host",
8+
"-p",
9+
"$port"
710
],
811
"edit": [
9-
"xdg-open"
12+
"xdg-open",
13+
"$file"
1014
]
1115
},
1216
"window": {

ssh_manager/CommandFactory.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
2+
from ssh_manager.config.Commands.EditCommand import EditCommand
3+
from ssh_manager.config.Commands.SshCommand import SshCommand
4+
5+
6+
class CommandFactory:
7+
def create_command(self, name: str, args: list) -> AbstractCommand:
8+
command = None
9+
if name == 'ssh':
10+
command = self.create_ssh_command(args)
11+
elif name == 'edit':
12+
command = self.create_edit_command(args)
13+
return command
14+
15+
@staticmethod
16+
def create_ssh_command(args: list) -> AbstractCommand:
17+
return SshCommand(args)
18+
19+
@staticmethod
20+
def create_edit_command(args: list) -> AbstractCommand:
21+
return EditCommand(args)

ssh_manager/Connection.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ class Connection:
33
label = ''
44
host = ''
55
port = 0
6+
args = []
67

7-
def __init__(self, label: str, host: str, port: int):
8+
def __init__(self, label: str, host: str, port: int, args: list):
89
self.label = label
910
self.host = host
1011
self.port = port
12+
self.args = args

ssh_manager/JsonConfigParser.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def parse(self, file: ConfigurationFile) -> Config:
1414

1515
self._parse_connections(json_object, config)
1616
self._parse_window(json_object, config)
17+
self._parse_commands(json_object, config)
1718

1819
return config
1920

@@ -23,11 +24,23 @@ def _parse_connections(json_object: dict, config: Config):
2324
for _connection in json_object['connections']:
2425
if 'port' not in _connection:
2526
_connection['port'] = Connection.DEFAULT_PORT
26-
connection = Connection(_connection['label'], _connection['host'], _connection['port'])
27+
if 'args' not in _connection:
28+
_connection['args'] = []
29+
connection = Connection(
30+
_connection['label'],
31+
_connection['host'],
32+
_connection['port'],
33+
_connection['args']
34+
)
2735
config.add_connection(connection)
2836

2937
@staticmethod
3038
def _parse_window(json_object: dict, config: Config):
3139
_window = json_object['window']
3240
window = Window(_window['x'], _window['y'], _window['width'], _window['height'])
3341
config.set_window(window)
42+
43+
def _parse_commands(self, json_object: dict, config: Config):
44+
for name, args in json_object['command'].items():
45+
command = self._command_factory.create_command(name, args)
46+
config.add_command(name, command)

ssh_manager/config/Command.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
2+
3+
4+
class Command:
5+
_commands = {}
6+
7+
def add_command(self, name: str, handler: AbstractCommand):
8+
self._commands[name] = handler
9+
10+
def get_command(self, name: str) -> AbstractCommand:
11+
return self._commands[name]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AbstractCommand:
2+
_args = []
3+
4+
def __init__(self, args: list):
5+
self._args = args
6+
7+
def run(self, *args):
8+
pass
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from os import system
2+
3+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
4+
5+
6+
class EditCommand(AbstractCommand):
7+
def run(self, *args):
8+
super().run(*args)
9+
cmd = self._args
10+
file = args[0]
11+
command = " ".join(cmd).replace("$file", file)
12+
system(command)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from os import system
2+
3+
from ssh_manager.Connection import Connection
4+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
5+
6+
7+
class SshCommand(AbstractCommand):
8+
def run(self, *args):
9+
super().run(*args)
10+
connection = args[0] # type: Connection
11+
command_args = args[1] # type: list
12+
cmd = self._args + command_args
13+
command = " ".join(cmd).replace("$host", connection.host).replace("$port", str(connection.port))
14+
system(command)

ssh_manager/config/Commands/__init__.py

Whitespace-only changes.

ssh_manager/config/Config.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from ssh_manager.Connection import Connection
2+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
3+
from ssh_manager.config.Commands.EditCommand import EditCommand
4+
from ssh_manager.config.Commands.SshCommand import SshCommand
25
from ssh_manager.config.Window import Window
36

47

58
class Config:
69
_connections = []
710
_window = None
11+
_commands = {}
812

913
def add_connection(self, connection: Connection):
1014
self._connections.append(connection)
@@ -20,3 +24,12 @@ def get_window(self) -> Window:
2024

2125
def set_connections(self, connections):
2226
self._connections = connections
27+
28+
def add_command(self, name: str, command: AbstractCommand):
29+
self._commands[name] = command
30+
31+
def get_ssh_command(self) -> SshCommand:
32+
return self._commands['ssh']
33+
34+
def get_edit_command(self) -> EditCommand:
35+
return self._commands['edit']

ssh_manager/interfaces/AbstractConfigParser.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from abc import abstractmethod, ABCMeta
22

33
from ssh_manager import ConfigurationFile
4+
from ssh_manager.CommandFactory import CommandFactory
45

56

67
class AbstractConfigParser:
78
__metaclass__ = ABCMeta
9+
_command_factory = None
10+
11+
def __init__(self):
12+
self._command_factory = CommandFactory()
813

914
@abstractmethod
1015
def parse(self, file: ConfigurationFile):

ui/ConnectionListWindow.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from os import system
2-
31
from gi import require_version
42

3+
from ssh_manager.config.Commands.AbtractCommand import AbstractCommand
4+
55
require_version('Gdk', '3.0')
66
require_version('Gtk', '3.0')
77

@@ -15,12 +15,23 @@
1515
class ConnectionListWindow:
1616
VERTICAL_MARGIN = 5
1717
HORIZONTAL_MARGIN = 2
18+
19+
__command_ssh = None
20+
__command_edit = None
21+
1822
_listbox = None
1923
_frame = None
2024
_scrollable = None
2125
_list_store = Gtk.ListStore(str)
2226

23-
def __init__(self, manager: Manager, window_config: Window):
27+
def __init__(
28+
self, manager: Manager,
29+
window_config: Window,
30+
command_ssh: AbstractCommand,
31+
command_edit: AbstractCommand
32+
):
33+
self.__command_ssh = command_ssh
34+
self.__command_edit = command_edit
2435
self._manager = manager
2536
self._window_config = window_config
2637

@@ -71,9 +82,8 @@ def show(self):
7182
Gtk.main()
7283

7384
# noinspection PyUnusedLocal
74-
@staticmethod
75-
def btn_settings_click(target):
76-
system("xdg-open " + Application.Application.get_config_file_path())
85+
def btn_settings_click(self, target):
86+
self.__command_edit.run(Application.Application.get_config_file_path())
7787

7888
# noinspection PyUnusedLocal
7989
def btn_reload_click(self, target):
@@ -97,4 +107,4 @@ def _fill_listbox(self):
97107
def row_activated(self, target: Gtk.TreeView, path: Gtk.TreePath, column: Gtk.TreeViewColumn):
98108
i = path.get_indices()[0]
99109
connection = self._manager.get_connection(i)
100-
system("gnome-terminal -x ssh {} -p {}".format(connection.host, connection.port))
110+
self.__command_ssh.run(connection, connection.args)

0 commit comments

Comments
 (0)