Skip to content

Commit b8983b5

Browse files
committed
Consistently use pathlib for bibfiles.
1 parent 0539975 commit b8983b5

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

src/sphinxcontrib/bibtex/bibfile.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717
"""
1818

1919
import math
20-
import os.path
21-
from typing import TYPE_CHECKING, Dict, List, NamedTuple, Set
20+
from pathlib import Path
21+
from typing import Dict, List, NamedTuple, Set
2222

2323
from docutils.nodes import make_id
2424
from pybtex.database import BibliographyData, BibliographyDataError
2525
from pybtex.database.input.bibtex import Parser
2626
from sphinx.util.logging import getLogger
2727

28-
if TYPE_CHECKING:
29-
from sphinx.environment import BuildEnvironment
30-
3128

3229
logger = getLogger(__name__)
3330

@@ -43,30 +40,25 @@ class BibData(NamedTuple):
4340
"""Contains information about a collection of bib files."""
4441

4542
encoding: str #: Encoding of all bib files.
46-
bibfiles: Dict[str, BibFile] #: Maps bib filename to information about it.
43+
bibfiles: Dict[Path, BibFile] #: Maps bib filename to information about it.
4744
data: BibliographyData #: Data parsed from all bib files.
4845

4946

50-
def normpath_filename(env: "BuildEnvironment", filename: str) -> str:
51-
"""Return normalised path to *filename* for the given environment *env*."""
52-
return os.path.normpath(env.relfn2path(filename.strip())[1])
53-
54-
55-
def get_mtime(bibfilename: str) -> float:
47+
def get_mtime(bibfilename: Path) -> float:
5648
try:
57-
return os.path.getmtime(bibfilename)
49+
return bibfilename.lstat().st_mtime
5850
except OSError:
5951
return -math.inf
6052

6153

62-
def parse_bibdata(bibfilenames: List[str], encoding: str) -> BibData:
54+
def parse_bibdata(bibfilenames: List[Path], encoding: str) -> BibData:
6355
"""Parse *bibfilenames* with given *encoding*, and return parsed data."""
6456
parser = Parser(encoding)
65-
bibfiles: Dict[str, BibFile] = {}
57+
bibfiles: Dict[Path, BibFile] = {}
6658
keys: Dict[str, None] = {}
6759
for filename in bibfilenames:
6860
logger.info("parsing bibtex file {0}... ".format(filename), nonl=True)
69-
if not os.path.isfile(filename):
61+
if not filename.is_file():
7062
logger.warning(
7163
"could not open bibtex file {0}.".format(filename),
7264
type="bibtex",
@@ -91,7 +83,7 @@ def parse_bibdata(bibfilenames: List[str], encoding: str) -> BibData:
9183

9284

9385
def is_bibdata_outdated(
94-
bibdata: BibData, bibfilenames: List[str], encoding: str
86+
bibdata: BibData, bibfilenames: List[Path], encoding: str
9587
) -> bool:
9688
return (
9789
bibdata.encoding != encoding
@@ -104,7 +96,7 @@ def is_bibdata_outdated(
10496

10597

10698
def process_bibdata(
107-
bibdata: BibData, bibfilenames: List[str], encoding: str
99+
bibdata: BibData, bibfilenames: List[Path], encoding: str
108100
) -> BibData:
109101
"""Parse *bibfilenames* and store parsed data in *bibdata*."""
110102
logger.info("checking bibtex cache... ", nonl=True)

src/sphinxcontrib/bibtex/directives.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import sphinx.util
2020
from docutils.parsers.rst import Directive
2121

22-
from .bibfile import _make_ids, normpath_filename
22+
from .bibfile import _make_ids
2323
from .nodes import bibliography as bibliography_node
2424

2525
if TYPE_CHECKING:
@@ -42,7 +42,7 @@ class BibliographyValue(NamedTuple):
4242
"""Contains information about a bibliography directive."""
4343

4444
line: int #: Line number of the directive in the document.
45-
bibfiles: List[str] #: List of bib files for this directive.
45+
bibfiles: List[Path] #: List of bib files for this directive.
4646
style: str #: The pybtex style.
4747
list_: str #: The list type.
4848
enumtype: str #: The sequence type (for enumerated lists).
@@ -147,23 +147,23 @@ def run(self) -> Sequence[docutils.nodes.Node]:
147147
domain = cast("BibtexDomain", env.get_domain("cite"))
148148
filter_ = self._get_filter()
149149
if self.arguments:
150-
bibfiles = []
151-
for bibfile in self.arguments[0].split():
152-
normbibfile = str(Path(normpath_filename(env, bibfile)).resolve())
153-
if normbibfile not in domain.bibdata.bibfiles:
150+
bibfiles: list[Path] = []
151+
for bibfile_str in self.arguments[0].split():
152+
bibfile = Path(env.relfn2path(bibfile_str)[1]).resolve()
153+
if bibfile not in domain.bibdata.bibfiles:
154154
logger.warning(
155155
"{0} not found or not configured"
156-
" in bibtex_bibfiles".format(bibfile),
156+
" in bibtex_bibfiles".format(bibfile_str),
157157
location=(env.docname, self.lineno),
158158
type="bibtex",
159159
subtype="bibfile_error",
160160
)
161161
else:
162-
bibfiles.append(normbibfile)
162+
bibfiles.append(bibfile)
163163
else:
164164
bibfiles = list(domain.bibdata.bibfiles.keys())
165165
for bibfile in bibfiles:
166-
env.note_dependency(bibfile)
166+
env.note_dependency(str(bibfile))
167167
# generate nodes and ids
168168
keyprefix: str = self.options.get("keyprefix", "")
169169
list_: str = self.options.get("list", "citation")

src/sphinxcontrib/bibtex/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def __init__(self, env: "BuildEnvironment"):
303303
raise ExtensionError("You must configure the bibtex_bibfiles setting")
304304
# canonicalize bibfile paths relative to confdir
305305
bibfiles = [
306-
str((Path(env.app.confdir) / bibfile).resolve())
306+
(Path(env.app.confdir) / bibfile).resolve()
307307
for bibfile in env.app.config.bibtex_bibfiles
308308
]
309309
# update bib file information in the cache
@@ -495,7 +495,7 @@ def get_all_cited_keys(self, docnames):
495495
for target in citation_ref.targets:
496496
yield target.key
497497

498-
def get_entries(self, bibfiles: List[str]) -> Iterable["Entry"]:
498+
def get_entries(self, bibfiles: List[Path]) -> Iterable["Entry"]:
499499
"""Return all bibliography entries from the bib files, unsorted (i.e.
500500
in order of appearance in the bib files).
501501
"""

0 commit comments

Comments
 (0)