summaryrefslogtreecommitdiff
path: root/sphinx/util/i18n.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2016-11-09 11:45:12 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2016-11-16 12:06:21 +0900
commit8cfb281b05653a32f480799cb39d4c7532d27f05 (patch)
tree95d99d97c2cd6f921644c7adbe071705491edf09 /sphinx/util/i18n.py
parentdb732ac0b839a028a868a180550bb4f55d6e9b4b (diff)
downloadsphinx-git-8cfb281b05653a32f480799cb39d4c7532d27f05.tar.gz
Add type-check annotations to sphinx.util
Diffstat (limited to 'sphinx/util/i18n.py')
-rw-r--r--sphinx/util/i18n.py30
1 files changed, 23 insertions, 7 deletions
diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py
index 112353d47..efbbb75f7 100644
--- a/sphinx/util/i18n.py
+++ b/sphinx/util/i18n.py
@@ -22,9 +22,12 @@ from babel.messages.pofile import read_po
from babel.messages.mofile import write_mo
from sphinx.errors import SphinxError
-from sphinx.util.osutil import walk
-from sphinx.util import SEP
+from sphinx.util.osutil import SEP, walk
+if False:
+ # For type annotation
+ from typing import Callable # NOQA
+ from sphinx.environment import BuildEnvironment # NOQA
LocaleFileInfoBase = namedtuple('CatalogInfo', 'base_dir,domain,charset')
@@ -33,32 +36,39 @@ class CatalogInfo(LocaleFileInfoBase):
@property
def po_file(self):
+ # type: () -> unicode
return self.domain + '.po'
@property
def mo_file(self):
+ # type: () -> unicode
return self.domain + '.mo'
@property
def po_path(self):
+ # type: () -> unicode
return path.join(self.base_dir, self.po_file)
@property
def mo_path(self):
+ # type: () -> unicode
return path.join(self.base_dir, self.mo_file)
def is_outdated(self):
+ # type: () -> bool
return (
not path.exists(self.mo_path) or
path.getmtime(self.mo_path) < path.getmtime(self.po_path))
def write_mo(self, locale):
+ # type: (unicode) -> None
with io.open(self.po_path, 'rt', encoding=self.charset) as po:
with io.open(self.mo_path, 'wb') as mo:
write_mo(mo, read_po(po, locale))
def find_catalog(docname, compaction):
+ # type: (unicode, bool) -> unicode
if compaction:
ret = docname.split(SEP, 1)[0]
else:
@@ -68,18 +78,20 @@ def find_catalog(docname, compaction):
def find_catalog_files(docname, srcdir, locale_dirs, lang, compaction):
+ # type: (unicode, unicode, List[unicode], unicode, bool) -> List[unicode]
if not(lang and locale_dirs):
return []
domain = find_catalog(docname, compaction)
- files = [gettext.find(domain, path.join(srcdir, dir_), [lang])
- for dir_ in locale_dirs]
- files = [path.relpath(f, srcdir) for f in files if f]
- return files
+ files = [gettext.find(domain, path.join(srcdir, dir_), [lang]) # type: ignore
+ for dir_ in locale_dirs] # type: ignore
+ files = [path.relpath(f, srcdir) for f in files if f] # type: ignore
+ return files # type: ignore
def find_catalog_source_files(locale_dirs, locale, domains=None, gettext_compact=False,
charset='utf-8', force_all=False):
+ # type: (List[unicode], unicode, List[unicode], bool, unicode, bool) -> Set[CatalogInfo]
"""
:param list locale_dirs:
list of path as `['locale_dir1', 'locale_dir2', ...]` to find
@@ -99,7 +111,7 @@ def find_catalog_source_files(locale_dirs, locale, domains=None, gettext_compact
if not locale:
return [] # locale is not specified
- catalogs = set()
+ catalogs = set() # type: Set[CatalogInfo]
for locale_dir in locale_dirs:
if not locale_dir:
continue # skip system locale directory
@@ -158,6 +170,7 @@ date_format_mappings = {
def babel_format_date(date, format, locale, warn=None, formatter=babel.dates.format_date):
+ # type: (datetime, unicode, unicode, Callable, Callable) -> unicode
if locale is None:
locale = 'en'
@@ -180,6 +193,7 @@ def babel_format_date(date, format, locale, warn=None, formatter=babel.dates.for
def format_date(format, date=None, language=None, warn=None):
+ # type: (str, datetime, unicode, Callable) -> unicode
if format is None:
format = 'medium'
@@ -226,6 +240,7 @@ def format_date(format, date=None, language=None, warn=None):
def get_image_filename_for_language(filename, env):
+ # type: (unicode, BuildEnvironment) -> unicode
if not env.config.language:
return filename
@@ -245,6 +260,7 @@ def get_image_filename_for_language(filename, env):
def search_image_for_language(filename, env):
+ # type: (unicode, BuildEnvironment) -> unicode
if not env.config.language:
return filename