-
Notifications
You must be signed in to change notification settings - Fork 1
/
sftp.py
executable file
·144 lines (123 loc) · 4.07 KB
/
sftp.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/python3
import sys
from getpass import getpass
from src.sftp_client import SFTPClient
from src.LoginInfoManager import LoginInfoManager
from simple_term_menu import TerminalMenu
LOGIN: str = 'Login'
PICK_SAVED: str = 'Pick from saved account'
DEL_SAVED: str = 'Delete a saved account'
EXIT: str = 'Exit'
QUIT: str = 'Quit'
RETURN: str = 'Return'
def login() -> SFTPClient:
"""Collects user input for host, username, and password and creates an SFTPClient
:return: SFTPClient object with the entered data
:rtype: SFTPClient
"""
user: str = input("Username: ")
host: str = input("host: ")
pwd = getpass("password: ")
response = input("Would you like to save the login information? [y/n]: ")
if len(response) < 1:
response = 'n'
if response[0] == 'Y' or response == 'y':
manager = LoginInfoManager()
result = manager.addLoginInfo(user_name=user, host=host, password=pwd)
if result == True:
print("your login information have been saved!")
else:
print("This login already exist.")
pass
return SFTPClient(host_name=host, user_name=user, password=pwd)
def pickFromSaved() -> dict:
"""Allows you to pick from saved connections/account
:return: picked connection/account
:rtype: dict
"""
manager = LoginInfoManager()
all_login = manager.getAllLoginInfo()
options = list()
for i in all_login:
options.append(str(i))
options.append(RETURN) # allows you to exit
terminal_menu = TerminalMenu(
menu_entries=options,
title="Saved login Info"
)
menu_entry_index = terminal_menu.show()
if options[menu_entry_index] == RETURN:
return None
else:
choosen_login: dict = all_login[menu_entry_index]
client: SFTPClient = SFTPClient(
host_name=choosen_login.get('host'),
user_name=choosen_login.get('username'),
password=choosen_login.get('password')
)
return client
def deleteFromSaved() -> None:
"""Allows you to delete from saved connections/account
"""
manager = LoginInfoManager()
all_login = manager.getAllLoginInfo()
options = list()
for i in all_login:
options.append(str(i))
options.append(RETURN) # allows you to return
terminal_menu = TerminalMenu(
menu_entries=options,
title="Saved login Info"
)
menu_entry_index = terminal_menu.show()
if options[menu_entry_index] == RETURN:
return None
else:
choosen_login: dict = all_login[menu_entry_index]
result = manager.deleteLoginInfo(choosen_login)
if result == True:
print("Selected account has been deleted!")
else:
print("Selected account has not been deleted!")
return None
def menu() -> None:
"""menu interface login
"""
options = [LOGIN,
PICK_SAVED,
DEL_SAVED,
EXIT]
terminal_menu: TerminalMenu = TerminalMenu( #Menu Window
menu_entries=options,
title="MAIN MENU"
)
exit_flag: bool = False
while not exit_flag:
menu_entry: str = options[terminal_menu.show()]
if menu_entry == LOGIN:
client: SFTPClient = login()
result = client.connect()
if result == False:
print("LOGIN FAILED!, please try again.")
else:
client.mainMenu()
client.close()
elif menu_entry == PICK_SAVED:
client: SFTPClient = pickFromSaved()
if client != None:
result = client.connect()
if result == False:
print("LOGIN FAILED!, please try again.")
else:
client.mainMenu()
client.close()
pass
elif menu_entry == DEL_SAVED:
deleteFromSaved()
elif menu_entry == EXIT:
exit_flag = True
pass
pass
pass
if __name__=='__main__':
menu()