Skip to content

Commit

Permalink
Update translation suggestion to use libsoup async
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmardojai committed Feb 20, 2022
1 parent 6156f9c commit ae71d18
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 63 deletions.
5 changes: 4 additions & 1 deletion dialect/translators/basetrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def format_detection(self, text):
def get_detect(self, data):
return None

def suggest(self, suggestion):
def format_suggestion(self, text, src, dest, suggestion):
pass

def get_suggestion(self, data):
pass

def format_translation(self, text, src, dest):
Expand Down
53 changes: 14 additions & 39 deletions dialect/translators/libretrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ class Translator(TranslatorBase):
settings_path = '/frontend/settings'
api_test_path = '/translate'

_data = {
'q': None,
'source': None,
'target': None,
'api_key': api_key,
}

def __init__(self, callback, base_url=None, api_key='', **kwargs):
def on_loaded():
callback(self.langs_success and self.settings_success, self.error)
Expand Down Expand Up @@ -185,19 +178,20 @@ def format_detection(self, text):
def get_detect(self, data):
return Detected(data[0]['language'], data[0]['confidence'])

def suggest(self, suggestion):
try:
data = self._data
data['s'] = suggestion
if self.api_key:
data['api_key'] = self.api_key
suggest_response_data = self._post(self.suggest_url, data)
error = suggest_response_data.get('error', None)
if error:
logging.error(error)
return suggest_response_data.get('success', False)
except Exception as exc:
raise TranslationError(exc) from exc
def format_suggestion(self, text, src, dest, suggestion):
data = {
'q': text,
'source': src,
'target': dest,
's': suggestion,
}
if self.api_key:
data['api_key'] = self.api_key

return (data, {})

def get_suggestion(self, data):
return data.get('success', False)

def format_translation(self, text, src, dest):
data = {
Expand All @@ -219,22 +213,3 @@ def get_translation(self, data):
'dest-pronunciation': None,
},
)

def _get(self, url):
message = Soup.Message.new('GET', url)
response = self.session.send_and_read(message, None)
response_data = json.loads(
response.get_data()
) if response else {}
return response_data

def _post(self, url, data):
message = Soup.Message.new('POST', url)
data_bytes = json.dumps(data).encode('utf-8')
data_glib_bytes = GLib.Bytes.new(data_bytes)
message.set_request_body_from_bytes('application/json', data_glib_bytes)
response = self.session.send_and_read(message, None)
response_data = json.loads(
response.get_data()
) if response else {}
return response_data
49 changes: 26 additions & 23 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,19 @@ def ui_suggest_ok(self, _action, _param):
self.dest_buffer.get_end_iter(),
True
)
threading.Thread(
target=self._suggest,
args=(dest_text,),
daemon=True
).start()

(data, headers) = self.translator.format_suggestion(
self.translator.history[self.current_history]['Text'][0],
self.translator.history[self.current_history]['Languages'][0],
self.translator.history[self.current_history]['Languages'][1],
dest_text
)
message = Session.create_post_message(
self.translator.suggest_url,
data, headers
)
Session.get().send_and_read_async(message, 0, None, self.on_suggest_response)

self.before_suggest = None

def ui_suggest_cancel(self, _action, _param):
Expand All @@ -800,26 +808,21 @@ def ui_suggest_cancel(self, _action, _param):
self.before_suggest = None
self.dest_text.set_editable(False)

def _suggest(self, text):
success = self.translator.suggest(text)
GLib.idle_add(
self.dest_toolbar_stack.set_visible_child_name,
'default'
)
def on_suggest_response(self, session, result):
success = False
self.dest_toolbar_stack.set_visible_child_name('default')
try:
data = Session.get_response(session, result)
success = self.translator.get_suggestion(data)
except Exception as exc:
logging.error(exc)

if success:
GLib.idle_add(
self.send_notification,
_('New translation has been suggested!')
)
self.send_notification(_('New translation has been suggested!'))
else:
GLib.idle_add(
self.send_notification,
_('Suggestion failed.')
)
GLib.idle_add(
self.dest_text.set_editable,
False
)
self.send_notification(_('Suggestion failed.'))

self.dest_text.set_editable(False)

def ui_src_voice(self, _action, _param):
src_text = self.src_buffer.get_text(
Expand Down

0 comments on commit ae71d18

Please sign in to comment.