/
writer.py
422 lines (336 loc) · 14.8 KB
/
writer.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3.4
#-*- coding: UTF-8 -*-
from PyQt4 import QtCore, QtGui, uic
import sys
import os
import pickle
import time
import enchant as en
from enchant.checker import SpellChecker
from enchant.tokenize import EmailFilter, URLFilter
text = None
savedText = None
recentPath = None
words = None
text_dic = None
en_dic = None
count = 1
autosaveTime = 5 * 60
checkerCount = 1
class AutoSave(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def run(self):
try:
global count
while True:
time.sleep(autosaveTime)
if window.editor.toPlainText() and window.editor.toPlainText() != savedText:
saveFile(os.path.expanduser('~/.ExPy/autosave-{}.txt'.format(count)))
print('[DEBUG] Autosaved')
if count == 20:
count = 1
else:
count += 1
except Exception:
pass
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self,parent)
uic.loadUi('writer.ui', self)
def closeEvent(self, event):
if quit_p() == 'cancel':
event.ignore()
else:
event.accept()
def resizeEvent(self, event):
event.accept()
self.editor.setFixedHeight(self.height()-45)
self.editor.setFixedWidth(self.width())
class Checker(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self,parent)
uic.loadUi('writer_o.ui', self)
def checkErrors():
global text, checkerCount, words, text_dic
words = []
text = window.editor.toPlainText()
text_dic.set_text(text)
checker.setWindowModality(QtCore.Qt.ApplicationModal)
for err in text_dic:
words.append(err.word)
if words:
checkErrorsOb()
checker.show()
else:
window.statusbar.showMessage('Ошибок не обнаружено', msecs =5000)
def checkErrorsOb():
global words, text
if words:
cur = words[0]
items = tuple(en_dic.suggest(cur))
checker.text.setText('Возможно, допущена ошибка в слове "{}" '.format(cur))
checker.variants.clear()
checker.variants.addItems(items)
checker.variants.setCurrentRow(0)
else:
checker.hide()
window.editor.setText(text)
def checkerReplaceAction():
global text, words
text = text.replace(words[0], checker.variants.currentItem().text())
del words[0]
window.editor.setText(text)
checkErrorsOb()
def checkerNext():
global words
del words[0]
checkErrorsOb()
def checkerAdd():
global words
if os.path.exists(os.path.expanduser('~/.ExPy/dict.txt')):
reg = 'a'
else:
reg = 'w'
with open(os.path.expanduser('~/.ExPy/dict.txt'), reg) as f:
f.write(words[0] + '\n')
del words[0]
checkErrorsOb()
def checkerCancel():
global words
words = None
checkErrorsOb()
def getCharsCount():
cursor = window.editor.textCursor()
text = cursor.selectedText()
stats_t = (len(text.strip()),
len(text.strip().replace(' ','').replace('\n',
'').replace('\t','')),
len(text.strip().split()))
stats = (len(window.editor.toPlainText().strip()),
len(window.editor.toPlainText().strip().replace(' ','').replace('\n',
'').replace('\t','')),
len(window.editor.toPlainText().strip().split()))
reply = QtGui.QMessageBox.information(None,
"Статистика", """
\t\tДокумент:\tВыделение:
Знаков:\t\t{0}\t{3}
Знаков (б/п):\t{1}\t{4}
Слов:\t\t{2}\t{5}
""".format(str(stats[0]),str(stats[1]),str(stats[2]), str(stats_t[0]),str(stats_t[1]),str(stats_t[2])))
def getAutosaveTime():
global autosaveTime
i = QtGui.QInputDialog.getInteger(None,"Настройка автосохранения",
"Укажите интервал между автосохранениями (в секундах):", autosaveTime,
60, 10800, 60)
if i:
autosaveTime = i[0]
print('[DEBUG] Autosave interval set to ', i[0])
def openFile(path=None):
if savedText != window.editor.toPlainText() and window.editor.toPlainText():
reply = QtGui.QMessageBox.question(None, 'Файл не сохранен',
"Текущий файл не сохранен. Сохранить?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel)
if reply == QtGui.QMessageBox.Yes:
saveFile(recentPath)
elif reply == QtGui.QMessageBox.Cancel:
return None
if not path:
if recentPath:
home_path = os.path.split(recentPath)[0] + '/'
else:
home_path = ''
path = QtGui.QFileDialog.getOpenFileName(None, "Открыть файл",
home_path, 'Текстовые файлы (*.txt)')
if path:
inFile = QtCore.QFile(path)
if inFile.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
text = inFile.readAll()
try:
# Python v3
try:
text = str(text, encoding='utf-8')
except UnicodeDecodeError:
text = str(text, encoding='windows-1251')
except TypeError:
# Python v2
text = str(text)
global savedText, recentPath
savedText = text
recentPath = path
window.editor.setPlainText(text)
with open('style.qss') as f:
window.editor.setStyleSheet(f.read())
print('[DEBUG] Cursor position: ', cursor.position())
def saveFile(path=None):
if not path:
if recentPath:
path = QtGui.QFileDialog.getSaveFileName(None, 'Сохранить файл', os.path.split(recentPath)[1], '.txt')
else:
path = QtGui.QFileDialog.getSaveFileName(None, 'Сохранить файл', '.txt')
if path:
inFile = open(path, 'w')
inFile.write(window.editor.toPlainText())
inFile.close()
global savedText, recentFilePath
saved = True
recentFilePath = path
savedText = window.editor.toPlainText()
window.statusbar.showMessage('Сохранено', msecs =5000)
def saveFileAs():
path = QtGui.QFileDialog.getSaveFileName(None, 'Сохранить файл', '.txt')
if path:
inFile = open(path, 'w')
inFile.write(window.textarea.toPlaitText())
inFile.close()
global savedText, recentFilePath
saved = True
recentFilePath = path
savedText = window.editor.toPlainText()
def newFile():
global recentPath
if savedText != window.editor.toPlainText() and window.editor.toPlainText():
reply = QtGui.QMessageBox.question(None, 'Файл не сохранен',
"Текущий файл не сохранен. Сохранить?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel)
if reply == QtGui.QMessageBox.Yes:
saveFile(recentPath)
elif reply == QtGui.QMessageBox.Cancel:
return None
window.editor.clear()
recentPath = None
window.statusbar.showMessage('Текстовое поле очищено', msecs = 5000)
def loadDicts():
global text_dic, en_dic
en_dic = en.Dict("ru_RU")
if en.dict_exists("ru_RU"):
if os.path.exists(os.path.expanduser('~/.ExPy/dict.txt')):
buf_dic = en.DictWithPWL("ru_RU",os.path.expanduser('~/.ExPy/dict.txt'))
text_dic = SpellChecker(buf_dic, filters=[EmailFilter,URLFilter])
else:
text_dic = SpellChecker("ru_RU", filters=[EmailFilter,URLFilter])
else:
QtGui.QMessageBox.information(None,
"Не обнаружен словарь", "Не обнаружен русский словарь Hunspell, проверка орфографии невозможна")
def textChanged():
selection = QtGui.QTextEdit.ExtraSelection()
selection.format.setBackground(QtGui.QColor('#555753'))
selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True)
selection.cursor = window.editor.textCursor()
selection.cursor.clearSelection()
selections = [selection]
window.editor.setExtraSelections(selections)
cursor = window.editor.textCursor()
cursor_pos = cursor.position()
print('[DEBUG] cursor position: ', cursor.position())
def auto_replace():
auto_replace = {'--': '—', '<<':'«', '>>': '»'}
cursor = window.editor.textCursor()
cursor_pos = cursor.position()
if window.editor.toPlainText()[cursor.position()-2:cursor.position()] in auto_replace and window.auto_replace.isChecked():
text = window.editor.toPlainText()
print(text[cursor_pos-2:cursor_pos])
text = text.replace(text[cursor_pos-2:cursor_pos], auto_replace[text[cursor_pos-2:cursor_pos]])
print(text)
window.editor.setPlainText(text)
cursor.setPosition(cursor_pos-1)
window.editor.setTextCursor(cursor)
def openAutosaveDir():
print('[DEBUG] Открытие паки с автосохранениями.')
p = sys.platform
if p == 'linux' or p == 'darwin':
os.system('xdg-open ~/.ExPy')
elif p == 'win32':
os.system('start "" "{}"'.format(os.path.expanduser('~/.ExPy/')))
##############################################################################################################################################
def quit_p():
print()
cursor = window.editor.textCursor()
data = {'recPath': recentPath, 'curPos': cursor.position(), 'cnt': count, 'ast': autosaveTime, 'w': window.width(), 'h': window.height() }
with open(os.path.expanduser('~/.ExPy/settings'), 'wb') as f:
pickle.dump(data, f)
if savedText != window.editor.toPlainText() and window.editor.toPlainText():
reply = QtGui.QMessageBox.question(None, 'Файл не сохранен',
"Текущий файл не сохранен. Сохранить?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel)
if reply == QtGui.QMessageBox.Yes:
saveFile(recentPath)
elif reply == QtGui.QMessageBox.Cancel:
return 'cancel'
sys.exit()
##############################################################################################################################################
## START
## START
##############################################################################################################################################
app = QtGui.QApplication(sys.argv)
window = MainWindow()
checker = Checker()
clipboard = app.clipboard()
cursor = window.editor.textCursor()
textChanged()
##############################################################################################################################################
QtCore.QObject.connect(window.open_f, QtCore.SIGNAL('triggered()'), openFile)
QtCore.QObject.connect(window.save_f, QtCore.SIGNAL('triggered()'), saveFile)
QtCore.QObject.connect(window.save_as, QtCore.SIGNAL('triggered()'), saveFileAs)
QtCore.QObject.connect(window.new_f, QtCore.SIGNAL('triggered()'), newFile)
QtCore.QObject.connect(window.autosave_s, QtCore.SIGNAL('triggered()'), getAutosaveTime)
QtCore.QObject.connect(window.open_autosave, QtCore.SIGNAL('triggered()'), openAutosaveDir)
QtCore.QObject.connect(window.quit_p, QtCore.SIGNAL('triggered()'), quit_p)
QtCore.QObject.connect(window.get_stats, QtCore.SIGNAL('triggered()'), getCharsCount)
QtCore.QObject.connect(window.check_o, QtCore.SIGNAL('triggered()'), checkErrors)
QtCore.QObject.connect(window.editor, QtCore.SIGNAL('cursorPositionChanged()'), textChanged)
QtCore.QObject.connect(window.editor, QtCore.SIGNAL('textChanged()'), auto_replace)
##############################################################################################################################################
QtCore.QObject.connect(checker.replace, QtCore.SIGNAL('clicked()'), checkerReplaceAction)
QtCore.QObject.connect(checker.add, QtCore.SIGNAL('clicked()'), checkerAdd)
QtCore.QObject.connect(checker.next, QtCore.SIGNAL('clicked()'), checkerNext)
QtCore.QObject.connect(checker.quit_c, QtCore.SIGNAL('clicked()'), checkerCancel)
QtCore.QObject.connect(checker.variants, QtCore.SIGNAL('itemDoubleClicked()'), checkerReplaceAction)
##############################################################################################################################################
## ON START
## ON START
##############################################################################################################################################
try:
os.mkdir(os.path.expanduser('~/.ExPy'))
if sys.platform == 'win32':
os.system('attrib +H "" "{}"'.format(os.path.expanduser('~/.ExPy/')))
except OSError as e:
print(e)
if os.path.exists(os.path.expanduser('~/.ExPy/settings')):
with open(os.path.expanduser('~/.ExPy/settings'), 'rb') as f:
try:
data = pickle.load(f)
print(data)
except pickle.PickleError:
print('[DEBUG] PickleError')
data = {'recPath': None, 'curPos': None, 'cnt': None, 'ast': 300, 'w': 700, 'h': 750}
else:
data = {'recPath': None, 'curPos': None, 'cnt': None, 'ast': 300, 'w': 700, 'h': 750}
recentPath = data['recPath']
if recentPath:
openFile(recentPath)
if data['curPos']:
print('[DEBUG] Cursor position loaded', data['curPos'])
cursor.setPosition(data['curPos'])
window.editor.setTextCursor(cursor)
else:
newFile()
if data.get('cnt'):
count = data['cnt']
if data.get('ast'):
autosaveTime = data['ast']
if not data.get('w'):
data['w'] = 700; data['h'] = 750
window.resize(data['w'], data['h'])
window.editor.setFixedHeight(window.height()-45)
window.editor.setFixedWidth(window.width())
del data
with open('style.qss') as f:
window.editor.setStyleSheet(f.read())
en.set_param("enchant.hunspell.dictionary.path", '')
##############################################################################################################################################
loadDicts()
window.show()
autosave = AutoSave()
autosave.start()
sys.exit(app.exec_())