1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
from __future__ import annotations
import sys
import warnings
from distutils.errors import DistutilsOptionError
from os import path
from babel.messages import frontend as babel
from pkg_resources import EntryPoint
warnings.warn(
"mkdocs.commands.babel is never used in MkDocs and will be removed soon.", DeprecationWarning
)
DEFAULT_MAPPING_FILE = path.normpath(
path.join(path.abspath(path.dirname(__file__)), '../themes/babel.cfg')
)
class ThemeMixin:
def get_theme_dir(self):
"""Validate theme option and return path to theme's root obtained from entry point."""
entry_points = EntryPoint.parse_map(self.distribution.entry_points, self.distribution)
if 'mkdocs.themes' not in entry_points:
raise DistutilsOptionError("no mkdocs.themes are defined in entry_points")
if self.theme is None and len(entry_points['mkdocs.themes']) == 1:
# Default to the only theme defined in entry_points as none specified.
self.theme = tuple(entry_points['mkdocs.themes'].keys())[0]
if self.theme not in entry_points['mkdocs.themes']:
raise DistutilsOptionError("you must specify a valid theme name to work on")
theme = entry_points['mkdocs.themes'][self.theme]
return path.dirname(theme.resolve().__file__)
class compile_catalog(babel.compile_catalog, ThemeMixin):
user_options = babel.compile_catalog.user_options + [
("theme=", "t", "theme name to work on"),
]
def run(self):
# Possible bug in Babel - produces unused return value:
# https://github.com/python-babel/babel/blob/v2.10.3/babel/messages/frontend.py#L194
if super().run():
sys.exit(1)
def initialize_options(self):
super().initialize_options()
self.theme = None
def finalize_options(self):
if not self.directory:
theme_dir = self.get_theme_dir()
self.directory = f"{theme_dir}/locales"
super().finalize_options()
class extract_messages(babel.extract_messages, ThemeMixin):
user_options = babel.extract_messages.user_options + [
("domain=", "d", "domains of the POT output file"),
("theme=", "t", "theme name to work on"),
]
def initialize_options(self):
super().initialize_options()
self.domain = "messages"
self.theme = None
def finalize_options(self):
if not self.version:
version = self.distribution.get_version()
self.version = ".".join(i for i in version.split(".") if "dev" not in i)
if not self.mapping_file:
self.mapping_file = DEFAULT_MAPPING_FILE
if not self.input_paths or not self.output_file:
theme_dir = self.get_theme_dir()
if not self.input_paths:
self.input_paths = theme_dir
if not self.output_file:
self.output_file = f"{theme_dir}/{self.domain}.pot"
super().finalize_options()
class init_catalog(babel.init_catalog, ThemeMixin):
user_options = babel.init_catalog.user_options + [
("theme=", "t", "theme name to work on"),
]
def initialize_options(self):
super().initialize_options()
self.theme = None
def finalize_options(self):
if not self.input_file or not self.output_dir:
theme_dir = self.get_theme_dir()
if not self.input_file:
self.input_file = f"{theme_dir}/{self.domain}.pot"
if not self.output_dir:
self.output_dir = f"{theme_dir}/locales"
super().finalize_options()
class update_catalog(babel.update_catalog, ThemeMixin):
user_options = babel.update_catalog.user_options + [
("theme=", "t", "theme name to work on"),
]
def initialize_options(self):
super().initialize_options()
self.theme = None
def finalize_options(self):
if not self.input_file or not self.output_dir:
theme_dir = self.get_theme_dir()
if not self.input_file:
self.input_file = f"{theme_dir}/{self.domain}.pot"
if not self.output_dir:
self.output_dir = f"{theme_dir}/locales"
super().finalize_options()
|