summaryrefslogtreecommitdiffstats
path: root/utils/babel_runner.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-05 16:20:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-05 16:20:58 +0000
commit5bb0bb4be543fd5eca41673696a62ed80d493591 (patch)
treead2c464f140e86c7f178a6276d7ea4a93e3e6c92 /utils/babel_runner.py
parentAdding upstream version 7.2.6. (diff)
downloadsphinx-upstream/7.3.7.tar.xz
sphinx-upstream/7.3.7.zip
Adding upstream version 7.3.7.upstream/7.3.7
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'utils/babel_runner.py')
-rw-r--r--utils/babel_runner.py96
1 files changed, 58 insertions, 38 deletions
diff --git a/utils/babel_runner.py b/utils/babel_runner.py
index dfb58db..b66425f 100644
--- a/utils/babel_runner.py
+++ b/utils/babel_runner.py
@@ -31,7 +31,7 @@ from babel.messages.pofile import read_po, write_po
from babel.util import pathmatch
from jinja2.ext import babel_extract as extract_jinja2
-ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", ".."))
+ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), '..', '..'))
TEX_DELIMITERS = {
'variable_start_string': '<%=',
'variable_end_string': '%>',
@@ -70,7 +70,7 @@ OPTIONS_MAP = {
KEYWORDS = {**DEFAULT_KEYWORDS, '_': None, '__': None}
-def run_extract():
+def run_extract() -> None:
"""Message extraction function."""
log = _get_logger()
@@ -100,12 +100,15 @@ def run_extract():
options = opt_dict
with open(os.path.join(root, filename), 'rb') as fileobj:
for lineno, message, comments, context in extract(
- method, fileobj, KEYWORDS, options=options,
+ method, fileobj, KEYWORDS, options=options
):
filepath = os.path.join(input_path, relative_name)
catalogue.add(
- message, None, [(filepath, lineno)],
- auto_comments=comments, context=context,
+ message,
+ None,
+ [(filepath, lineno)],
+ auto_comments=comments,
+ context=context,
)
break
@@ -115,9 +118,8 @@ def run_extract():
write_po(outfile, catalogue)
-def run_update():
+def run_update() -> None:
"""Catalog merging command."""
-
log = _get_logger()
domain = 'sphinx'
@@ -138,7 +140,8 @@ def run_update():
catalog.update(template)
tmp_name = os.path.join(
- os.path.dirname(filename), tempfile.gettempprefix() + os.path.basename(filename),
+ os.path.dirname(filename),
+ tempfile.gettempprefix() + os.path.basename(filename),
)
try:
with open(tmp_name, 'wb') as tmpfile:
@@ -150,7 +153,7 @@ def run_update():
os.replace(tmp_name, filename)
-def run_compile():
+def run_compile() -> None:
"""
Catalog compilation command.
@@ -160,11 +163,10 @@ def run_compile():
Unfortunately, babel's setup command isn't built very extensible, so
most of the run() code is duplicated here.
"""
-
log = _get_logger()
directory = os.path.join('sphinx', 'locale')
- total_errors = 0
+ total_errors = {}
for locale in os.listdir(directory):
po_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.po')
@@ -179,10 +181,17 @@ def run_compile():
continue
for message, errors in catalog.check():
+ if locale not in total_errors:
+ total_errors[locale] = 0
for error in errors:
- total_errors += 1
- log.error('error: %s:%d: %s\nerror: in message string: %s',
- po_file, message.lineno, error, message.string)
+ total_errors[locale] += 1
+ log.error(
+ 'error: %s:%d: %s\nerror: in message string: %r',
+ po_file,
+ message.lineno,
+ error,
+ message.string,
+ )
mo_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.mo')
log.info('compiling catalog %s to %s', po_file, mo_file)
@@ -194,26 +203,37 @@ def run_compile():
js_catalogue = {}
for message in catalog:
if any(
- x[0].endswith(('.js', '.js.jinja', '.js_t', '.html'))
- for x in message.locations
+ x[0].endswith(('.js', '.js.jinja', '.js_t', '.html'))
+ for x in message.locations
):
msgid = message.id
if isinstance(msgid, (list, tuple)):
msgid = msgid[0]
js_catalogue[msgid] = message.string
- obj = json.dumps({
- 'messages': js_catalogue,
- 'plural_expr': catalog.plural_expr,
- 'locale': str(catalog.locale),
- }, sort_keys=True, indent=4)
+ obj = json.dumps(
+ {
+ 'messages': js_catalogue,
+ 'plural_expr': catalog.plural_expr,
+ 'locale': str(catalog.locale),
+ },
+ sort_keys=True,
+ indent=4,
+ )
with open(js_file, 'wb') as outfile:
# to ensure lines end with ``\n`` rather than ``\r\n``:
outfile.write(f'Documentation.addTranslations({obj});'.encode())
- if total_errors > 0:
- log.error('%d errors encountered.', total_errors)
- print("Compiling failed.", file=sys.stderr)
+ if 'ta' in total_errors:
+ # Tamil is a known failure.
+ err_count = total_errors.pop('ta')
+ log.error('%d errors encountered in %r locale.', err_count, 'ta')
+
+ if len(total_errors) > 0:
+ for locale, err_count in total_errors.items():
+ log.error('%d errors encountered in %r locale.', err_count, locale)
+ log.error('%d errors encountered.', sum(total_errors.values()))
+ print('Compiling failed.', file=sys.stderr)
raise SystemExit(2)
@@ -234,17 +254,17 @@ if __name__ == '__main__':
raise SystemExit(2) from None
os.chdir(ROOT)
- if action == "extract":
- raise SystemExit(run_extract())
- if action == "update":
- raise SystemExit(run_update())
- if action == "compile":
- raise SystemExit(run_compile())
- if action == "all":
- exit_code = run_extract()
- if exit_code:
- raise SystemExit(exit_code)
- exit_code = run_update()
- if exit_code:
- raise SystemExit(exit_code)
- raise SystemExit(run_compile())
+ if action == 'extract':
+ run_extract()
+ elif action == 'update':
+ run_update()
+ elif action == 'compile':
+ run_compile()
+ elif action == 'all':
+ run_extract()
+ run_update()
+ run_compile()
+ else:
+ msg = f"invalid action: '{action}'"
+ raise ValueError(msg)
+ raise SystemExit