-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.py
91 lines (71 loc) · 2.88 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import argparse
from abc import ABCMeta, abstractmethod
from os.path import expanduser, join
from gui.window import ConnectionListWindow
from manager.config import Config, ConfigurationFile
from manager.manager import Manager
from manager.parser import JsonConfigParser
class AbstractApplication:
__metaclass__ = ABCMeta
def __init__(self):
self._config_file_path = self.get_config_file_path()
self._config = self.get_config(self._config_file_path)
self._manager = self.get_manager(self._config)
@abstractmethod
def run(self):
raise NotImplementedError("Cannot run application because `run` method is not implemented")
@staticmethod
def get_config_file_path() -> str:
return join(expanduser("~"), '.sshManager.config.json')
@staticmethod
def get_manager(config: Config):
connections = config.get_connections()
manager = Manager(connections)
return manager
@staticmethod
def get_config(file_path: str) -> Config:
file = ConfigurationFile(file_path)
parser = JsonConfigParser()
config = parser.parse(file)
return config
@staticmethod
def create_default_manager():
config = AbstractApplication.get_config(AbstractApplication.get_config_file_path())
manager = AbstractApplication.get_manager(config)
return manager
class GtkApplication(AbstractApplication):
def run(self):
window = ConnectionListWindow(
self._manager,
self._config.get_window(),
self._config.get_ssh_command(),
self._config.get_edit_command()
)
window.show()
class ConsoleApplication(AbstractApplication):
def run(self):
parser = argparse.ArgumentParser(description='Helps you to store and use SSH connections list')
parser.add_argument(
'--connection',
'-c',
help="Connect to specific server connection by it number. "
"Run program without arguments to list all available connections"
)
parser.add_argument('--edit', '-e', help="Edit configuration", action="store_true")
args = parser.parse_args()
if args.connection:
connection = self._manager.get_connection(int(args.connection))
self._connect_to(connection)
if args.edit:
self._edit_configuration()
else:
self._list_connections()
def _connect_to(self, connection):
self._config.get_ssh_command().run(connection, connection.args)
def _list_connections(self):
connections = self._manager.get_connections()
print("Available connections:")
for connection in connections:
print("[{}]\t{}".format(connections.index(connection), connection.label))
def _edit_configuration(self):
self._config.get_edit_command().run(self._config_file_path)