-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (128 loc) · 6.96 KB
/
main.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
import sys
from typing import List
from resources import resources_rc
from utils.file_manager import FileManager, FileSystemObject
from utils.network_manager import NetworkManager
from custom_components.custom_completer import CustomCompleter
from PyQt5 import uic
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem,QMessageBox, QTreeWidget, QTreeWidgetItem
from PyQt5.QtCore import QDir, Qt
from flatten_json import flatten
class Workbench(QMainWindow):
def __init__(self):
super().__init__()
# Remove unused pylint warning
_ = resources_rc
self.configuration = FileManager.deserialize_configuration_file("./config.yml")
# Load the interface.ui file
uic.loadUi('./interface.ui', self)
self.custom_completer = CustomCompleter()
# Connect uiBtnExecute to the onClickUiBtnExecuteEvent function
self.uiBtnExecute.aboutToShow.connect(self.__on_uibtnexecute_clicked_event)
# Connect uiBtnExit to the onClickUiBtnExitEvent function
self.uiBtnExit.triggered.connect(self.__on_uibtnexit_clicked_event)
# Set the column label for the uiTreeRules widget
self.uiTreeRules.setHeaderLabels(['Name', 'Type'])
# Register the double click event for the uiTreeRules item
self.uiTreeRules.itemDoubleClicked.connect(self.__on_ui_tree_rules_item_double_clicked)
# Register the double click event for the uiListFields item
self.uiListFields.itemDoubleClicked.connect(self.__on_list_widget_item_double_clicked)
# Populate the uiTreeRules Widget with the rule set
self.__fill_tree_widget(self.uiTreeRules,
FileManager.generate_folder_tree(self.configuration["rules_path"]))
# Instantiate the NetworkManager
self.network_manager = NetworkManager(self, self.configuration["rumbledb"]["url"],
self.configuration["rumbledb"]["materialization_cap"])
self.network_manager.check_connection_signal.connect(self.handle_connection_check)
self.network_manager.run_query_signal.connect(self.handle_run_query)
self.network_manager.check_connection()
self.verticalLayout_2.replaceWidget(self.uiTxtQuery, self.custom_completer)
self.document_fields = None
def __on_ui_tree_rules_item_double_clicked(self, item):
if item.text(1) == "File":
file_path = item.data(0, Qt.UserRole)
self.custom_completer.setText(FileManager.read_file_content(file_path, 'r', 'utf-8'))
def __on_list_widget_item_double_clicked(self, item):
cursor = self.custom_completer.textCursor()
#cursor.movePosition(cursor.End)
self.custom_completer.setTextCursor(cursor)
# Insert text at cursor position
self.custom_completer.insertPlainText(item.text())
def add_items_to_listwidget(self, list_widget, items):
"""
Adds a list of strings to the specified QListWidget.
Args:
list_widget (QListWidget): The QListWidget instance to which the items will be added.
items (list of str): A list of strings that will be added to the QListWidget.
"""
list_widget.clear()
for item in items:
list_widget.addItem(item)
def __fill_tree_widget(self, ui_tree: QTreeWidget, elements_to_show: List[FileSystemObject]):
"""
Populates the given QTreeWidget with items based on the provided elements.
"""
for element in elements_to_show:
item = QTreeWidgetItem()
icon = ":/ico/ico_document.ico" if element.type == "File" else ":/ico/ico_binder.ico"
item.setIcon(0, QIcon(icon))
item.setText(0, QDir(element.path).dirName())
item.setText(1, element.type)
# Set custom data for item selection ( __on_ui_tree_rules_item_double_clicked )
item.setData(0, Qt.UserRole, element.path)
item.setData(1, Qt.UserRole, element.type)
if isinstance(ui_tree, QTreeWidget):
ui_tree.addTopLevelItem(item)
elif isinstance(ui_tree, QTreeWidgetItem):
ui_tree.addChild(item)
if element.type == "Folder":
self.__fill_tree_widget(item, element.children)
def __on_uibtnexit_clicked_event(self):
self.close()
def __on_uibtnexecute_clicked_event(self):
self.uiBtnExecute.setEnabled(False)
self.network_manager.run_query(self.custom_completer.toPlainText())
def handle_connection_check(self, success: bool):
if not success:
QMessageBox.critical(self,"Error","Backend not connected!")
def handle_run_query(self, json_data) -> None:
self.uiBtnExecute.setEnabled(True)
# Checks if there are errors:
if "error-code" in json_data:
QMessageBox.critical(self, json_data["error-code"],json_data["error-message"])
return
if len(json_data['values']) > 0:
# Prints a simple list
if not isinstance(json_data['values'][0], dict):
self.table.setRowCount(len(json_data['values']))
self.table.setColumnCount(len(json_data))
#if self.document_fields is None:
self.document_fields = sorted(list(json_data.keys()))
for col_num, header in enumerate(self.document_fields):
self.table.setHorizontalHeaderItem(col_num, QTableWidgetItem(header))
for row_num, row_data in enumerate(json_data["values"]):
self.table.setItem(row_num, 0, QTableWidgetItem(str(row_data)))
# Prints a list of objects
if isinstance(json_data['values'][0], dict):
json_data = [flatten(item,separator=".") for item in json_data['values']]
if json_data:
self.table.setRowCount(len(json_data))
self.table.setColumnCount(len(json_data[0]))
self.document_fields = sorted(list({key for d in json_data for key in d.keys()}))
self.custom_completer.setWordlist(self.document_fields)
self.add_items_to_listwidget(self.uiListFields, self.document_fields)
for col_num, header in enumerate(self.document_fields):
self.table.setHorizontalHeaderItem(col_num, QTableWidgetItem(header))
for row_num, row_data in enumerate(json_data):
for col_num, key in enumerate(self.document_fields):
self.table.setItem(row_num, col_num, QTableWidgetItem(str(row_data.get(key, ''))))
else:
self.table.setRowCount(0)
self.table.setColumnCount(0)
QMessageBox.information(self,"Info","No resultset found!")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Workbench()
ex.show()
sys.exit(app.exec_())