forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_base64.py
More file actions
275 lines (220 loc) · 7.24 KB
/
Copy pathgui_base64.py
File metadata and controls
275 lines (220 loc) · 7.24 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import base64
import traceback
import sys
from os.path import split as path_split
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
# from encodings.aliases import aliases
# print(aliases)
# OR:
STANDART_ENCODINGS = [
"ascii",
"big5",
"big5hkscs",
"cp037",
"cp424",
"cp437",
"cp500",
"cp720",
"cp737",
"cp775",
"cp850",
"cp852",
"cp855",
"cp856",
"cp857",
"cp858",
"cp860",
"cp861",
"cp862",
"cp863",
"cp864",
"cp865",
"cp866",
"cp869",
"cp874",
"cp875",
"cp932",
"cp949",
"cp950",
"cp1006",
"cp1026",
"cp1140",
"cp1250",
"cp1251",
"cp1252",
"cp1253",
"cp1254",
"cp1255",
"cp1256",
"cp1257",
"cp1258",
"euc_jp",
"euc_jis_2004",
"euc_jisx0213",
"euc_kr",
"gb2312",
"gbk",
"gb18030",
"hz",
"iso2022_jp",
"iso2022_jp_1",
"iso2022_jp_2",
"iso2022_jp_2004",
"iso2022_jp_3",
"iso2022_jp_ext",
"iso2022_kr",
"latin_1",
"iso8859_2",
"iso8859_3",
"iso8859_4",
"iso8859_5",
"iso8859_6",
"iso8859_7",
"iso8859_8",
"iso8859_9",
"iso8859_10",
"iso8859_13",
"iso8859_14",
"iso8859_15",
"iso8859_16",
"johab",
"koi8_r",
"koi8_u",
"mac_cyrillic",
"mac_greek",
"mac_iceland",
"mac_latin2",
"mac_roman",
"mac_turkish",
"ptcp154",
"shift_jis",
"shift_jis_2004",
"shift_jisx0213",
"utf_32",
"utf_32_be",
"utf_32_le",
"utf_16",
"utf_16_be",
"utf_16_le",
"utf_7",
"utf_8",
"utf_8_sig",
]
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle(path_split(__file__)[1])
self.button_direct = QPushButton()
self.button_direct.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self.cb_encoding = QComboBox()
self.cb_encoding.addItems(STANDART_ENCODINGS)
self.cb_encoding.setFixedWidth(100)
index = self.cb_encoding.findText("utf_8")
self.cb_encoding.setCurrentIndex(index)
self.cb_raw = QCheckBox("raw")
button_layout = QHBoxLayout()
button_layout.addWidget(self.button_direct)
button_layout.addWidget(self.cb_encoding)
button_layout.addWidget(self.cb_raw)
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.last_error_message = None
self.last_detail_error_message = None
# True -- кодирование текста, False -- раскодирование
self.direct_encode_text = True
# Первый вызов, чтобы у кнопки появился текст (заодно это сменит режим кодирования)
self.change_convert_direct()
self.button_direct.clicked.connect(self.change_convert_direct)
self.text_edit_input.textChanged.connect(self.input_text_changed)
self.cb_encoding.currentIndexChanged.connect(self.input_text_changed)
self.cb_raw.clicked.connect(self.input_text_changed)
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):
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 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:
codec_name = self.cb_encoding.currentText()
in_text = self.text_edit_input.toPlainText().encode(encoding=codec_name)
if self.direct_encode_text:
text = base64.b64encode(in_text)
else:
text = base64.b64decode(in_text)
# Для 'raw' не делаем декодирование, а показываем представление объекта как строку
if self.cb_raw.isChecked():
text = repr(text)
else:
# Параметр errors='replace' нужен для того, чтобы при декодировании в строку проблемные символы
# заменялись символами-заменителями (�)
text = text.decode(encoding=codec_name, errors="replace")
self.text_edit_output.setPlainText(text)
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 change_convert_direct(self):
self.direct_encode_text = not self.direct_encode_text
self.button_direct.setText(
"text -> base64" if self.direct_encode_text else "base64 -> text"
)
self.input_text_changed()
if __name__ == "__main__":
app = QApplication([])
mw = MainWindow()
mw.resize(650, 500)
mw.show()
sys.exit(app.exec_())