-
Notifications
You must be signed in to change notification settings - Fork 0
/
utranslate.py
189 lines (175 loc) · 6.83 KB
/
utranslate.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
import logging
import uuid
from far3.far3cffi import ffi, ffic
from far3.plugin import PluginBase
from far3.far3dialog import Dialog
from far3 import fardialogbuilder as dlgb
log = logging.getLogger(__name__)
import debugpy
import shlex
import optparse
from far3packages import googletranslate
log = logging.getLogger(__name__)
class OptionParser(optparse.OptionParser):
def error(self, msg):
log.error('utranslate: {}'.format(msg))
raise ValueError('bad option')
class Plugin(PluginBase):
openFrom = ["PLUGINSMENU", "COMMANDLINE", "EDITOR", "VIEWER"]
name = 'utranslate'
flags = ffic.PF_NONE
title = "Python Translate"
author = "Grzegorz Makarewicz <mak@trisoft.com.pl>"
description = title
guid = uuid.UUID('{A01E7520-F997-46BD-AB47-4F0AD76436FB}')
guid1 = uuid.UUID('{F0432EB3-A7A7-4FAA-A1B0-046EDF328A03}')
def __init__(self, info):
super().__init__(info)
def CommandLine(self, line):
cmd = line.split(' ', 1)
if cmd[0] == 'utranslate':
if len(cmd) > 1 and cmd[1] == 'debug':
debugpy.breakpoint()
return self.Dialog()
if len(cmd) == 1:
return
line = cmd[1]
cmd = cmd[0]
if cmd == 'spell':
args = shlex.split(line)
parser = OptionParser()
parser.add_option('--lang', action="store", dest="lang", default='en')
parser.add_option('--text', action="store", dest="text", default='en')
try:
opts, args = parser.parse_args(args)
except ValueError:
return
log.debug('spell: opts={} args={}'.format(opts, args))
try:
t = googletranslate.get_translate(opts.text, opts.lang, opts.lang)
except:
log.exception('spell')
return
log.debug('spell result: {}'.format(t))
s = t['revise']
elif cmd == 'translate':
args = shlex.split(line)
parser = optparse.OptionParser()
parser.add_option('--from', action="store", dest="lfrom", default='en')
parser.add_option('--to', action="store", dest="lto", default='en')
parser.add_option('--text', action="store", dest="text", default='')
try:
opts, args = parser.parse_args(args)
except ValueError:
return
log.debug('translate: opts={} args={}'.format(opts, args))
try:
t = googletranslate.get_translate(opts.text, opts.lfrom, opts.lto)
except:
log.exception('translate')
return
log.debug('translate result: {}'.format(t))
s = t['result'][0]
else:
log.error('bad command:{} line={}'.format(cmd, line))
return
self.info.FSF.CopyToClipboard(ffic.FCT_STREAM, self.s2f(s))
def Dialog(self):
@ffi.callback("FARWINDOWPROC")
def DialogProc(hDlg, Msg, Param1, Param2):
if Msg == ffic.DN_INITDIALOG:
try:
data = self.s2f('0'*120)
nb = self.info.FSF.PasteFromClipboard(ffic.FCT_STREAM, data, 120)
s = self.f2s(data)
except:
log.exception('DN_INITDIALOG.1')
s = "And do beautiful things"
try:
dlg.SetText(dlg.ID_text, s)
dlg.SetCheck(dlg.ID_spell, 1)
dlg.SetText(dlg.ID_from, "en")
dlg.SetText(dlg.ID_to, "pl")
dlg.SetFocus(dlg.ID_text)
except:
log.exception('DN_INITDIALOG.2')
elif Msg == ffic.DN_BTNCLICK:
if dlg.ID_perform == Param1:
text = dlg.GetText(dlg.ID_text)
spell = dlg.GetCheck(dlg.ID_spell)
translate = dlg.GetCheck(dlg.ID_translate)
lfrom = dlg.GetText(dlg.ID_from)
lto = dlg.GetText(dlg.ID_to)
log.debug('DN_BTNCLICK(spell={}, translate={} nfrom={} nto={} text={})'.format(spell, translate, lfrom, lto, text))
try:
if spell:
t = googletranslate.get_translate(text, lfrom, lfrom)
s = t['revise']
if s is None:
s = t['result'][0]
else:
t = googletranslate.get_translate(text, lfrom, lto)
s = t['result'][0]
log.debug('translate result: {} full={}'.format(s, t))
self.info.FSF.CopyToClipboard(ffic.FCT_STREAM, self.s2f(s))
except:
log.exception('DN_INITDIALOG.3')
s = 'connection error'
dlg.SetText(dlg.ID_result, s)
return self.info.DefDlgProc(hDlg, Msg, Param1, Param2)
dlg = Dialog(self)
b = dlgb.DialogBuilder(
dlg,
DialogProc,
"Python utranslate",
"helptopic",
0,
dlgb.VSizer(
dlgb.HSizer(
dlgb.TEXT("Text:"),
dlgb.EDIT("text", 60, 120)
),
dlgb.HSizer(
dlgb.TEXT("Operation:"),
dlgb.RADIOBUTTON('spell', "Spell", True, flags=ffic.DIF_GROUP),
dlgb.Spacer(),
dlgb.RADIOBUTTON('translate', "Translate"),
),
dlgb.HSizer(
dlgb.TEXT("From:"),
dlgb.EDIT("from", 4, 4)
),
dlgb.HSizer(
dlgb.TEXT("To:"),
dlgb.EDIT("to", 4, 4)
),
dlgb.HLine(),
dlgb.HSizer(
dlgb.TEXT("Result:"),
dlgb.EDIT("result", 60, 120)
),
dlgb.HLine(),
dlgb.HSizer(
dlgb.BUTTON('perform', "Perform", flags=ffic.DIF_CENTERGROUP|ffic.DIF_DEFAULTBUTTON|ffic.DIF_BTNNOCLOSE),
dlgb.BUTTON('close', "Close", flags=ffic.DIF_CENTERGROUP),
),
),
)
b.build(
-1,
-1
)
self.info.DialogRun(dlg.hDlg)
dlg.close()
def OpenW(self, info):
self.Message(
"Python Translate/Spellcheck",
"utranslate",
"From shell:",
" py:translate --from=en --to=pl --text='And do beautiful things'",
" py:spell --lang=en --text='And do beautifull things'",
" py:utrnaslate",
"",
"Result is in clipboard :)",
)
return None