forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
126 lines (85 loc) · 3.64 KB
/
Copy pathmain.py
File metadata and controls
126 lines (85 loc) · 3.64 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import traceback
import sys
from PyQt5 import Qt
# pip install cssselect
from cssselect import HTMLTranslator
css_to_xpath = HTMLTranslator(xhtml=True).css_to_xpath
def log_uncaught_exceptions(ex_cls, ex, tb):
text = f"{ex_cls.__name__}: {ex}:\n"
text += "".join(traceback.format_tb(tb))
print(text)
Qt.QMessageBox.critical(None, "Error", text)
sys.exit(1)
sys.excepthook = log_uncaught_exceptions
class MainWindow(Qt.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("css_to_xpath__gui")
self.text_edit_input = Qt.QPlainTextEdit()
self.text_edit_output = Qt.QPlainTextEdit()
self.label_error = Qt.QLabel()
self.label_error.setStyleSheet("QLabel { color : red; }")
self.label_error.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
self.label_error.setSizePolicy(
Qt.QSizePolicy.Expanding, Qt.QSizePolicy.Preferred
)
self.button_detail_error = Qt.QPushButton("...")
self.button_detail_error.setFixedSize(20, 20)
self.button_detail_error.setToolTip("Detail error")
self.button_detail_error.hide()
self.last_error_message = None
self.last_detail_error_message = None
self.text_edit_input.textChanged.connect(self.on_process)
self.button_detail_error.clicked.connect(self.show_detail_error_message)
splitter = Qt.QSplitter()
splitter.setSizePolicy(Qt.QSizePolicy.Expanding, Qt.QSizePolicy.Expanding)
splitter.addWidget(self.text_edit_input)
splitter.addWidget(self.text_edit_output)
layout = Qt.QVBoxLayout()
layout.addWidget(splitter)
layout_error = Qt.QHBoxLayout()
layout_error.addWidget(self.label_error)
layout_error.addWidget(self.button_detail_error)
layout.addLayout(layout_error)
self.setLayout(layout)
def on_process(self):
self.text_edit_output.clear()
self.label_error.clear()
self.button_detail_error.hide()
self.last_error_message = None
self.last_detail_error_message = None
try:
text = self.text_edit_input.toPlainText()
if not text.strip():
return
output = css_to_xpath(text)
self.text_edit_output.setPlainText(output)
except Exception as e:
# # Выводим ошибку в консоль
# traceback.print_exc()
# Сохраняем в переменную
tb = traceback.format_exc()
self.last_error_message = str(e)
self.last_detail_error_message = str(tb)
self.button_detail_error.show()
self.label_error.setText("Error: " + self.last_error_message)
def show_detail_error_message(self):
message = self.last_error_message + "\n\n" + self.last_detail_error_message
mb = Qt.QErrorMessage()
mb.setWindowTitle("Error")
# Сообщение ошибки содержит отступы, символы-переходы на следующую строку,
# которые поломаются при вставке через QErrorMessage.showMessage, и нет возможности
# выбрать тип текста, то делаем такой хак.
mb.findChild(Qt.QTextEdit).setPlainText(message)
mb.exec_()
if __name__ == "__main__":
app = Qt.QApplication([])
mw = MainWindow()
mw.resize(400, 300)
mw.show()
# For example
mw.text_edit_input.setPlainText("div#main > a[href]")
app.exec_()