Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de15d61

Browse files
committed
Use pathlib in sphinx.project
1 parent 932e5c5 commit de15d61

15 files changed

Lines changed: 51 additions & 43 deletions

File tree

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
('py:class', 'IndexEntry'), # sphinx.domains.IndexEntry
185185
('py:class', 'Node'), # sphinx.domains.Domain
186186
('py:class', 'NullTranslations'), # gettext.NullTranslations
187+
('py:class', 'Path'), # sphinx.environment.BuildEnvironment.doc2path
187188
('py:class', 'RoleFunction'), # sphinx.domains.Domain
188189
('py:class', 'RSTState'), # sphinx.utils.parsing.nested_parse_to_nodes
189190
('py:class', 'Theme'), # sphinx.application.TemplateBridge

sphinx/builders/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ def read_doc(self, docname: str, *, _cache: bool = True) -> None:
519519
if path.isfile(docutilsconf):
520520
self.env.note_dependency(docutilsconf)
521521

522-
filename = self.env.doc2path(docname)
522+
filename = str(self.env.doc2path(docname))
523523
filetype = get_filetype(self.app.config.source_suffix, filename)
524524
publisher = self.app.registry.get_publisher(self.app, filetype)
525525
self.env.temp_data['_parser'] = publisher.parser

sphinx/builders/changes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def hl(no: int, line: str) -> str:
134134
with open(targetfn, 'w', encoding='utf-8') as f:
135135
text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines))
136136
ctx = {
137-
'filename': self.env.doc2path(docname, False),
137+
'filename': str(self.env.doc2path(docname, False)),
138138
'text': text,
139139
}
140140
f.write(self.templates.render('changes/rstsource.html', ctx))

sphinx/builders/html/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def get_doc_context(self, docname: str, body: str, metatags: str) -> dict[str, A
611611
title = self.render_partial(title_node)['title'] if title_node else ''
612612

613613
# Suffix for the document
614-
source_suffix = self.env.doc2path(docname, False)[len(docname):]
614+
source_suffix = str(self.env.doc2path(docname, False))[len(docname):]
615615

616616
# the name for the copied source
617617
if self.config.html_copy_source:
@@ -976,7 +976,7 @@ def load_indexer(self, docnames: Iterable[str]) -> None:
976976
def index_page(self, pagename: str, doctree: nodes.document, title: str) -> None:
977977
# only index pages with title
978978
if self.indexer is not None and title:
979-
filename = self.env.doc2path(pagename, base=False)
979+
filename = str(self.env.doc2path(pagename, base=False))
980980
metadata = self.env.metadata.get(pagename, {})
981981
if 'no-search' in metadata or 'nosearch' in metadata:
982982
self.indexer.feed(pagename, filename, '', new_document(''))

sphinx/builders/linkcheck.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
if TYPE_CHECKING:
3030
from collections.abc import Callable, Iterator
31+
from pathlib import Path
3132
from typing import Any
3233

3334
from requests import Response
@@ -82,7 +83,7 @@ def process_result(self, result: CheckResult) -> None:
8283
filename = self.env.doc2path(result.docname, False)
8384

8485
linkstat: dict[str, str | int] = {
85-
'filename': filename, 'lineno': result.lineno,
86+
'filename': str(filename), 'lineno': result.lineno,
8687
'status': result.status, 'code': result.code,
8788
'uri': result.uri, 'info': result.message,
8889
}
@@ -149,7 +150,7 @@ def write_linkstat(self, data: dict[str, str | int]) -> None:
149150
self.json_outfile.write(json.dumps(data))
150151
self.json_outfile.write('\n')
151152

152-
def write_entry(self, what: str, docname: str, filename: str, line: int,
153+
def write_entry(self, what: str, docname: str, filename: Path, line: int,
153154
uri: str) -> None:
154155
self.txt_outfile.write(f'{filename}:{line}: [{what}] {uri}\n')
155156

@@ -225,7 +226,7 @@ def _add_uri(self, uri: str, node: nodes.Element) -> None:
225226
class Hyperlink(NamedTuple):
226227
uri: str
227228
docname: str
228-
docpath: str
229+
docpath: Path
229230
lineno: int
230231

231232

sphinx/directives/other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def parse_content(self, toctree: addnodes.toctree) -> list[Node]:
145145
continue
146146

147147
if docname not in frozen_all_docnames:
148-
if excluded(self.env.doc2path(docname, False)):
148+
if excluded(str(self.env.doc2path(docname, False))):
149149
message = __('toctree contains reference to excluded document %r')
150150
subtype = 'excluded'
151151
else:

sphinx/environment/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,13 @@ def path2doc(self, filename: str | os.PathLike[str]) -> str | None:
413413
"""
414414
return self.project.path2doc(filename)
415415

416-
def doc2path(self, docname: str, base: bool = True) -> str:
416+
def doc2path(self, docname: str, base: bool = True) -> Path:
417417
"""Return the filename for the document name.
418418
419419
If *base* is True, return absolute path under self.srcdir.
420420
If *base* is False, return relative path to self.srcdir.
421421
"""
422-
return self.project.doc2path(docname, base)
422+
return self.project.doc2path(docname, absolute=base)
423423

424424
def relfn2path(self, filename: str, docname: str | None = None) -> tuple[str, str]:
425425
"""Return paths to a file referenced from a document, relative to
@@ -628,7 +628,7 @@ def get_doctree(self, docname: str) -> nodes.document:
628628

629629
doctree = pickle.loads(serialised)
630630
doctree.settings.env = self
631-
doctree.reporter = LoggingReporter(self.doc2path(docname))
631+
doctree.reporter = LoggingReporter(str(self.doc2path(docname)))
632632
return doctree
633633

634634
@functools.cached_property
@@ -650,7 +650,7 @@ def get_and_resolve_doctree(
650650
try:
651651
doctree = self._write_doc_doctree_cache.pop(docname)
652652
doctree.settings.env = self
653-
doctree.reporter = LoggingReporter(self.doc2path(docname))
653+
doctree.reporter = LoggingReporter(str(self.doc2path(docname)))
654654
except KeyError:
655655
doctree = self.get_doctree(docname)
656656

sphinx/environment/adapters/toctree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def _toctree_entry(
319319
ref, location=toctreenode, type='toc', subtype='no_title')
320320
except KeyError:
321321
# this is raised if the included file does not exist
322-
ref_path = env.doc2path(ref, False)
322+
ref_path = str(env.doc2path(ref, False))
323323
if excluded(ref_path):
324324
message = __('toctree contains reference to excluded document %r')
325325
elif not included(ref_path):

sphinx/ext/autosummary/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def run(self) -> list[Node]:
249249
docname = posixpath.join(tree_prefix, real_name)
250250
docname = posixpath.normpath(posixpath.join(dirname, docname))
251251
if docname not in self.env.found_docs:
252-
if excluded(self.env.doc2path(docname, False)):
252+
if excluded(str(self.env.doc2path(docname, False))):
253253
msg = __('autosummary references excluded document %r. Ignored.')
254254
else:
255255
msg = __('autosummary: stub file not found %r. '
@@ -802,7 +802,7 @@ def process_generate_options(app: Sphinx) -> None:
802802

803803
if genfiles is True:
804804
env = app.builder.env
805-
genfiles = [env.doc2path(x, base=False) for x in env.found_docs
805+
genfiles = [str(env.doc2path(x, base=False)) for x in env.found_docs
806806
if os.path.isfile(env.doc2path(x))]
807807
elif genfiles is False:
808808
pass

sphinx/ext/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def get_filename_for_node(self, node: Node, docname: str) -> str:
373373
try:
374374
filename = relpath(node.source, self.env.srcdir).rsplit(':docstring of ', maxsplit=1)[0] # type: ignore[arg-type] # noqa: E501
375375
except Exception:
376-
filename = self.env.doc2path(docname, False)
376+
filename = str(self.env.doc2path(docname, False))
377377
return filename
378378

379379
@staticmethod

0 commit comments

Comments
 (0)