mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add command runners
This commit is contained in:
parent
1e443bdcb4
commit
0db2817328
14
setup.cfg
14
setup.cfg
@ -12,20 +12,6 @@ upload = upload --sign --identity=36580288
|
|||||||
[build_sphinx]
|
[build_sphinx]
|
||||||
warning-is-error = 1
|
warning-is-error = 1
|
||||||
|
|
||||||
[extract_messages]
|
|
||||||
mapping_file = babel.cfg
|
|
||||||
output_file = sphinx/locale/sphinx.pot
|
|
||||||
keywords = _ __ l_ lazy_gettext
|
|
||||||
|
|
||||||
[update_catalog]
|
|
||||||
input_file = sphinx/locale/sphinx.pot
|
|
||||||
domain = sphinx
|
|
||||||
output_dir = sphinx/locale/
|
|
||||||
|
|
||||||
[compile_catalog]
|
|
||||||
domain = sphinx
|
|
||||||
directory = sphinx/locale/
|
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
max-line-length = 95
|
max-line-length = 95
|
||||||
ignore = E116,E241,E251,E741,W504,I101
|
ignore = E116,E241,E251,E741,W504,I101
|
||||||
|
@ -1,29 +1,32 @@
|
|||||||
from io import StringIO
|
"""Run babel for translations.
|
||||||
from json import dump
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
babel_runner.py extract
|
||||||
|
Extract messages from the source code and update the ".pot" template file.
|
||||||
|
|
||||||
|
babel_runner.py update
|
||||||
|
Update all language catalogues in "sphinx/locale/<language>/LC_MESSAGES"
|
||||||
|
with the current messages in the template file.
|
||||||
|
|
||||||
|
babel_runner.py compile
|
||||||
|
Compile the ".po" catalogue files to ".mo" and ".js" files.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from babel.messages.frontend import compile_catalog
|
from babel.messages.frontend import compile_catalog, extract_messages, update_catalog
|
||||||
from babel.messages.pofile import read_po
|
from babel.messages.pofile import read_po
|
||||||
|
|
||||||
# Provide a "compile_catalog" command that also creates the translated
|
import sphinx
|
||||||
# JavaScript files if Babel is available.
|
|
||||||
|
ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", ".."))
|
||||||
|
|
||||||
|
|
||||||
class Tee:
|
class compile_catalog_plusjs(compile_catalog): # NoQA
|
||||||
def __init__(self, stream):
|
|
||||||
self.stream = stream
|
|
||||||
self.buffer = StringIO()
|
|
||||||
|
|
||||||
def write(self, s):
|
|
||||||
self.stream.write(s)
|
|
||||||
self.buffer.write(s)
|
|
||||||
|
|
||||||
def flush(self):
|
|
||||||
self.stream.flush()
|
|
||||||
|
|
||||||
|
|
||||||
class compile_catalog_plusjs(compile_catalog):
|
|
||||||
"""
|
"""
|
||||||
An extended command that writes all message strings that occur in
|
An extended command that writes all message strings that occur in
|
||||||
JavaScript files to a JavaScript file along with the .mo file.
|
JavaScript files to a JavaScript file along with the .mo file.
|
||||||
@ -33,19 +36,12 @@ class compile_catalog_plusjs(compile_catalog):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
if super().run():
|
||||||
sys.stderr = Tee(sys.stderr)
|
print("Compiling failed.", file=sys.stderr)
|
||||||
compile_catalog.run(self)
|
raise SystemExit(2)
|
||||||
finally:
|
|
||||||
if sys.stderr.buffer.getvalue():
|
|
||||||
print("Compiling failed.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if isinstance(self.domain, list):
|
for domain in self.domain:
|
||||||
for domain in self.domain:
|
self._run_domain_js(domain)
|
||||||
self._run_domain_js(domain)
|
|
||||||
else:
|
|
||||||
self._run_domain_js(self.domain)
|
|
||||||
|
|
||||||
def _run_domain_js(self, domain):
|
def _run_domain_js(self, domain):
|
||||||
po_files = []
|
po_files = []
|
||||||
@ -86,7 +82,7 @@ class compile_catalog_plusjs(compile_catalog):
|
|||||||
if catalog.fuzzy and not self.use_fuzzy:
|
if catalog.fuzzy and not self.use_fuzzy:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.log.info('writing JavaScript strings in catalog %r to %r',
|
self.log.info('writing JavaScript strings in catalog %s to %s',
|
||||||
po_file, js_file)
|
po_file, js_file)
|
||||||
|
|
||||||
jscatalog = {}
|
jscatalog = {}
|
||||||
@ -98,12 +94,78 @@ class compile_catalog_plusjs(compile_catalog):
|
|||||||
msgid = msgid[0]
|
msgid = msgid[0]
|
||||||
jscatalog[msgid] = message.string
|
jscatalog[msgid] = message.string
|
||||||
|
|
||||||
with open(js_file, 'wt', encoding='utf8') as outfile:
|
obj = json.dumps({
|
||||||
outfile.write('Documentation.addTranslations(')
|
'messages': jscatalog,
|
||||||
dump({
|
'plural_expr': catalog.plural_expr,
|
||||||
'messages': jscatalog,
|
'locale': f'{catalog.locale!s}'
|
||||||
'plural_expr': catalog.plural_expr,
|
}, sort_keys=True, indent=4)
|
||||||
'locale': str(catalog.locale)
|
with open(js_file, 'w', encoding='utf8') as outfile:
|
||||||
}, outfile, sort_keys=True, indent=4)
|
outfile.write(f'Documentation.addTranslations({obj});')
|
||||||
outfile.write(');')
|
|
||||||
|
|
||||||
|
|
||||||
|
def _get_logger():
|
||||||
|
log = logging.getLogger('babel')
|
||||||
|
log.setLevel(logging.INFO)
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setFormatter(logging.Formatter('%(message)s'))
|
||||||
|
log.addHandler(handler)
|
||||||
|
return log
|
||||||
|
|
||||||
|
|
||||||
|
def run_extract():
|
||||||
|
os.chdir(ROOT)
|
||||||
|
command = extract_messages()
|
||||||
|
command.log = _get_logger()
|
||||||
|
command.initialize_options()
|
||||||
|
|
||||||
|
command.keywords = "_ __ l_ lazy_gettext"
|
||||||
|
command.mapping_file = "babel.cfg"
|
||||||
|
command.output_file = os.path.join("sphinx", "locale", "sphinx.pot")
|
||||||
|
command.project = "Sphinx"
|
||||||
|
command.version = sphinx.__version__
|
||||||
|
command.input_paths = "sphinx"
|
||||||
|
|
||||||
|
command.finalize_options()
|
||||||
|
return command.run()
|
||||||
|
|
||||||
|
|
||||||
|
def run_update():
|
||||||
|
os.chdir(ROOT)
|
||||||
|
command = update_catalog()
|
||||||
|
command.log = _get_logger()
|
||||||
|
command.initialize_options()
|
||||||
|
|
||||||
|
command.domain = "sphinx"
|
||||||
|
command.input_file = os.path.join("sphinx", "locale", "sphinx.pot")
|
||||||
|
command.output_dir = os.path.join("sphinx", "locale")
|
||||||
|
|
||||||
|
command.finalize_options()
|
||||||
|
return command.run()
|
||||||
|
|
||||||
|
|
||||||
|
def run_compile():
|
||||||
|
os.chdir(ROOT)
|
||||||
|
command = compile_catalog_plusjs()
|
||||||
|
command.log = _get_logger()
|
||||||
|
command.initialize_options()
|
||||||
|
|
||||||
|
command.domain = "sphinx"
|
||||||
|
command.directory = os.path.join("sphinx", "locale")
|
||||||
|
|
||||||
|
command.finalize_options()
|
||||||
|
return command.run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
action = sys.argv[1].lower()
|
||||||
|
except IndexError:
|
||||||
|
print(__doc__, file=sys.stderr)
|
||||||
|
raise SystemExit(2)
|
||||||
|
|
||||||
|
if action == "extract":
|
||||||
|
raise SystemExit(run_extract())
|
||||||
|
if action == "update":
|
||||||
|
raise SystemExit(run_update())
|
||||||
|
if action == "compile":
|
||||||
|
raise SystemExit(run_compile())
|
||||||
|
Loading…
Reference in New Issue
Block a user