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
217 lines (156 loc) · 6.76 KB
/
Copy pathmain.py
File metadata and controls
217 lines (156 loc) · 6.76 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
import time
import traceback
import sys
try:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
except:
try:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
except:
from PySide.QtGui import *
from PySide.QtCore import *
def log_uncaught_exceptions(ex_cls, ex, tb):
text = f'{ex_cls.__name__}: {ex}:\n'
text += ''.join(traceback.format_tb(tb))
print(text)
QMessageBox.critical(None, 'Error', text)
sys.exit(1)
sys.excepthook = log_uncaught_exceptions
class MainWindow(QWidget):
ESCAPE_RULES = [
('"', '"', r'\"', True),
("'", "'", r"\'", False),
(r'\n', "\n", r'\n', True),
(r'\t', "\t", r'\t', False),
(r'\b', "\b", r'\b', False),
(r'\f', "\f", r'\f', False),
('\\', "\\", '\\\\', False),
]
TITLE = 'EscapeString'
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle(self.TITLE)
self.escape_char_by_checkbox = {}
self.char_by_escape = {}
button_layout = QHBoxLayout()
for title, char, escape, checked in self.ESCAPE_RULES:
checkbox = QCheckBox(title)
checkbox.setChecked(checked)
checkbox.clicked.connect(self.input_text_changed)
font = checkbox.font()
font.setBold(True)
font.setPointSize(font.pointSize() + 2)
checkbox.setFont(font)
button_layout.addWidget(checkbox)
self.escape_char_by_checkbox[char] = checkbox
self.char_by_escape[char] = escape
button_layout.addStretch()
self.cb_save_multiline = QCheckBox('Save multiline')
self.cb_save_multiline.setChecked(False)
self.cb_save_multiline.clicked.connect(self.input_text_changed)
button_layout.addWidget(self.cb_save_multiline)
self.cb_string_literal = QCheckBox('String literal')
self.cb_string_literal.setChecked(True)
self.cb_string_literal.clicked.connect(self.input_text_changed)
button_layout.addWidget(self.cb_string_literal)
layout = QVBoxLayout()
layout.addLayout(button_layout)
self.text_edit_input = QPlainTextEdit()
self.text_edit_output = QPlainTextEdit()
self.text_edit_output.setReadOnly(True)
self.label_error = QLabel()
self.label_error.setStyleSheet("QLabel { color : red; }")
self.label_error.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.label_error.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self.button_detail_error = QPushButton('...')
self.button_detail_error.setFixedSize(20, 20)
self.button_detail_error.setToolTip('Detail error')
self.button_detail_error.clicked.connect(self.show_detail_error_massage)
self.button_detail_error.hide()
self.last_error_message = None
self.last_detail_error_message = None
self.text_edit_input.textChanged.connect(self.input_text_changed)
self.label_input_number = QLabel()
self.label_output_number = QLabel()
splitter = QSplitter()
splitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
splitter.addWidget(self.text_edit_input)
splitter.addWidget(self.text_edit_output)
layout.addWidget(splitter)
layout_error = QHBoxLayout()
layout_error.addWidget(self.label_error)
layout_error.addWidget(self.button_detail_error)
layout.addLayout(layout_error)
self.setLayout(layout)
def show_detail_error_massage(self):
if not self.last_error_message or not self.last_detail_error_message:
return
message = self.last_error_message + '\n\n' + self.last_detail_error_message
mb = QErrorMessage()
mb.setWindowTitle('Error')
# Сообщение ошибки содержит отступы, символы-переходы на следующую строку,
# которые поломаются при вставке через QErrorMessage.showMessage, и нет возможности
# выбрать тип текста, то делаем такой хак.
mb.findChild(QTextEdit).setPlainText(message)
mb.exec_()
def _escape(self, in_text: str, ignored='') -> str:
out_text = []
for char in in_text:
# Если char нет в словаре экранирования, или флаг экранирования для char выключен,
# или char среди игнорируемых
if char not in self.escape_char_by_checkbox \
or not self.escape_char_by_checkbox[char].isChecked()\
or char in ignored:
out_text.append(char)
continue
out_text.append(self.char_by_escape[char])
return ''.join(out_text)
def input_text_changed(self):
self.label_error.clear()
self.button_detail_error.hide()
self.last_error_message = None
self.last_detail_error_message = None
try:
t = time.perf_counter()
in_text = self.text_edit_input.toPlainText()
if self.cb_save_multiline.isChecked():
lines = []
for line in in_text.splitlines(keepends=True):
line = self._escape(line)
lines.append(f'"{line}"')
out_text = ' +\n'.join(lines)
else:
out_text = self._escape(in_text)
if self.cb_string_literal.isChecked():
if not out_text.startswith('"'):
out_text = '"' + out_text
if not out_text.endswith('"'):
out_text += '"'
out_text += ';'
self.text_edit_output.setPlainText(out_text)
self.setWindowTitle(
f'{self.TITLE} (number of characters: '
f'{len(self.text_edit_input.toPlainText())} -> '
f'{len(self.text_edit_output.toPlainText())})'
)
print('Escape for {:.6f} secs'.format(time.perf_counter() - t))
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)
if __name__ == '__main__':
app = QApplication([])
mw = MainWindow()
mw.resize(650, 500)
mw.show()
sys.exit(app.exec_())