-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheditor.py
539 lines (390 loc) · 17.9 KB
/
editor.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# editor.py
# [*] Catching too general exception
# pylint: disable=W0718
# [*] Attribute defined outside init
# pylint: disable=W0201
# [*] Pointless string statement
# pylint: disable=W0105
# [i] General imports
from typing import Any, Callable, Literal, Iterable
import sys
# [i] Tkinter imports
from tkinter import Canvas, Misc, END, WORD, INSERT, SEL, SEL_FIRST, SEL_LAST, Toplevel, IntVar, TclError
from tkinter.ttk import Checkbutton, Button, Frame, Label, Entry
from tkinter.scrolledtext import ScrolledText as _ScrolledText
from tkinter.font import Font
from tkinter import messagebox as mb
from tkinter import colorchooser as picker
# [*] Samples
TITLE_CASING = 'Title Casing'
UPPER_CASING = 'UPPER CASING'
LOWER_CASING = 'lower casing'
PASCAL_CASING = 'PascalCasing'
CONSTANT_CASING = 'CONSTANT_CASING',
SNAKE_CASING = 'snake_casing',
CAMEL_CASING = 'camelCasing'
KEBAB_CASING = 'kebab-case'
TRAIN_CASING = 'Train-Case'
COBOL_CASING = 'COBOL-CASE'
INVERTED_CASING_1 = 'InVeRtEd cAsInG 1'
INVERTED_CASING_2 = 'iNvErTeD CaSiNg 2'
class WriterClassicEditor(_ScrolledText):
"""
Provides an editor interface for WriterClassic.
"""
def __init__(self, master: Misc | None = None, **kwargs: Any) -> None:
kwargs.update({'wrap': WORD})
self._wrapline = WORD
self.write = super().insert
super().__init__(master, **kwargs)
def change_wrapping(self, char_level: Literal['none', 'char', 'word'] = 'char'):
self._wrapline: str = char_level
super().configure(wrap=self._wrapline)
def select_text(self, **kw) -> None:
"""
select_text handles the task of selecting text with simplicity
Possible args (all optional):
start (TextIndex-like object): starting index for the selection. Defaults to 0.0.
end (TextIndex-like object): end index for the selection. Defaults to `END` ('end').
see (TextIndex-like object): the value to mark as INSERT. If `mark` is False, this argument will be ignored and the action won't be performed. Defaults to the value given by the `end` argument.
mark (bool): whether to mark INSERT as the value given by the `see` argument or not. Defaults to True.
"""
_start = kw.get('start', 0.0)
_end = kw.get('end', END)
_mark = kw.get('mark', True)
super().tag_add(SEL, _start, _end)
if _mark:
_see = kw.get('see', _end)
super().mark_set(INSERT, _see)
super().see(INSERT)
def change_selection_casing(self, casing: str):
if not self.selection:
return
casing = casing.lower().strip()
cur_selection: str = self.selection
match casing:
case 'title':
result = cur_selection.title()
case 'upper':
result = cur_selection.upper()
case 'lower':
result = cur_selection.lower()
case 'pascal':
result = cur_selection.title().replace(' ', '')
case 'train':
result = cur_selection.title().replace(' ', '-')
case 'cobol':
result = cur_selection.upper().replace(' ', '-')
case 'kebab':
result = cur_selection.lower().replace(' ', '-')
case 'constant':
result = cur_selection.upper().replace(' ', '_')
case 'snake':
result = cur_selection.lower().replace(' ', '_')
case 'camel':
result = cur_selection[0].lower() + cur_selection.title()[1:].replace(' ', '')
case 'inverted' | 'inverted1':
cache: list[str] = [i for i in cur_selection]
for index, elem in enumerate(cache, 0):
if index == 0:
cache[index] = elem.upper()
continue
if cache[index - 1].islower():
cache[index] = elem.upper()
continue
if cache[index - 1].isupper():
cache[index] = elem.lower()
continue
result: str = ''.join(cache)
case 'inverted2' | 'alternating':
cache = [i for i in cur_selection]
for index, elem in enumerate(cache, 0):
if index == 0:
cache[index] = elem.lower()
continue
if cache[index - 1].islower():
cache[index] = elem.upper()
continue
if cache[index - 1].isupper():
cache[index] = elem.lower()
continue
result: str = ''.join(cache)
case _:
raise ValueError('no such casing')
self.replace(SEL_FIRST, SEL_LAST, result)
@property
def wrapping(self) -> str:
return self._wrapline
@property
def selection(self) -> str | Literal[False]:
"""
Current selection.
If nothing is selected, return False.
"""
try:
return self.get(SEL_FIRST, SEL_LAST)
except TclError:
return False
@property
def content(self) -> str:
"""
Returns the text in the widget.
"""
return self.get(0.0, END)
@property
def num_lines(self) -> int:
"""
Returns the number of lines.
"""
return int(self.index(f"{END} - 1 char").split('.')[0])
class SearchReplace(Toplevel):
def __init__(self, master: Misc | None = None, widget: WriterClassicEditor | None = None, regexp: bool = False, lang_exps: Iterable[str] | None = None, **kwargs) -> None:
ico = kwargs.pop('ico', None)
if not isinstance(lang_exps, (list, tuple)):
raise ValueError('lang expressions must be tuple or list')
if not widget:
raise TypeError('a widget must be specified and match type WriterClassicEditor')
self._regexp = regexp
self._exps = lang_exps
self._MARKED: bool = False
widget.tag_configure('found', background='yellow', foreground='black', font=Font(weight='bold', slant='roman', overstrike=False, underline=True))
widget.tag_remove("found", 0.0, END)
self.editor: WriterClassicEditor = widget
self.master = master
super().__init__(master, **kwargs)
super().title(f"{self.lang[1]} - {self.lang[345]}")
if sys.platform == 'win32':
super().iconbitmap(ico)
# [!?] exact is not working for some reason
# [i] this is something related to tkinter tho and not this class
# [?] I might do a bit more research on this topic ig
def initiate_setup(self, widget: Toplevel | Misc):
self._F1 = Frame(widget)
self._F2 = Frame(widget)
self._F3 = Frame(widget)
self._F4 = Frame(widget)
self._B3 = Button(widget, text=self.lang[367], command=self._mark_matches)
self._L1 = Label(self._F1, text=self.lang[346].strip() + ' ')
self._E1 = Entry(self._F1)
self._PREVBUTT = Button(self._F1, text=chr(9650), command=lambda:
self._find('prev'))
self._NEXTBUTT = Button(self._F1, text=chr(9660), command=lambda:
self._find('next'))
self._CASING = IntVar(widget, value=0)
self._C1 = Checkbutton(self._F2, text=self.lang[347], variable=self._CASING)
self._E2 = Entry(self._F3)
self._B1 = Button(self._F4, text=self.lang[368], command=lambda:
self._replace_next(self.replacewith))
self._B2 = Button(self._F4, text=self.lang[369], command=lambda:
self._replace_all(self.replacewith))
# [*] Frame 1 content
self._L1.grid(column=0, row=0)
self._E1.grid(column=1, row=0)
self._PREVBUTT.grid(column=2, row=0)
self._NEXTBUTT.grid(column=3, row=0)
# [*] Frame 2 content
self._C1.pack()
# [*] Frame 3 content
self._E2.grid(column=1, row=0)
# [*] Frame 4 content
self._B1.grid(column=0, row=0)
self._B2.grid(column=1, row=0)
# [*] super() / window content
self._F1.pack()
self._F2.pack()
self._F3.pack()
self._F4.pack()
self._B3.pack()
def _find(self, direction: Literal['prev', 'next']):
self.editor.tag_remove(SEL, 0.0, END)
if not self.pattern:
mb.showwarning(self.lang[1], self.lang[348])
return
match direction:
case 'next':
first_index, last_index = self.__n()
case 'prev':
first_index, last_index = self.__p()
case _:
raise ValueError('must be next/prev')
self.editor.mark_set(INSERT, last_index if direction == 'next' else first_index)
self.editor.tag_add(SEL, first_index, last_index)
self.rf()
self.editor.see(INSERT)
def __n(self, ind: str | float = INSERT) -> tuple[str | None, str | None]:
if not self.pattern:
mb.showwarning(self.lang[1], self.lang[366])
return None, None
use_regexp = self.regexp
use_exact_sentences = False if self.regexp is True else True
pattern = self.pattern
first_index = self.editor.search(pattern, ind, END, True, False, use_exact_sentences, use_regexp, self.nocasing)
if not first_index:
mb.showwarning(self.lang[1], self.lang[349])
return None, None
first_index = self.editor.index(first_index)
last_index = first_index
for _ in range(len(self.pattern)):
index_iter = last_index.split('.')
line_index = int(index_iter[1]) + 1
last_index = '.'.join((index_iter[0], str(line_index)))
return first_index, last_index
def __p(self) -> tuple[str | None, str | None]:
if not self.pattern:
mb.showwarning(self.lang[1], self.lang[366])
return None, None
use_regexp = self.regexp
use_exact_sentences = False if self.regexp is True else True
pattern = self.pattern
first_index = self.editor.search(pattern, INSERT, 0.0, False, True, use_exact_sentences, use_regexp, self.nocasing)
if not first_index:
mb.showwarning(self.lang[1], self.lang[350])
return None, None
first_index = self.editor.index(first_index)
last_index = first_index
for _ in range(len(self.pattern)):
index_iter = last_index.split('.')
line_index = int(index_iter[1]) + 1
last_index = '.'.join((index_iter[0], str(line_index)))
return first_index, last_index
def _replace_next(self, replacement: str):
self.__p()
first_index, last_index = self.__n()
if first_index and last_index:
self.editor.replace(first_index, last_index, replacement)
self.editor.mark_set(INSERT, last_index)
self.rf()
self.editor.see(INSERT)
def _replace_all(self, replacement: str):
while True:
first_index, last_index = self.__n(0.0)
if not first_index or not last_index:
self.rf()
break
self.editor.delete(first_index, last_index)
self.editor.insert(first_index, replacement)
def _mark_matches(self):
self.editor.tag_configure('found', background='yellow', foreground='black', font=Font(weight='bold', slant='roman', overstrike=False, underline=True))
self.editor.tag_remove('found', 0.0, END)
last_index = 0.0
if not self.pattern:
mb.showwarning(self.lang[1], self.lang[370])
return
while True:
first_index, last_index = self.__n(last_index)
if not first_index or not last_index:
self.rf()
break
self.editor.tag_add('found', first_index, last_index)
def return_focus(self):
self.editor.focus_set()
rf = return_focus
@property
def lang(self) -> list | tuple:
if isinstance(self._exps, list):
return self._exps.copy()
return self._exps
@property
def regexp(self) -> bool:
return self._regexp
@property
def pattern(self) -> str:
return self._E1.get()
@property
def nocasing(self) -> bool:
return not bool(self._CASING.get())
@property
def replacewith(self) -> str:
return self._E2.get()
class CustomThemeMaker(Frame):
def __init__(self, language_data: list[str], settings: dict[str, Any], dump_func: Callable, master: Toplevel | None = None, **kw) -> None:
super().__init__(master, *kw)
self._lang = language_data.copy()
self._dump_func = dump_func
self._config = settings
self._master = master
master.resizable(False, False)
self._F1 = Frame(master)
self._exit_butt = Button(master, text='Ok', command=lambda:
self.save_and_leave(self._dump_func))
self._L1 = Label(self._F1, text=str(self._lang[360]))
self._L2 = Label(self._F1, text=str(self._lang[361]))
self._L3 = Label(self._F1, text=str(self._lang[362]))
self._L4 = Label(self._F1, text=str(self._lang[363]))
self._L5 = Label(self._F1, text=str(self._lang[364]))
self._L6 = Label(self._F1, text="Dark Mode")
self._DARK = IntVar(self._F1, 1 if str(self._config['theme']['sv']) == 'dark' else 0)
self._B1 = Button(self._F1, text=str(self._config['theme']['color']), command=lambda:
self.askcolor(self._B1, self._C1))
self._B2 = Button(self._F1, text=str(self._config['theme']['fg']), command=lambda:
self.askcolor(self._B2, self._C2))
self._B3 = Button(self._F1, text=str(self._config['theme']['ct']), command=lambda:
self.askcolor(self._B3, self._C3))
self._B4 = Button(self._F1, text=str(self._config['theme']['menu']), command=lambda:
self.askcolor(self._B4, self._C4))
self._B5 = Button(self._F1, text=str(self._config['theme']['mfg']), command=lambda:
self.askcolor(self._B5, self._C5))
self._B6 = Checkbutton(self._F1, variable=self._DARK)
self._C1 = Canvas(self._F1, width=50, height=50)
self._C2 = Canvas(self._F1, width=50, height=50)
self._C3 = Canvas(self._F1, width=50, height=50)
self._C4 = Canvas(self._F1, width=50, height=50)
self._C5 = Canvas(self._F1, width=50, height=50)
a = ('color', 'fg', 'ct', 'menu', 'mfg')
for i, j in enumerate((self._C1, self._C2, self._C3, self._C4, self._C5), 0):
j.create_rectangle(0, 0, 51, 51, fill=str(self._config['theme'][a[i]]))
self._butt_text_rel: dict[Button, str | bytes] = {
self._B1: self._config['theme']['color'],
self._B2: self._config['theme']['fg'],
self._B3: self._config['theme']['ct'],
self._B4: self._config['theme']['menu'],
self._B5: self._config['theme']['mfg']
}
self._L1.grid(column=0, row=0, padx=10, pady=10)
self._L2.grid(column=0, row=1, padx=10, pady=10)
self._L3.grid(column=0, row=2, padx=10, pady=10)
self._L4.grid(column=0, row=3, padx=10, pady=10)
self._L5.grid(column=0, row=4, padx=10, pady=10)
self._L6.grid(column=0, row=5, padx=10, pady=10)
self._B1.grid(column=1, row=0, padx=10, pady=10)
self._B2.grid(column=1, row=1, padx=10, pady=10)
self._B3.grid(column=1, row=2, padx=10, pady=10)
self._B4.grid(column=1, row=3, padx=10, pady=10)
self._B5.grid(column=1, row=4, padx=10, pady=10)
self._B6.grid(column=1, row=5, padx=10, pady=10)
self._C1.grid(column=2, row=0, padx=10, pady=10)
self._C2.grid(column=2, row=1, padx=10, pady=10)
self._C3.grid(column=2, row=2, padx=10, pady=10)
self._C4.grid(column=2, row=3, padx=10, pady=10)
self._C5.grid(column=2, row=4, padx=10, pady=10)
self._F1.pack(pady=5)
self._exit_butt.pack(pady=5)
def askcolor(self, butt_scope: Button, canvas: Canvas):
color: str = picker.askcolor(self._butt_text_rel[butt_scope], title=self._lang[365])[1]
self.focus_set()
if not color:
return
self._butt_text_rel[butt_scope] = color
butt_scope.configure(text=str(color))
canvas.create_rectangle(0, 0, 51, 51, fill=str(color))
def _leave(self):
self._master.destroy()
def _save(self, dump_func: Callable, **kwargs):
dump_func(**kwargs)
def save_and_leave(self, dump_func: Callable, **kw):
self._save(dump_func, bg=self._butt_text_rel[self._B1], fg=self._butt_text_rel[self._B2], ct=self._butt_text_rel[self._B3], mbg=self._butt_text_rel[self._B4], mfg=self._butt_text_rel[self._B5], sv='dark' if self._DARK.get() else 'light', *kw)
self._leave()
def deprecated(message: str | None = None, dep_version: str | float | int | None = None):
def decorator(func):
def wrapper(*args, **kwargs):
new_message = message
if dep_version is None and not message:
new_message = f'{func.__name__} is deprecated'
elif dep_version is not None and not message:
new_message = f'{func.__name__} has been deprecated since {dep_version}'
else:
new_message = message
print(new_message)
return func(*args, **kwargs)
return wrapper
return decorator