-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
622 lines (537 loc) · 29.3 KB
/
main.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import json
import logging
import time
import math
import re
from functools import cmp_to_key
from pathlib import Path
from deepl import Translator, DeepLException, Formality
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent, SystemExitEvent, PreferencesUpdateEvent, \
PreferencesEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from xdg.BaseDirectory import xdg_data_home
LOGGER = logging.getLogger(__name__)
class DeepLExtension(Extension):
def __init__(self):
super().__init__()
self.subscribe(SystemExitEvent, SystemExitEventListener())
self.subscribe(PreferencesEvent, PreferencesEventListener())
self.subscribe(PreferencesUpdateEvent, PreferencesUpdateEventListener())
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterListener())
data_folder = Path(xdg_data_home) / 'ulauncher-deepl'
if data_folder.is_file():
raise IOError(f'"{str(data_folder)}" is a file.')
if not data_folder.exists():
data_folder.mkdir()
self.data_file = data_folder / 'data.json'
self.data = json.load(self.data_file.open('r')) if self.data_file.exists() else {}
self.translator = None
self.source_languages = None
self.target_languages = None
self.last_source_language_fetch = 0
self.last_target_language_fetch = 0
self.usage = None
self.last_usage_fetch = 0
def get_last_source_languages(self):
if 'last_source_languages' not in self.data:
self.data['last_source_languages'] = []
return self.data['last_source_languages']
def get_last_target_languages(self):
if 'last_target_languages' not in self.data:
self.data['last_target_languages'] = []
return self.data['last_target_languages']
def set_last_source_language(self, lang):
languages = self.get_last_source_languages()
if lang in languages:
languages.remove(lang)
languages.insert(0, lang)
json.dump(self.data, self.data_file.open('w'))
def set_last_target_language(self, lang):
languages = self.get_last_target_languages()
if lang in languages:
languages.remove(lang)
languages.insert(0, lang)
json.dump(self.data, self.data_file.open('w'))
def get_source_languages(self):
current = time.time()
if current - self.last_source_language_fetch >= 3600:
self.source_languages = self.translator.get_source_languages()
self.last_source_language_fetch = current
return self.source_languages
def get_target_languages(self):
current = time.time()
if current - self.last_target_language_fetch >= 3600:
self.target_languages = self.translator.get_target_languages()
self.last_target_language_fetch = current
return self.target_languages
def get_source_language_name(self, lang_code):
for language in self.get_source_languages():
if language.code == lang_code:
return language.name
return None
def get_target_language_name(self, lang_code):
for language in self.get_target_languages():
if language.code == lang_code:
return language.name
return None
def get_usage(self):
current = time.time()
if current - self.last_usage_fetch >= 20:
self.usage = self.translator.get_usage()
self.last_usage_fetch = current
return self.usage
def on_input(self, keyword, arg):
if not self.translator:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='Translator could not be initialized',
description='Please validate that your API key is correct.',
highlightable=False,
on_enter=HideWindowAction())
])
try:
usage = self.get_usage().character
except DeepLException as error:
LOGGER.error(error)
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='An error occured',
description='Please validate that your API key is correct.',
highlightable=False,
on_enter=HideWindowAction())
])
if usage.limit_exceeded:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='DeepL API Usage exceeded',
highlightable=False,
on_enter=HideWindowAction())
])
source_lang = self.preferences['source_language'].upper()
if source_lang == 'AUTO':
source_lang = None
select_source_lang = False
elif source_lang == 'SELECT':
source_lang = None
select_source_lang = True
elif self.get_source_language_name(source_lang):
select_source_lang = False
else:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Source language "{source_lang}" not found (from preferences).',
description='Press Alt+Enter for a list of source languages.',
highlightable=False,
on_enter=HideWindowAction(),
on_alt_enter=ExtensionCustomAction({
'keyword': keyword,
'action': 'source_languages'}, keep_app_open=True))
])
target_lang = self.preferences['target_language'].upper()
if target_lang == 'EN':
target_lang = 'EN-US'
if target_lang == 'SELECT':
select_target_lang = True
elif self.get_target_language_name(target_lang):
select_target_lang = False
else:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Target language "{target_lang}" not found (from preferences).',
description='Press Alt+Enter for a list of target languages.',
highlightable=False,
on_enter=HideWindowAction(),
on_alt_enter=ExtensionCustomAction({
'keyword': keyword,
'action': 'target_languages'}, keep_app_open=True))
])
usage_str = f'Usage: {usage.count}/{usage.limit} ({round(usage.count / usage.limit * 10000) / 100}%)'
if not arg:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='Enter text...',
description=usage_str,
highlightable=False,
on_enter=DoNothingAction())
])
original_arg = arg
match = re.search('^(?P<source>select|auto|[a-zA-Z]{2})?:(?P<target>select|[a-zA-Z]{2}(-[a-zA-Z]{2})?)?(?P<space> )?', arg,
re.IGNORECASE)
if match:
if match['space']:
if match['source']:
source_lang = match['source'].upper()
if source_lang == 'AUTO':
source_lang = None
select_source_lang = False
elif source_lang == 'SELECT':
source_lang = None
select_source_lang = True
elif self.get_source_language_name(source_lang):
select_source_lang = False
else:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Source language "{source_lang}" not found.',
description='Press Alt+Enter for a list of source languages.',
highlightable=False,
on_enter=HideWindowAction(),
on_alt_enter=ExtensionCustomAction({
'keyword': keyword,
'action': 'source_languages'}, keep_app_open=True))
])
if match['target']:
target_lang = match['target'].upper()
if target_lang == 'EN':
target_lang = 'EN-US'
if target_lang == 'SELECT':
select_target_lang = True
elif self.get_target_language_name(target_lang):
select_target_lang = False
else:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Target language "{target_lang}" not found.',
description='Press Alt+Enter for a list of target languages.',
highlightable=False,
on_enter=HideWindowAction(),
on_alt_enter=ExtensionCustomAction({
'keyword': keyword,
'action': 'target_languages'}, keep_app_open=True))
])
arg = arg[match.end():].strip()
if not arg:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='Enter text...',
description=usage_str,
highlightable=False,
on_enter=DoNothingAction())
])
if not match['space']:
arg = original_arg
if not select_target_lang:
data = {
'keyword': keyword,
'text': arg,
'original_text': original_arg,
'target_lang': target_lang
}
if not select_source_lang:
data['source_lang'] = source_lang
data['translate_directly'] = False
return self.on_enter(data)
translate_data = {
'keyword': keyword,
'text': arg
}
if not select_source_lang:
translate_data['source_lang'] = source_lang
items = [
ExtensionResultItem(
icon='images/icon.png',
name='Translate text',
description='Alt+Enter to skip source language and detect it instead.' if select_source_lang else '',
highlightable=False,
on_enter=ExtensionCustomAction(translate_data, keep_app_open=True),
on_alt_enter=ExtensionCustomAction({'keyword': keyword,
'text': arg,
'original_text': original_arg,
'source_lang': None},
keep_app_open=True) if select_source_lang else None)
]
last_target_languages = self.get_last_target_languages()
quick_access_languages = str(self.preferences['quick_access_languages'])
for i in range(min(len(last_target_languages),
int(quick_access_languages) if quick_access_languages.isnumeric() else 3)):
lang = last_target_languages[i]
items.append(ExtensionResultItem(
icon='images/icon.png',
name=f'Translate to {self.get_target_language_name(lang)}',
description='Alt+Enter to choose source language.' if select_source_lang else '',
highlightable=False,
on_enter=ExtensionCustomAction({'keyword': keyword,
'text': arg,
'original_text': original_arg,
'source_lang': source_lang,
'target_lang': lang}, keep_app_open=True),
on_alt_enter=ExtensionCustomAction({'keyword': keyword,
'text': arg,
'original_text': original_arg,
'target_lang': lang},
keep_app_open=True) if select_source_lang else None))
return RenderResultListAction(items)
def on_enter(self, data):
if 'reset' in data:
return self.on_input(data['keyword'], data['reset'])
languages_per_page_str = str(self.preferences['languages_per_page'])
languages_per_page = int(languages_per_page_str) if languages_per_page_str.isnumeric() else 10
if 'action' in data:
if data['action'] == 'source_languages':
languages = self.get_source_languages()
elif data['action'] == 'target_languages':
languages = self.get_target_languages()
else:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='An error occured',
highlightable=False,
on_enter=HideWindowAction())
])
page = data['page'] if 'page' in data else 1
total_page_count = math.ceil((len(languages) + 1) / languages_per_page)
items = []
if total_page_count > 1:
page_descriptions = []
next_page, previous_page = page < total_page_count, page > 1
if next_page:
page_descriptions.append('Enter for next page')
if previous_page:
page_descriptions.append('Alt+Enter for previous page')
page_data = data.copy()
page_data['page'] = page + 1
page_alt_data = data.copy()
page_alt_data['page'] = page - 1
items.append(
ExtensionResultItem(
icon='images/icon.png',
name=f'Page {page}/{total_page_count}',
description=', '.join(page_descriptions),
highlightable=False,
on_enter=ExtensionCustomAction(page_data, keep_app_open=True) if next_page else None,
on_alt_enter=ExtensionCustomAction(page_alt_data,
keep_app_open=True) if previous_page else None)
)
for i in range((page - 1) * languages_per_page, min(len(languages), page * languages_per_page)):
language = languages[i]
items.append(ExtensionResultItem(icon='images/icon.png',
name=f'{language.code}: {language.name}',
highlightable=False,
on_enter=HideWindowAction()))
return RenderResultListAction(items)
def compare(last_languages, lang1, lang2):
l1, l2 = lang1.code, lang2.code
e1, e2 = l1 in last_languages, l2 in last_languages
if not e1 and not e2:
return 0
if e1 and not e2:
return -1
if not e1 and e2:
return 1
return last_languages.index(l1) - last_languages.index(l2)
last_source_languages = self.get_last_source_languages()
last_target_languages = self.get_last_target_languages()
if 'source_lang' not in data:
last_target = last_target_languages[0] if last_target_languages else None
last_target_name = self.get_target_language_name(last_target) if last_target else None
languages = sorted(self.get_source_languages(),
key=cmp_to_key(lambda lang1, lang2: compare(last_source_languages, lang1, lang2)))
description = f'Alt+Enter to translate to {last_target_name}.' if last_target and 'target_lang' not in \
data else ''
page = data['page'] if 'page' in data else 1
total_page_count = math.ceil((len(languages) + 1) / languages_per_page)
items = []
if total_page_count > 1:
page_descriptions = []
next_page, previous_page = page < total_page_count, page > 1
if next_page:
page_descriptions.append('Enter for next page')
if previous_page:
page_descriptions.append('Alt+Enter for previous page')
page_data = data.copy()
page_data['page'] = page + 1
page_alt_data = data.copy()
page_alt_data['page'] = page - 1
items.append(
ExtensionResultItem(
icon='images/icon.png',
name=f'Page {page}/{total_page_count}',
description=', '.join(page_descriptions),
highlightable=False,
on_enter=ExtensionCustomAction(page_data, keep_app_open=True) if next_page else None,
on_alt_enter=ExtensionCustomAction(page_alt_data,
keep_app_open=True) if previous_page else None)
)
if page == 1:
detect_data = data.copy()
if 'page' in detect_data:
del detect_data['page']
detect_data['source_lang'] = None
detect_alt_data = detect_data.copy()
if last_target and 'target_lang' not in data:
detect_alt_data['target_lang'] = last_target
items.append(
ExtensionResultItem(icon='images/icon.png',
name='Detect language',
description=description,
highlightable=False,
on_enter=ExtensionCustomAction(detect_data, keep_app_open=True),
on_alt_enter=ExtensionCustomAction(detect_alt_data, keep_app_open=True))
)
for i in range((page - 1) * languages_per_page - (1 if page != 1 else 0),
min(len(languages), page * languages_per_page - 1)):
language = languages[i]
new_data = data.copy()
if 'page' in new_data:
del new_data['page']
new_data['source_lang'] = language.code
new_alt_data = new_data.copy()
if last_target and 'target_lang' not in data:
new_alt_data['target_lang'] = last_target
items.append(ExtensionResultItem(icon='images/icon.png',
name=f'Translate from {language.name}',
description=description,
highlightable=False,
on_enter=ExtensionCustomAction(new_data, keep_app_open=True),
on_alt_enter=ExtensionCustomAction(new_alt_data, keep_app_open=True)))
return RenderResultListAction(items)
elif data['source_lang']:
self.set_last_source_language(data['source_lang'])
if 'target_lang' not in data:
languages = sorted(self.get_target_languages(),
key=cmp_to_key(lambda lang1, lang2: compare(last_target_languages, lang1, lang2)))
page = data['page'] if 'page' in data else 1
total_page_count = math.ceil((len(languages) + 1) / languages_per_page)
items = []
if total_page_count > 1:
page_descriptions = []
next_page, previous_page = page < total_page_count, page > 1
if next_page:
page_descriptions.append('Enter for next page')
if previous_page:
page_descriptions.append('Alt+Enter for previous page')
page_data = data.copy()
page_data['page'] = page + 1
page_alt_data = data.copy()
page_alt_data['page'] = page - 1
items.append(
ExtensionResultItem(
icon='images/icon.png',
name=f'Page {page}/{total_page_count}',
description=', '.join(page_descriptions),
highlightable=False,
on_enter=ExtensionCustomAction(page_data, keep_app_open=True) if next_page else None,
on_alt_enter=ExtensionCustomAction(page_alt_data,
keep_app_open=True) if previous_page else None)
)
for i in range((page - 1) * languages_per_page, min(len(languages), page * languages_per_page)):
language = languages[i]
new_data = data.copy()
if 'page' in new_data:
del new_data['page']
new_data['target_lang'] = language.code
items.append(ExtensionResultItem(icon='images/icon.png',
name=f'Translate to {language.name}',
highlightable=False,
on_enter=ExtensionCustomAction(new_data, keep_app_open=True)))
return RenderResultListAction(items)
if data['target_lang']:
self.set_last_target_language(data['target_lang'])
if 'translate_directly' in data and not data['translate_directly']:
new_data = data.copy()
new_data['translate_directly'] = True
source_lang = data['source_lang']
target_lang = data['target_lang']
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Translate ' +
(f'from {self.get_source_language_name(source_lang)} ' if source_lang else '')
+ f'to {self.get_target_language_name(target_lang)}',
highlightable=False,
on_enter=ExtensionCustomAction(new_data, keep_app_open=True))
])
try:
supports_formality = False
for language in self.get_target_languages():
if language.code == data['target_lang']:
supports_formality = language.supports_formality
if supports_formality:
try:
formality = Formality[self.preferences['formality'].upper()]
except KeyError:
formality = Formality.DEFAULT
else:
formality = Formality.DEFAULT
result = self.translator.translate_text(data['text'].strip(),
source_lang=data['source_lang'],
target_lang=data['target_lang'],
formality=formality)
source_lang, target_lang = data['source_lang'] or result.detected_source_lang, data['target_lang']
split_result = str(self.preferences['split_result'])
shown_text = None
if split_result.isnumeric():
split_result_int = int(split_result)
if split_result_int is not 0:
parts, current = [], None
for word in result.text.split(' '):
new_current = f'{current} {word}' if current else word
if not current or len(new_current) <= split_result_int:
current = new_current
else:
parts.append(current)
current = word
parts.append(current)
shown_text = '\n'.join(parts)
if not shown_text:
shown_text = result.text
keyword = data['keyword']
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name=f'Translation: {self.get_source_language_name(source_lang)} \u27A1 '
f'{self.get_target_language_name(target_lang)}',
description=shown_text,
highlightable=False,
on_enter=CopyToClipboardAction(result.text),
on_alt_enter=ExtensionCustomAction({'reset': result.text,
'keyword': keyword}, keep_app_open=True)
if data['text'] == result.text else SetUserQueryAction(
f'{keyword} {target_lang.lower().split("-")[0]}:select {result.text}')),
ExtensionResultItem(icon='images/icon.png',
name='Actions',
description='Enter on the result item to copy the result.'
'\nAlt+Enter on the result item to translate the result into another '
'language.'
'\nEnter on this item to translate again and reset the input text.'
'\nAlt+Enter on this item to translate again and keep the input text.',
highlightable=False,
on_enter=SetUserQueryAction(f'{keyword} '),
on_alt_enter=ExtensionCustomAction({'reset': data['original_text'],
'keyword': keyword},
keep_app_open=True))
])
except DeepLException as error:
LOGGER.error(error)
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='An error occured',
highlightable=False,
on_enter=HideWindowAction())
])
class SystemExitEventListener(EventListener):
def on_event(self, event: SystemExitEvent, extension: DeepLExtension):
json.dump(extension.data, extension.data_file.open('w'))
class PreferencesEventListener(EventListener):
def on_event(self, event: PreferencesEvent, extension: DeepLExtension):
api_key = event.preferences['api_key']
extension.translator = Translator(api_key) if api_key else None
class PreferencesUpdateEventListener(EventListener):
def on_event(self, event: PreferencesUpdateEvent, extension: DeepLExtension):
if event.id == 'api_key':
extension.translator = Translator(event.new_value) if event.new_value else None
class KeywordQueryEventListener(EventListener):
def on_event(self, event: KeywordQueryEvent, extension: DeepLExtension):
return extension.on_input(event.get_keyword(), event.get_argument())
class ItemEnterListener(EventListener):
def on_event(self, event: ItemEnterEvent, extension: DeepLExtension):
return extension.on_enter(event.get_data())
if __name__ == '__main__':
DeepLExtension().run()