Skip to content

Commit

Permalink
Merge 762beea into f0dad29
Browse files Browse the repository at this point in the history
  • Loading branch information
unhammer committed Feb 28, 2022
2 parents f0dad29 + 762beea commit 8c6aa98
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
9 changes: 6 additions & 3 deletions apertium_apy/handlers/translate_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@
}


async def translate_doc(file_to_translate, fmt, mode_file, unknown_marks=False):
async def translate_doc(file_to_translate, fmt, mode_file, unknown_marks=False, prefs=''):
modes_dir = os.path.dirname(os.path.dirname(mode_file))
mode = os.path.splitext(os.path.basename(mode_file))[0]
if unknown_marks:
cmd = ['apertium', '-f', fmt, '-d', modes_dir, mode]
else:
cmd = ['apertium', '-f', fmt, '-u', '-d', modes_dir, mode]
env = {'AP_SETVAR': prefs}
proc = tornado.process.Subprocess(cmd,
stdin=file_to_translate,
stdout=tornado.process.Subprocess.STREAM)
stdout=tornado.process.Subprocess.STREAM,
env=env)
translated = await proc.stdout.read_until_close()
proc.stdout.close()
# TODO: raises but not caught:
Expand Down Expand Up @@ -109,6 +111,7 @@ def get(self):
t = yield translate_doc(temp_file,
ALLOWED_MIME_TYPES[mtype],
self.pairs['%s-%s' % pair],
self.mark_unknown)
self.mark_unknown,
self.get_argument('prefs', default=''))
self.write(t)
self.finish()
7 changes: 5 additions & 2 deletions apertium_apy/handlers/translate_webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ def get(self):
if pair is None:
return
self.note_pair_usage(pair)
prefs = self.get_argument('prefs', default='')
mode_path = self.pairs['%s-%s' % pair]
url = self.get_argument('url')
if not url.startswith('http'):
url = 'http://' + url
got304 = False
cached = self.get_cached(pair, url)
if prefs:
logging.warn("Web translation with prefs doesn't work with caching yet")
request = httpclient.HTTPRequest(url=url,
# TODO: tweak timeouts:
connect_timeout=20.0,
Expand Down Expand Up @@ -178,7 +181,7 @@ def get(self):
self.send_error(503, explanation="Couldn't decode (or detect charset/encoding of) {}".format(url))
return
before = self.log_before_translation()
translated = yield translation.translate_html_mark_headings(to_translate, mode_path)
translated = yield translation.translate_html_mark_headings(to_translate, mode_path, prefs)
self.log_after_translation(before, len(to_translate))
self.set_cached(pair, url, translated, to_translate)
self.send_response({
Expand All @@ -191,6 +194,6 @@ def get(self):
retranslate = self.retranslate_cache(pair, url, cached)
if got304 and retranslate is not None:
logging.info('Retranslating {}'.format(url))
translated = yield translation.translate_html_mark_headings(retranslate, mode_path)
translated = yield translation.translate_html_mark_headings(retranslate, mode_path, prefs)
logging.info('Done retranslating {}'.format(url))
self.set_cached(pair, url, translated, retranslate)
9 changes: 5 additions & 4 deletions apertium_apy/utils/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,18 @@ def start_pipeline_from_modefile(mode_file, fmt, unknown_marks=False):
return start_pipeline([cmd])


async def translate_modefile_bytes(to_translate_bytes, fmt, mode_file, unknown_marks=False):
async def translate_modefile_bytes(to_translate_bytes, fmt, mode_file, unknown_marks=False, prefs=''):
proc_in, proc_out = start_pipeline_from_modefile(mode_file, fmt, unknown_marks)
assert proc_in == proc_out
await proc_in.stdin.write(bytes(format_prefs(prefs), 'utf-8'))
await proc_in.stdin.write(to_translate_bytes)
proc_in.stdin.close()
translated_bytes = await proc_out.stdout.read_until_close()
proc_in.stdout.close()
return translated_bytes
return strip_prefs(translated_bytes)


@gen.coroutine
def translate_html_mark_headings(to_translate, mode_file, unknown_marks=False):
translated = yield translate_modefile_bytes(bytes(to_translate, 'utf-8'), 'html', mode_file, unknown_marks)
def translate_html_mark_headings(to_translate, mode_file, unknown_marks=False, prefs=''):
translated = yield translate_modefile_bytes(bytes(to_translate, 'utf-8'), 'html', mode_file, unknown_marks, prefs)
return translated.decode('utf-8')

0 comments on commit 8c6aa98

Please sign in to comment.