Port the RS gettext funcs to use webengine

This commit is contained in:
Kovid Goyal 2019-09-05 12:59:15 +05:30
parent c388500dfb
commit 46f5413b34
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 14 deletions

Binary file not shown.

View File

@ -157,6 +157,17 @@ document.title = 'compiler initialized';
f.write(as_bytes(json.dumps(write_cache))) f.write(as_bytes(json.dumps(write_cache)))
return self.compiler_result[0] return self.compiler_result[0]
def eval(self, js):
self.compiler_result = None
self.errors = []
self.working = True
self.runJavaScript(js, QWebEngineScript.ApplicationWorld, self.compilation_done)
while self.working:
self.spin_loop()
if self.compiler_result is None:
raise CompileFailure('Failed to eval JS with error: ' + '\n'.join(self.errors))
return self.compiler_result
def compilation_done(self, js): def compilation_done(self, js):
self.working = False self.working = False
self.compiler_result = js self.compiler_result = js
@ -342,27 +353,25 @@ def compile_srv():
def create_pot(source_files): def create_pot(source_files):
ctx = compiler() c = compiler()
ctx.g.gettext_options = { gettext_options = json.dumps({
'package_name': __appname__, 'package_name': __appname__,
'package_version': __version__, 'package_version': __version__,
'bugs_address': 'https://bugs.launchpad.net/calibre' 'bugs_address': 'https://bugs.launchpad.net/calibre'
} })
ctx.eval('catalog = {}') c.eval('window.catalog = {{}}; window.gettext_options = {}; 1'.format(gettext_options))
for fname in source_files: for fname in source_files:
with open(fname, 'rb') as f: with open(fname, 'rb') as f:
ctx.g.code = f.read().decode('utf-8') code = f.read().decode('utf-8')
ctx.g.fname = fname fname = fname
ctx.eval('exports.gettext_parse(catalog, code, fname)') c.eval('RapydScript.gettext_parse(window.catalog, {}, {}); 1'.format(*map(json.dumps, (code, fname))))
buf = []
ctx.g.pywrite = buf.append buf = c.eval('ans = []; RapydScript.gettext_output(window.catalog, window.gettext_options, ans.push.bind(ans)); ans;')
ctx.eval('exports.gettext_output(catalog, gettext_options, pywrite)')
return ''.join(buf) return ''.join(buf)
def msgfmt(po_data_as_string): def msgfmt(po_data_as_string):
ctx = compiler() c = compiler()
ctx.g.po_data = po_data_as_string return c.eval('RapydScript.msgfmt({}, {})'.format(
ctx.g.msgfmt_options = {'use_fuzzy': False} json.dumps(po_data_as_string), json.dumps({'use_fuzzy': False})))
return ctx.eval('exports.msgfmt(po_data, msgfmt_options)')
# }}} # }}}