Convert `package_dir to _StrPath`

This commit is contained in:
Adam Turner 2025-02-11 02:55:13 +00:00
parent 1f693448b0
commit 41277fc7bd
15 changed files with 24 additions and 26 deletions

View File

@ -11,6 +11,8 @@ __display_version__ = __version__ # used for command line version
import os
import warnings
from sphinx.util._pathlib import _StrPath
# by default, all DeprecationWarning under sphinx package will be emit.
# Users can avoid this by using environment variable: PYTHONWARNINGS=
if 'PYTHONWARNINGS' not in os.environ:
@ -34,7 +36,7 @@ warnings.filterwarnings(
#: Before version 1.2, check the string ``sphinx.__version__``.
version_info = (8, 2, 0, 'beta', 0)
package_dir = os.path.abspath(os.path.dirname(__file__))
package_dir = _StrPath(__file__).resolve().parent
_in_development = True
if _in_development:

View File

@ -361,7 +361,7 @@ class Sphinx:
locale_dirs: list[_StrPath | None] = list(repo.locale_dirs)
locale_dirs += [None]
locale_dirs += [_StrPath(package_dir, 'locale')]
locale_dirs += [package_dir / 'locale']
self.translator, has_translation = locale.init(
locale_dirs, self.config.language

View File

@ -3,7 +3,6 @@
from __future__ import annotations
import html
from pathlib import Path
from typing import TYPE_CHECKING
from sphinx import package_dir
@ -148,14 +147,14 @@ class ChangesBuilder(Builder):
'theme_' + key: val for (key, val) in self.theme.get_options({}).items()
}
copy_asset_file(
Path(package_dir, 'themes', 'default', 'static', 'default.css.jinja'),
package_dir.joinpath('themes', 'default', 'static', 'default.css.jinja'),
self.outdir,
context=themectx,
renderer=self.templates,
force=True,
)
copy_asset_file(
Path(package_dir, 'themes', 'basic', 'static', 'basic.css'),
package_dir.joinpath('themes', 'basic', 'static', 'basic.css'),
self.outdir / 'basic.css',
force=True,
)

View File

@ -17,7 +17,6 @@ from sphinx.builders import _epub_base
from sphinx.config import ENUM
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util._pathlib import _StrPath
from sphinx.util.fileutil import copy_asset_file
from sphinx.util.osutil import make_filename
@ -85,7 +84,7 @@ class Epub3Builder(_epub_base.EpubBuilder):
epilog = __('The ePub file is in %(outdir)s.')
supported_remote_images = False
template_dir = _StrPath(package_dir, 'templates', 'epub3')
template_dir = package_dir.joinpath('templates', 'epub3')
doctype = DOCTYPE
html_tag = HTML_TAG
use_meta_charset = True

View File

@ -39,7 +39,7 @@ if TYPE_CHECKING:
from sphinx.util.i18n import CatalogInfo
from sphinx.util.typing import ExtensionMetadata
DEFAULT_TEMPLATE_PATH = Path(package_dir, 'templates', 'gettext')
DEFAULT_TEMPLATE_PATH = package_dir.joinpath('templates', 'gettext')
logger = logging.getLogger(__name__)

View File

@ -199,8 +199,8 @@ class StandaloneHTMLBuilder(Builder):
if js_file.is_file():
return js_file
js_file = Path(
package_dir, 'locale', self.config.language, 'LC_MESSAGES', 'sphinx.js'
js_file = package_dir.joinpath(
'locale', self.config.language, 'LC_MESSAGES', 'sphinx.js'
)
if js_file.is_file():
return js_file

View File

@ -442,7 +442,7 @@ class LaTeXBuilder(Builder):
'xindy_lang_option': xindy_lang_option,
'xindy_cyrillic': xindy_cyrillic,
}
static_dir_name = Path(package_dir, 'texinputs')
static_dir_name = package_dir / 'texinputs'
for filename in Path(static_dir_name).iterdir():
if not filename.name.startswith('.'):
copy_asset_file(
@ -454,7 +454,7 @@ class LaTeXBuilder(Builder):
# use pre-1.6.x Makefile for make latexpdf on Windows
if os.name == 'nt':
static_dir_name = Path(package_dir, 'texinputs_win')
static_dir_name = package_dir / 'texinputs_win'
copy_asset_file(
static_dir_name / 'Makefile.jinja',
self.outdir,
@ -522,7 +522,7 @@ class LaTeXBuilder(Builder):
context['addtocaptions'] = r'\addto\captions%s' % self.babel.get_language()
copy_asset_file(
Path(package_dir, 'templates', 'latex', 'sphinxmessages.sty.jinja'),
package_dir.joinpath('templates', 'latex', 'sphinxmessages.sty.jinja'),
self.outdir,
context=context,
renderer=LaTeXRenderer(),

View File

@ -2,10 +2,8 @@
from __future__ import annotations
import os
import os.path
import warnings
from pathlib import Path
from typing import TYPE_CHECKING
from docutils import nodes
@ -36,7 +34,7 @@ if TYPE_CHECKING:
from sphinx.util.typing import ExtensionMetadata
logger = logging.getLogger(__name__)
template_dir = Path(package_dir, 'templates', 'texinfo')
template_dir = package_dir.joinpath('templates', 'texinfo')
class TexinfoBuilder(Builder):

View File

@ -33,7 +33,7 @@ else:
PY_SUFFIXES = ('.py', '.pyx', *EXTENSION_SUFFIXES)
template_dir = Path(package_dir, 'templates', 'apidoc')
template_dir = package_dir.joinpath('templates', 'apidoc')
def is_initpy(filename: str | Path) -> bool:

View File

@ -132,7 +132,9 @@ class AutosummaryRenderer:
msg = 'Expected a Sphinx application object!'
raise TypeError(msg)
system_templates_path = [Path(package_dir, 'ext', 'autosummary', 'templates')]
system_templates_path = [
package_dir.joinpath('ext', 'autosummary', 'templates')
]
loader = SphinxTemplateLoader(
app.srcdir, app.config.templates_path, system_templates_path
)

View File

@ -8,7 +8,6 @@ import subprocess
import xml.etree.ElementTree as ET
from hashlib import sha1
from itertools import chain
from pathlib import Path
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
from urllib.parse import urlsplit, urlunsplit
@ -506,7 +505,7 @@ def man_visit_graphviz(self: ManualPageTranslator, node: graphviz) -> None:
def on_config_inited(_app: Sphinx, config: Config) -> None:
css_path = Path(sphinx.package_dir, 'templates', 'graphviz', 'graphviz.css')
css_path = sphinx.package_dir.joinpath('templates', 'graphviz', 'graphviz.css')
config.html_static_path.append(str(css_path))

View File

@ -40,7 +40,7 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
templates_path = Path(package_dir, 'templates', 'imgmath')
templates_path = package_dir.joinpath('templates', 'imgmath')
class MathExtError(SphinxError):

View File

@ -10,7 +10,6 @@ import os
import pickle
import re
from importlib import import_module
from pathlib import Path
from typing import TYPE_CHECKING
from docutils import nodes
@ -39,8 +38,8 @@ if TYPE_CHECKING:
def write(self, s: _T_contra, /) -> object: ...
_NON_MINIFIED_JS_PATH = Path(package_dir, 'search', 'non-minified-js')
_MINIFIED_JS_PATH = Path(package_dir, 'search', 'minified-js')
_NON_MINIFIED_JS_PATH = package_dir.joinpath('search', 'non-minified-js')
_MINIFIED_JS_PATH = package_dir.joinpath('search', 'minified-js')
class SearchLanguage:

View File

@ -159,7 +159,7 @@ class HTMLThemeFactory:
def _load_builtin_themes(self) -> None:
"""Load built-in themes."""
themes = self._find_themes(Path(package_dir, 'themes'))
themes = self._find_themes(package_dir / 'themes')
for name, theme in themes.items():
self._themes[name] = _StrPath(theme)

View File

@ -22,7 +22,7 @@ if TYPE_CHECKING:
from jinja2.environment import Environment
_TEMPLATES_PATH = Path(package_dir, 'templates')
_TEMPLATES_PATH = package_dir / 'templates'
_LATEX_TEMPLATES_PATH = _TEMPLATES_PATH / 'latex'