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 cc78b00
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 56 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, suggestion):
pass

def get_suggestion(self, data):
pass

def format_translation(self, text, src, dest):
Expand Down
43 changes: 11 additions & 32 deletions dialect/translators/libretrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,17 @@ 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, suggestion):
data = {
'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 +217,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
44 changes: 21 additions & 23 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,14 @@ 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(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 +803,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_nam('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 cc78b00

Please sign in to comment.