2010-05-29 11:14:42 -05:00
|
|
|
"""
|
2011-01-08 10:32:32 -06:00
|
|
|
sphinx.builders.gettext
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
2010-05-29 11:14:42 -05:00
|
|
|
|
|
|
|
The MessageCatalogBuilder class.
|
|
|
|
|
2022-01-01 03:45:03 -06:00
|
|
|
:copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
|
2010-05-29 11:14:42 -05:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
2010-08-15 05:26:37 -05:00
|
|
|
from codecs import open
|
2020-11-11 05:00:27 -06:00
|
|
|
from collections import OrderedDict, defaultdict
|
|
|
|
from datetime import datetime, timedelta, tzinfo
|
|
|
|
from os import getenv, path, walk
|
2018-01-27 10:52:16 -06:00
|
|
|
from time import time
|
2020-11-11 11:52:50 -06:00
|
|
|
from typing import Any, DefaultDict, Dict, Generator, Iterable, List, Set, Tuple, Union
|
2013-03-10 08:07:31 -05:00
|
|
|
from uuid import uuid4
|
2010-06-02 02:35:33 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
from docutils import nodes
|
|
|
|
from docutils.nodes import Element
|
|
|
|
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx import addnodes, package_dir
|
2019-06-08 05:33:35 -05:00
|
|
|
from sphinx.application import Sphinx
|
2010-05-29 11:14:42 -05:00
|
|
|
from sphinx.builders import Builder
|
2018-02-24 01:48:12 -06:00
|
|
|
from sphinx.domains.python import pairindextypes
|
2018-10-10 08:49:39 -05:00
|
|
|
from sphinx.errors import ThemeError
|
2018-02-25 07:16:09 -06:00
|
|
|
from sphinx.locale import __
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx.util import logging, split_index_msg, status_iterator
|
2018-01-27 10:52:16 -06:00
|
|
|
from sphinx.util.console import bold # type: ignore
|
2019-06-08 05:33:35 -05:00
|
|
|
from sphinx.util.i18n import CatalogInfo, docname_to_domain
|
2013-10-04 03:12:20 -05:00
|
|
|
from sphinx.util.nodes import extract_messages, traverse_translatable_index
|
2020-11-11 05:00:27 -06:00
|
|
|
from sphinx.util.osutil import canon_path, ensuredir, relpath
|
2018-01-27 10:52:16 -06:00
|
|
|
from sphinx.util.tags import Tags
|
2019-11-07 09:36:57 -06:00
|
|
|
from sphinx.util.template import SphinxRenderer
|
2010-05-29 11:14:42 -05:00
|
|
|
|
2016-12-21 22:41:56 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
|
|
|
|
class Message:
|
|
|
|
"""An entry of translatable message."""
|
|
|
|
def __init__(self, text: str, locations: List[Tuple[str, int]], uuids: List[str]):
|
|
|
|
self.text = text
|
|
|
|
self.locations = locations
|
|
|
|
self.uuids = uuids
|
2010-06-02 02:45:13 -05:00
|
|
|
|
2010-08-21 12:34:05 -05:00
|
|
|
|
2018-09-11 08:48:35 -05:00
|
|
|
class Catalog:
|
2011-07-06 01:31:40 -05:00
|
|
|
"""Catalog of translatable messages."""
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def __init__(self) -> None:
|
2021-03-13 01:30:59 -06:00
|
|
|
self.messages: List[str] = [] # retain insertion order, a la OrderedDict
|
|
|
|
|
|
|
|
# msgid -> file, line, uid
|
|
|
|
self.metadata: Dict[str, List[Tuple[str, int, str]]] = OrderedDict()
|
2011-07-06 01:31:40 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def add(self, msg: str, origin: Union[Element, "MsgOrigin"]) -> None:
|
2014-03-01 23:40:29 -06:00
|
|
|
if not hasattr(origin, 'uid'):
|
2014-03-02 01:40:48 -06:00
|
|
|
# Nodes that are replicated like todo don't have a uid,
|
|
|
|
# however i18n is also unnecessary.
|
2014-03-01 23:40:29 -06:00
|
|
|
return
|
2011-07-06 01:25:25 -05:00
|
|
|
if msg not in self.metadata: # faster lookup in hash
|
|
|
|
self.messages.append(msg)
|
|
|
|
self.metadata[msg] = []
|
2018-12-03 10:52:07 -06:00
|
|
|
self.metadata[msg].append((origin.source, origin.line, origin.uid)) # type: ignore
|
2011-07-06 01:25:25 -05:00
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
def __iter__(self) -> Generator[Message, None, None]:
|
|
|
|
for message in self.messages:
|
|
|
|
positions = [(source, line) for source, line, uuid in self.metadata[message]]
|
|
|
|
uuids = [uuid for source, line, uuid in self.metadata[message]]
|
|
|
|
yield Message(message, positions, uuids)
|
|
|
|
|
2011-07-06 01:25:25 -05:00
|
|
|
|
2018-09-11 08:48:35 -05:00
|
|
|
class MsgOrigin:
|
2013-03-10 08:07:31 -05:00
|
|
|
"""
|
|
|
|
Origin holder for Catalog message origin.
|
|
|
|
"""
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def __init__(self, source: str, line: int) -> None:
|
2013-03-10 08:07:31 -05:00
|
|
|
self.source = source
|
|
|
|
self.line = line
|
|
|
|
self.uid = uuid4().hex
|
|
|
|
|
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
class GettextRenderer(SphinxRenderer):
|
2020-01-12 23:25:51 -06:00
|
|
|
def __init__(self, template_path: str = None, outdir: str = None) -> None:
|
|
|
|
self.outdir = outdir
|
2019-11-07 09:36:57 -06:00
|
|
|
if template_path is None:
|
|
|
|
template_path = path.join(package_dir, 'templates', 'gettext')
|
|
|
|
super().__init__(template_path)
|
|
|
|
|
|
|
|
def escape(s: str) -> str:
|
|
|
|
s = s.replace('\\', r'\\')
|
|
|
|
s = s.replace('"', r'\"')
|
|
|
|
return s.replace('\n', '\\n"\n"')
|
|
|
|
|
|
|
|
# use texescape as escape filter
|
|
|
|
self.env.filters['e'] = escape
|
|
|
|
self.env.filters['escape'] = escape
|
|
|
|
|
2020-01-12 23:25:51 -06:00
|
|
|
def render(self, filename: str, context: Dict) -> str:
|
|
|
|
def _relpath(s: str) -> str:
|
|
|
|
return canon_path(relpath(s, self.outdir))
|
|
|
|
|
|
|
|
context['relpath'] = _relpath
|
|
|
|
return super().render(filename, context)
|
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
|
2016-08-20 06:44:17 -05:00
|
|
|
class I18nTags(Tags):
|
|
|
|
"""Dummy tags module for I18nBuilder.
|
|
|
|
|
|
|
|
To translate all text inside of only nodes, this class
|
|
|
|
always returns True value even if no tags are defined.
|
|
|
|
"""
|
2019-06-08 05:33:35 -05:00
|
|
|
def eval_condition(self, condition: Any) -> bool:
|
2016-08-20 06:44:17 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2011-01-07 17:06:02 -06:00
|
|
|
class I18nBuilder(Builder):
|
2010-06-04 11:38:16 -05:00
|
|
|
"""
|
2010-08-21 12:34:05 -05:00
|
|
|
General i18n builder.
|
2010-06-04 11:38:16 -05:00
|
|
|
"""
|
2010-08-15 05:13:04 -05:00
|
|
|
name = 'i18n'
|
2011-01-08 10:32:32 -06:00
|
|
|
versioning_method = 'text'
|
2021-03-13 01:30:59 -06:00
|
|
|
versioning_compare: bool = None # be set by `gettext_uuid`
|
2017-03-19 18:54:36 -05:00
|
|
|
use_message_catalog = False
|
2014-10-05 07:50:44 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def init(self) -> None:
|
2018-12-15 10:25:47 -06:00
|
|
|
super().init()
|
2017-03-19 09:16:37 -05:00
|
|
|
self.env.set_versioning_method(self.versioning_method,
|
|
|
|
self.env.config.gettext_uuid)
|
2016-08-20 06:44:17 -05:00
|
|
|
self.tags = I18nTags()
|
2021-03-13 01:30:59 -06:00
|
|
|
self.catalogs: DefaultDict[str, Catalog] = defaultdict(Catalog)
|
2010-05-29 11:14:42 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def get_target_uri(self, docname: str, typ: str = None) -> str:
|
2010-05-29 11:14:42 -05:00
|
|
|
return ''
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def get_outdated_docs(self) -> Set[str]:
|
2010-05-29 11:14:42 -05:00
|
|
|
return self.env.found_docs
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def prepare_writing(self, docnames: Set[str]) -> None:
|
2010-05-29 11:14:42 -05:00
|
|
|
return
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def compile_catalogs(self, catalogs: Set[CatalogInfo], message: str) -> None:
|
2014-08-03 02:22:08 -05:00
|
|
|
return
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def write_doc(self, docname: str, doctree: nodes.document) -> None:
|
2019-02-27 00:19:56 -06:00
|
|
|
catalog = self.catalogs[docname_to_domain(docname, self.config.gettext_compact)]
|
2010-08-15 05:53:00 -05:00
|
|
|
|
2022-01-01 10:06:24 -06:00
|
|
|
for toctree in self.env.tocs[docname].findall(addnodes.toctree):
|
2019-03-17 01:03:13 -05:00
|
|
|
for node, msg in extract_messages(toctree):
|
|
|
|
node.uid = '' # type: ignore # Hack UUID model
|
|
|
|
catalog.add(msg, node)
|
|
|
|
|
2010-08-15 05:53:00 -05:00
|
|
|
for node, msg in extract_messages(doctree):
|
2011-07-06 01:25:25 -05:00
|
|
|
catalog.add(msg, node)
|
2010-08-15 05:53:00 -05:00
|
|
|
|
2015-02-21 08:48:18 -06:00
|
|
|
if 'index' in self.env.config.gettext_additional_targets:
|
2014-09-28 07:19:54 -05:00
|
|
|
# Extract translatable messages from index entries.
|
|
|
|
for node, entries in traverse_translatable_index(doctree):
|
2022-01-10 03:21:53 -06:00
|
|
|
for typ, msg, _tid, _main, _key in entries:
|
2014-09-28 07:19:54 -05:00
|
|
|
for m in split_index_msg(typ, msg):
|
|
|
|
if typ == 'pair' and m in pairindextypes.values():
|
|
|
|
# avoid built-in translated message was incorporated
|
|
|
|
# in 'sphinx.util.nodes.process_index_entry'
|
|
|
|
continue
|
|
|
|
catalog.add(m, node)
|
2013-01-05 08:38:21 -06:00
|
|
|
|
2010-08-21 12:34:05 -05:00
|
|
|
|
2013-09-16 01:28:47 -05:00
|
|
|
# determine tzoffset once to remain unaffected by DST change during build
|
2013-06-14 22:59:49 -05:00
|
|
|
timestamp = time()
|
2013-09-16 01:28:47 -05:00
|
|
|
tzdelta = datetime.fromtimestamp(timestamp) - \
|
|
|
|
datetime.utcfromtimestamp(timestamp)
|
2016-05-03 02:53:54 -05:00
|
|
|
# set timestamp from SOURCE_DATE_EPOCH if set
|
|
|
|
# see https://reproducible-builds.org/specs/source-date-epoch/
|
2016-04-28 16:36:28 -05:00
|
|
|
source_date_epoch = getenv('SOURCE_DATE_EPOCH')
|
|
|
|
if source_date_epoch is not None:
|
|
|
|
timestamp = float(source_date_epoch)
|
2016-06-19 08:40:42 -05:00
|
|
|
tzdelta = timedelta(0)
|
2015-03-08 10:24:24 -05:00
|
|
|
|
2016-05-02 03:05:25 -05:00
|
|
|
|
2013-06-12 22:54:05 -05:00
|
|
|
class LocalTimeZone(tzinfo):
|
2019-12-30 02:41:00 -06:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2021-12-16 11:01:33 -06:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-06-12 22:54:05 -05:00
|
|
|
self.tzdelta = tzdelta
|
2013-05-28 06:30:57 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def utcoffset(self, dt: datetime) -> timedelta:
|
2013-06-12 22:54:05 -05:00
|
|
|
return self.tzdelta
|
2013-05-28 06:30:57 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def dst(self, dt: datetime) -> timedelta:
|
2013-06-12 22:54:05 -05:00
|
|
|
return timedelta(0)
|
2013-05-28 06:30:57 -05:00
|
|
|
|
2016-11-15 19:58:41 -06:00
|
|
|
|
2013-06-12 22:54:05 -05:00
|
|
|
ltz = LocalTimeZone()
|
2013-05-28 06:30:57 -05:00
|
|
|
|
|
|
|
|
2019-12-29 08:56:30 -06:00
|
|
|
def should_write(filepath: str, new_content: str) -> bool:
|
2017-03-01 23:57:39 -06:00
|
|
|
if not path.exists(filepath):
|
|
|
|
return True
|
2017-04-26 10:17:07 -05:00
|
|
|
try:
|
2018-12-12 10:33:14 -06:00
|
|
|
with open(filepath, encoding='utf-8') as oldpot:
|
2017-04-26 10:17:07 -05:00
|
|
|
old_content = oldpot.read()
|
|
|
|
old_header_index = old_content.index('"POT-Creation-Date:')
|
|
|
|
new_header_index = new_content.index('"POT-Creation-Date:')
|
|
|
|
old_body_index = old_content.index('"PO-Revision-Date:')
|
|
|
|
new_body_index = new_content.index('"PO-Revision-Date:')
|
|
|
|
return ((old_content[:old_header_index] != new_content[:new_header_index]) or
|
|
|
|
(new_content[new_body_index:] != old_content[old_body_index:]))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2017-03-01 23:57:39 -06:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2010-08-21 12:34:05 -05:00
|
|
|
class MessageCatalogBuilder(I18nBuilder):
|
2010-08-15 05:13:04 -05:00
|
|
|
"""
|
|
|
|
Builds gettext-style message catalogs (.pot files).
|
|
|
|
"""
|
|
|
|
name = 'gettext'
|
2018-02-25 07:16:09 -06:00
|
|
|
epilog = __('The message catalogs are in %(outdir)s.')
|
2010-05-29 11:14:42 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def init(self) -> None:
|
2018-12-15 10:25:47 -06:00
|
|
|
super().init()
|
2013-03-10 08:07:31 -05:00
|
|
|
self.create_template_bridge()
|
|
|
|
self.templates.init(self)
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def _collect_templates(self) -> Set[str]:
|
2013-03-10 08:07:31 -05:00
|
|
|
template_files = set()
|
|
|
|
for template_path in self.config.templates_path:
|
|
|
|
tmpl_abs_path = path.join(self.app.srcdir, template_path)
|
2022-01-10 03:21:53 -06:00
|
|
|
for dirpath, _dirs, files in walk(tmpl_abs_path):
|
2013-03-10 08:07:31 -05:00
|
|
|
for fn in files:
|
|
|
|
if fn.endswith('.html'):
|
2015-10-10 08:44:46 -05:00
|
|
|
filename = canon_path(path.join(dirpath, fn))
|
2013-03-10 08:07:31 -05:00
|
|
|
template_files.add(filename)
|
|
|
|
return template_files
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def _extract_from_template(self) -> None:
|
2018-02-16 23:15:27 -06:00
|
|
|
files = list(self._collect_templates())
|
|
|
|
files.sort()
|
2019-01-19 06:20:14 -06:00
|
|
|
logger.info(bold(__('building [%s]: ') % self.name), nonl=True)
|
2018-02-25 07:16:09 -06:00
|
|
|
logger.info(__('targets for %d template files'), len(files))
|
2013-03-10 08:07:31 -05:00
|
|
|
|
|
|
|
extract_translations = self.templates.environment.extract_translations
|
|
|
|
|
2018-12-12 10:33:14 -06:00
|
|
|
for template in status_iterator(files, __('reading templates... '), "purple",
|
2017-01-04 22:41:17 -06:00
|
|
|
len(files), self.app.verbosity):
|
2018-10-10 08:49:39 -05:00
|
|
|
try:
|
2018-12-12 10:33:14 -06:00
|
|
|
with open(template, encoding='utf-8') as f:
|
2018-10-10 08:49:39 -05:00
|
|
|
context = f.read()
|
2022-01-10 03:21:53 -06:00
|
|
|
for line, _meth, msg in extract_translations(context):
|
2018-10-10 08:49:39 -05:00
|
|
|
origin = MsgOrigin(template, line)
|
|
|
|
self.catalogs['sphinx'].add(msg, origin)
|
|
|
|
except Exception as exc:
|
2020-06-13 16:46:19 -05:00
|
|
|
raise ThemeError('%s: %r' % (template, exc)) from exc
|
2013-03-10 08:07:31 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def build(self, docnames: Iterable[str], summary: str = None, method: str = 'update') -> None: # NOQA
|
2013-03-10 08:07:31 -05:00
|
|
|
self._extract_from_template()
|
2018-12-15 10:25:47 -06:00
|
|
|
super().build(docnames, summary, method)
|
2013-03-10 08:07:31 -05:00
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def finish(self) -> None:
|
2018-12-15 10:25:47 -06:00
|
|
|
super().finish()
|
2019-11-07 09:36:57 -06:00
|
|
|
context = {
|
2018-11-13 20:43:24 -06:00
|
|
|
'version': self.config.version,
|
|
|
|
'copyright': self.config.copyright,
|
|
|
|
'project': self.config.project,
|
2019-12-14 01:27:11 -06:00
|
|
|
'last_translator': self.config.gettext_last_translator,
|
|
|
|
'language_team': self.config.gettext_language_team,
|
2019-11-07 09:36:57 -06:00
|
|
|
'ctime': datetime.fromtimestamp(timestamp, ltz).strftime('%Y-%m-%d %H:%M%z'),
|
|
|
|
'display_location': self.config.gettext_location,
|
|
|
|
'display_uuid': self.config.gettext_uuid,
|
2018-11-13 20:43:24 -06:00
|
|
|
}
|
2018-12-12 10:33:14 -06:00
|
|
|
for textdomain, catalog in status_iterator(self.catalogs.items(),
|
2018-02-25 07:16:09 -06:00
|
|
|
__("writing message catalogs... "),
|
2017-01-04 22:41:17 -06:00
|
|
|
"darkgreen", len(self.catalogs),
|
|
|
|
self.app.verbosity,
|
|
|
|
lambda textdomain__: textdomain__[0]):
|
2011-10-03 06:20:53 -05:00
|
|
|
# noop if config.gettext_compact is set
|
|
|
|
ensuredir(path.join(self.outdir, path.dirname(textdomain)))
|
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
context['messages'] = list(catalog)
|
2020-01-12 23:25:51 -06:00
|
|
|
content = GettextRenderer(outdir=self.outdir).render('message.pot_t', context)
|
2017-03-03 03:43:22 -06:00
|
|
|
|
2019-11-07 09:36:57 -06:00
|
|
|
pofn = path.join(self.outdir, textdomain + '.pot')
|
2017-03-03 03:43:22 -06:00
|
|
|
if should_write(pofn, content):
|
2018-12-12 10:33:14 -06:00
|
|
|
with open(pofn, 'w', encoding='utf-8') as pofile:
|
2017-03-03 03:43:22 -06:00
|
|
|
pofile.write(content)
|
2016-07-03 20:46:27 -05:00
|
|
|
|
|
|
|
|
2019-06-08 05:33:35 -05:00
|
|
|
def setup(app: Sphinx) -> Dict[str, Any]:
|
2016-07-03 20:46:27 -05:00
|
|
|
app.add_builder(MessageCatalogBuilder)
|
|
|
|
|
2020-12-19 11:18:56 -06:00
|
|
|
app.add_config_value('gettext_compact', True, 'gettext', {bool, str})
|
2016-07-03 20:46:27 -05:00
|
|
|
app.add_config_value('gettext_location', True, 'gettext')
|
|
|
|
app.add_config_value('gettext_uuid', False, 'gettext')
|
|
|
|
app.add_config_value('gettext_auto_build', True, 'env')
|
|
|
|
app.add_config_value('gettext_additional_targets', [], 'env')
|
2019-12-14 01:27:11 -06:00
|
|
|
app.add_config_value('gettext_last_translator', 'FULL NAME <EMAIL@ADDRESS>', 'gettext')
|
|
|
|
app.add_config_value('gettext_language_team', 'LANGUAGE <LL@li.org>', 'gettext')
|
2016-12-12 06:22:16 -06:00
|
|
|
|
|
|
|
return {
|
|
|
|
'version': 'builtin',
|
|
|
|
'parallel_read_safe': True,
|
|
|
|
'parallel_write_safe': True,
|
|
|
|
}
|