-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
338 lines (299 loc) · 15.5 KB
/
gui.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import logging
from PyQt5.QtWidgets import QMessageBox
from api import ApiRequest, Response, Auth
from config import AuthConfig
from data import DataHandler
from PyQt5 import uic, QtWidgets, QtCore
from pandas import DataFrame
class PandasModel(QtCore.QAbstractTableModel):
"""
Class to populate a table view with a pandas dataframe
"""
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[col]
return None
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
uic.loadUi('ui/cbcdmv1.4.ui', self)
# Initializes UI objects
self.setObjectName("CBCDM")
self.resize(1280, 834)
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.NumFoundLabel = QtWidgets.QLabel(self.centralwidget)
self.NumFoundLabel.setGeometry(QtCore.QRect(10, 110, 201, 17))
self.NumFoundLabel.setObjectName("NumFoundLabel")
self.DevicesTable = QtWidgets.QTableView(self.centralwidget)
self.DevicesTable.setGeometry(QtCore.QRect(10, 140, 991, 661))
self.DevicesTable.setProperty("toolTipDuration", 0)
self.DevicesTable.setObjectName("DevicesTable")
self.Refresh = QtWidgets.QPushButton(self.centralwidget)
self.Refresh.setGeometry(QtCore.QRect(360, 20, 141, 61))
self.Refresh.setObjectName("Refresh")
self.ColumnList = QtWidgets.QListWidget(self.centralwidget)
self.ColumnList.setGeometry(QtCore.QRect(1010, 140, 256, 661))
self.ColumnList.setObjectName("ColumnList")
__sortingEnabled = self.ColumnList.isSortingEnabled()
self.ColumnList.setSortingEnabled(False)
self.ColumnList.setSortingEnabled(__sortingEnabled)
self.ColumnList.sortItems(QtCore.Qt.AscendingOrder)
self.SelectAll = QtWidgets.QPushButton(self.centralwidget)
self.SelectAll.setGeometry(QtCore.QRect(1010, 110, 131, 27))
self.SelectAll.setObjectName("SelectAll")
self.DeselectAll = QtWidgets.QPushButton(self.centralwidget)
self.DeselectAll.setGeometry(QtCore.QRect(1144, 110, 121, 27))
self.DeselectAll.setObjectName("DeslectAll")
self.Export = QtWidgets.QPushButton(self.centralwidget)
self.Export.setGeometry(QtCore.QRect(510, 20, 141, 61))
self.Export.setObjectName("Export")
self.APIFrame = QtWidgets.QFrame(self.centralwidget)
self.APIFrame.setGeometry(QtCore.QRect(10, 10, 341, 81))
self.APIFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.APIFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.APIFrame.setObjectName("APIFrame")
self.APIComboBox = QtWidgets.QComboBox(self.APIFrame)
self.APIComboBox.setGeometry(QtCore.QRect(20, 40, 301, 25))
self.APIComboBox.setEditable(False)
self.APIComboBox.setProperty("currentText", "")
self.APIComboBox.setObjectName("APIComboBox")
self.APITitle = QtWidgets.QLabel(self.APIFrame)
self.APITitle.setGeometry(QtCore.QRect(20, 10, 81, 17))
self.APITitle.setObjectName("APITitle")
self.AddAPI = QtWidgets.QPushButton(self.centralwidget)
self.AddAPI.setGeometry(QtCore.QRect(660, 20, 141, 61))
self.AddAPI.setObjectName("AddAPIBtn")
self.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(self)
# Initialise other frames
self.api_info_dialog = Ui_APIDialog()
self.api_info_dialog.DialogButton.accepted.connect(self.write_new_api)
# Data Request and Load
self.auth_config = AuthConfig()
if self.auth_config.first_setup:
self.api_info_dialog.show()
self.auth_config.create_profile(self.api_info_dialog.CredLine.text(), self.api_info_dialog.APICombo.itemText(0),
self.api_info_dialog.TokenLine.text(), self.api_info_dialog.KeyLine.text(),
self.api_info_dialog.OrgLine.text(), "True")
self.add_api_config()
logging.debug('Number of profiles to load: ' + str(self.auth_config.get_num_profiles()))
self.auth = Auth(self.auth_config.config, self.auth_config.profiles[0])
self.data_handler = DataHandler(data_file="devices.json", data_path="data")
# Environment Variables
self.filtered = False
self.request = ApiRequest(self.auth)
self.request.http_request()
if self.request.success:
self.response = Response(self.request.json_response)
if self.data_handler.file_dump(self.response.content):
self.data_frame = DataFrame(self.data_handler.read_json_data_results())
self.data_frame_columns = []
for col in self.data_frame.columns:
self.data_frame_columns.append(col)
self.edited_data_frame = self.data_frame
self.results_model = PandasModel(self.data_frame)
self.refresh_table()
self.set_up_items()
self.bind_buttons()
self.retranslateUi(self)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.NumFoundLabel.setText(_translate("MainWindow", "Number of Results: " + str(self.response.active_results)))
self.Refresh.setText(_translate("MainWindow", "Refresh"))
self.SelectAll.setText(_translate("MainWindow", "Select All"))
self.DeselectAll.setText(_translate("MainWindow", "Deselect All"))
self.Export.setText(_translate("MainWindow", "Export to CSV"))
self.APITitle.setText(_translate("MainWindow", "Select API:"))
self.AddAPI.setText(_translate("MainWindow", "Add API"))
def set_up_items(self):
for x in self.data_frame:
item = QtWidgets.QListWidgetItem()
item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Checked)
item.setText(str(x))
self.ColumnList.addItem(item)
def add_api_config(self):
self.auth_config.load_config()
for x in range(self.auth_config.get_num_profiles()):
self.APIComboBox.addItem(self.auth_config.profiles[x])
logging.debug('Adding profile ' + str(self.auth_config.profiles[x]) + ' to combo box')
logging.debug("Number of APIs to display: " + str(self.APIComboBox.count()))
self.APIComboBox.show()
def write_new_api(self):
logging.debug("Writing new API to config file")
self.auth_config.create_profile(self.api_info_dialog.CredLine.text(),
self.api_info_dialog.APICombo.itemText(
self.api_info_dialog.APICombo.currentIndex()),
self.api_info_dialog.TokenLine.text(), self.api_info_dialog.KeyLine.text(),
self.api_info_dialog.OrgLine.text(), "True")
logging.debug("Adding APIs to combo box")
self.reset_combobox()
self.add_api_config()
def reset_combobox(self):
self.APIComboBox.close()
self.APIComboBox = QtWidgets.QComboBox(self.APIFrame)
self.APIComboBox.setGeometry(QtCore.QRect(20, 40, 301, 25))
self.APIComboBox.setEditable(False)
self.APIComboBox.setProperty("currentText", "")
self.APIComboBox.setObjectName("APIComboBox")
self.APIComboBox.currentIndexChanged.connect(self.refresh_data)
logging.debug("API Combo box reset")
def bind_buttons(self):
self.Refresh.clicked.connect(self.refresh_data)
self.Export.clicked.connect(self.export_data)
self.SelectAll.clicked.connect(self.select_all)
self.DeselectAll.clicked.connect(self.deselect_all)
self.AddAPI.clicked.connect(self.add_api)
self.APIComboBox.currentIndexChanged.connect(self.refresh_data)
logging.debug("Functions bound to buttons")
def refresh_data(self):
logging.info('Refreshing data')
logging.debug('ComboBox Set to index ' + str(self.APIComboBox.currentIndex()))
logging.debug('Profile selected: ' + self.APIComboBox.itemText(self.APIComboBox.currentIndex()))
self.request = ApiRequest(
self.auth_config.load_profile(self.APIComboBox.itemText(self.APIComboBox.currentIndex())))
# Todo Consider removing the constant data dumping
self.request.http_request()
if self.request.success:
self.response = Response(self.request.json_response)
if self.data_handler.file_dump(self.response.content):
self.data_frame = DataFrame(self.data_handler.read_json_data_results())
self.data_frame_columns = []
for col in self.data_frame.columns:
self.data_frame_columns.append(col)
self.edited_data_frame = self.data_frame
self.refresh_table()
else:
QMessageBox.about(self, "Failed Request", "Failed to Pull Data from API, Error code:"
+ str(self.request.response_code))
def refresh_table(self):
for x in range(self.ColumnList.__len__()):
if not self.ColumnList.item(x).checkState():
logging.info("Data is now filtered")
self.filtered = True
break
else:
self.filtered = False
if self.filtered:
self.edited_data_frame = self.filter_data(self.data_frame)
logging.info("Setting table with filtered data")
self.results_model = PandasModel(self.edited_data_frame)
self.DevicesTable.setModel(self.results_model)
else:
logging.info("Setting table with default data")
self.results_model = PandasModel(self.data_frame)
self.DevicesTable.setModel(self.results_model)
self.DevicesTable.create()
self.NumFoundLabel.setText("Number of Results: " + str(self.response.active_results))
def filter_data(self, data_frame):
logging.debug("Filtering Data")
for x in range(self.ColumnList.__len__()):
if not self.ColumnList.item(x).checkState():
try:
data_frame = data_frame.drop(columns=self.ColumnList.item(x).text())
except KeyError:
logging.warning("Could not find column " + self.ColumnList.item(x).text())
continue
return data_frame
def export_data(self):
if self.filtered:
self.data_handler.print_to_csv(self.edited_data_frame)
else:
self.data_handler.print_to_csv(self.data_frame)
QMessageBox.about(self, "Export Data", "Data has been exported to CSV")
def select_all(self):
for x in range(self.ColumnList.__len__()):
self.ColumnList.item(x).setCheckState(QtCore.Qt.Checked)
def deselect_all(self):
for x in range(self.ColumnList.__len__()):
self.ColumnList.item(x).setCheckState(QtCore.Qt.Unchecked)
def add_api(self):
logging.info("Adding new API")
self.api_info_dialog.show()
class Ui_APIDialog(QtWidgets.QDialog):
def __init__(self):
super(Ui_APIDialog, self).__init__()
self.setObjectName("APIDialog")
self.resize(333, 400)
self.DialogButton = QtWidgets.QDialogButtonBox(self)
self.DialogButton.setGeometry(QtCore.QRect(130, 350, 181, 32))
self.DialogButton.setOrientation(QtCore.Qt.Horizontal)
self.DialogButton.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
self.DialogButton.setObjectName("DialogButton")
self.APICombo = QtWidgets.QComboBox(self)
self.APICombo.setGeometry(QtCore.QRect(20, 100, 291, 27))
self.APICombo.setObjectName("APICombo")
self.TokenLine = QtWidgets.QLineEdit(self)
self.TokenLine.setGeometry(QtCore.QRect(20, 160, 291, 29))
self.TokenLine.setObjectName("TokenLine")
self.OrgLine = QtWidgets.QLineEdit(self)
self.OrgLine.setGeometry(QtCore.QRect(20, 300, 291, 29))
self.OrgLine.setObjectName("OrgLine")
self.APIURLLabel = QtWidgets.QLabel(self)
self.APIURLLabel.setGeometry(QtCore.QRect(20, 80, 66, 17))
self.APIURLLabel.setObjectName("APIURLLabel")
self.TokenLabel = QtWidgets.QLabel(self)
self.TokenLabel.setGeometry(QtCore.QRect(20, 140, 91, 17))
self.TokenLabel.setObjectName("TokenLabel")
self.OrgLabel = QtWidgets.QLabel(self)
self.OrgLabel.setGeometry(QtCore.QRect(20, 280, 121, 17))
self.OrgLabel.setObjectName("OrgLabel")
self.CredLabel = QtWidgets.QLabel(self)
self.CredLabel.setGeometry(QtCore.QRect(20, 20, 161, 17))
self.CredLabel.setObjectName("CredLabel")
self.CredLine = QtWidgets.QLineEdit(self)
self.CredLine.setGeometry(QtCore.QRect(20, 40, 291, 29))
self.CredLine.setText("")
self.CredLine.setObjectName("CredLine")
self.KeyLabel = QtWidgets.QLabel(self)
self.KeyLabel.setGeometry(QtCore.QRect(20, 200, 66, 17))
self.KeyLabel.setObjectName("KeyLabel")
self.KeyLine = QtWidgets.QLineEdit(self)
self.KeyLine.setGeometry(QtCore.QRect(20, 230, 291, 29))
self.KeyLine.setObjectName("KeyLine")
self.retranslateUi(self)
self.DialogButton.accepted.connect(self.parse_api)
self.DialogButton.rejected.connect(self.reject)
QtCore.QMetaObject.connectSlotsByName(self)
self.APICombo.addItem("https://defense-eu.conferdeploy.net")
self.APICombo.addItem("https://defense-prod05.conferdeploy.net")
# API Values
self.cred = ""
self.token = ""
self.url = ""
self.org = ""
self.key = ""
def retranslateUi(self, APIDialog):
_translate = QtCore.QCoreApplication.translate
APIDialog.setWindowTitle(_translate("APIDialog", "Dialog"))
self.APIURLLabel.setText(_translate("APIDialog", "API URL:"))
self.TokenLabel.setText(_translate("APIDialog", "API Token:"))
self.OrgLabel.setText(_translate("APIDialog", "API Org Key:"))
self.CredLabel.setText(_translate("APIDialog", "Credential Set Name:"))
self.KeyLabel.setText(_translate("APIDialog", "API Key:"))
def parse_api(self):
logging.debug("Gathering info from entered API details")
# Todo Flesh out the input validation here
self.cred = self.CredLine.text()
self.token = self.TokenLine.text()
self.url = self.APICombo.itemText(self.APICombo.currentIndex())
self.org = self.OrgLine.text()
self.key = self.KeyLine.text()
self.close()
# Todo Fix tab for API dialog
# Todo add option to save filtered view
# Todo figure out why some machines are not appearing in the results (linux machines for instance)