-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathcloud_api.py
More file actions
143 lines (123 loc) · 6.9 KB
/
Copy pathcloud_api.py
File metadata and controls
143 lines (123 loc) · 6.9 KB
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
from PySide6.QtWidgets import QFileDialog, QMessageBox
from PySide6.QtCore import QObject, Slot
import configparser
from sftp import fileTransfer
class CloudAPI(QObject):
def __init__(self):
super().__init__()
self.config = configparser.ConfigParser()
self.config.optionxform = str
self.root_object = None
def set_root_object(self, root_object):
self.root_object = root_object
def connect_buttons(self):
if self.root_object is None:
print("Error: root_object not set in CloudAPI")
return
try:
self.root_object.findChild(QObject, "saveConfigButton").clicked.connect(self.save_config)
self.root_object.findChild(QObject, "loadConfigButton").clicked.connect(self.load_config)
self.root_object.findChild(QObject, "clearConfigButton").clicked.connect(self.clear_config)
self.root_object.findChild(QObject, "uploadButton").clicked.connect(self.upload)
self.root_object.findChild(QObject, "privateKeyDirButton").clicked.connect(self.browse_private_key_dir)
self.root_object.findChild(QObject, "sourceDirButton").clicked.connect(self.browse_source_dir)
self.root_object.findChild(QObject, "targetDirButton").clicked.connect(self.browse_target_dir)
print("Cloud API buttons connected successfully")
except Exception as e:
print(f"Error connecting cloud API buttons: {e}")
# Start of change : Added Cloud Computing (Transfer Data) functionality
@Slot()
def browse_private_key_dir(self):
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.FileMode.Directory)
file_dialog.setViewMode(QFileDialog.ViewMode.List)
if file_dialog.exec():
file_paths = file_dialog.selectedFiles()
if file_paths:
self.root_object.findChild(QObject, "privateKeyDirInput").setProperty("text", file_paths[0])
@Slot()
def browse_source_dir(self):
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.FileMode.Directory)
file_dialog.setViewMode(QFileDialog.ViewMode.List)
if file_dialog.exec():
file_paths = file_dialog.selectedFiles()
if file_paths:
self.root_object.findChild(QObject, "sourceDirInput").setProperty("text", file_paths[0])
@Slot()
def browse_target_dir(self):
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.FileMode.Directory)
file_dialog.setViewMode(QFileDialog.ViewMode.List)
if file_dialog.exec():
file_paths = file_dialog.selectedFiles()
if file_paths:
self.root_object.findChild(QObject, "targetDirInput").setProperty("text", file_paths[0])
@Slot()
def save_config(self):
selected_file, _ = QFileDialog.getSaveFileName(
None,
"Save config file",
"",
"INI Files (*.ini)"
)
if selected_file:
if not selected_file.endswith(".ini"):
selected_file += ".ini"
with open(selected_file, 'w') as configfile:
self.config['data'] = {
"-HOST-": self.root_object.findChild(QObject, "hostInput").property("text"),
"-USERNAME-": self.root_object.findChild(QObject, "usernameInput").property("text"),
"-PRIVATE_KEY-": self.root_object.findChild(QObject, "privateKeyDirInput").property("text"),
"-IGNORE_HOST_KEY-": self.root_object.findChild(QObject, "ignoreHostKeyCheckbox").property("checked"),
"-SOURCE-": self.root_object.findChild(QObject, "sourceDirInput").property("text"),
"-TARGET-": self.root_object.findChild(QObject, "targetDirInput").property("text"),
}
self.config.write(configfile)
@Slot()
def load_config(self):
selected_file, _ = QFileDialog.getOpenFileName(
None,
"Load config file",
"",
"INI Files (*.ini)"
)
try:
if selected_file:
self.config.read(selected_file)
self.root_object.findChild(QObject, "hostInput").setProperty("text", self.config["data"]["-HOST-"])
self.root_object.findChild(QObject, "usernameInput").setProperty("text", self.config["data"]["-USERNAME-"])
self.root_object.findChild(QObject, "privateKeyDirInput").setProperty("text", self.config["data"]["-PRIVATE_KEY-"])
self.root_object.findChild(QObject, "ignoreHostKeyCheckbox").setProperty("checked", self.config["data"]["-IGNORE_HOST_KEY-"].lower() in ("true"))
self.root_object.findChild(QObject, "sourceDirInput").setProperty("text", self.config["data"]["-SOURCE-"])
self.root_object.findChild(QObject, "targetDirInput").setProperty("text", self.config["data"]["-TARGET-"])
except Exception as e:
QMessageBox.critical(None, "Loading failed", "Error: " + str(e))
@Slot()
def clear_config(self):
self.root_object.findChild(QObject, "hostInput").setProperty("text", "")
self.root_object.findChild(QObject, "usernameInput").setProperty("text", "")
self.root_object.findChild(QObject, "privateKeyDirInput").setProperty("text", "")
self.root_object.findChild(QObject, "ignoreHostKeyCheckbox").setProperty("checked", True) # Reset checkbox to checked
self.root_object.findChild(QObject, "sourceDirInput").setProperty("text", "")
self.root_object.findChild(QObject, "targetDirInput").setProperty("text", "/home/") # Reset to default
@Slot()
def upload(self):
try:
svrcon = fileTransfer(
self.root_object.findChild(QObject, "hostInput").property("text"),
self.root_object.findChild(QObject, "usernameInput").property("text"),
self.root_object.findChild(QObject, "privateKeyDirInput").property("text"),
self.root_object.findChild(QObject, "passwordInput").property("text"),
self.root_object.findChild(QObject, "ignoreHostKeyCheckbox").property("checked")
)
source_dir = self.root_object.findChild(QObject, "sourceDirInput").property("text")
target_dir = self.root_object.findChild(QObject, "targetDirInput").property("text")
if source_dir and target_dir:
svrcon.transfer(source_dir, target_dir)
QMessageBox.information(None, "Upload complete")
else:
QMessageBox.critical(None, "Upload failed", "Please ensure that all fields have been filled!")
except Exception as e:
QMessageBox.critical(None, "Upload failed", "Please ensure that your inputs are correct and that the server is running\n\nERROR:\n" + str(e))
# End of change : Added Cloud Computing (Transfer Data) functionality