Skip to content

Commit be8fcb8

Browse files
committed
Directly import Path from pathlib.
The simpler use looks nicer, and the pathlib._normal_accessor attribute was removed in Python 3.11 so we no longer need to check for it.
1 parent 486fc73 commit be8fcb8

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

pgfutils.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import inspect
3030
import io
3131
import os
32-
import pathlib
32+
from pathlib import Path
3333
import re
3434
import string
3535
import sys
@@ -343,9 +343,9 @@ def in_tracking_dir(self, type, fn):
343343
raise ValueError(f"Unknown tracking type {type}.")
344344

345345
# If we can compute a relative path, it must be within the directory.
346-
fn = pathlib.Path(fn).resolve()
346+
fn = Path(fn).resolve()
347347
for path in paths:
348-
resolved = pathlib.Path(path).resolve()
348+
resolved = Path(path).resolve()
349349
try:
350350
fn.relative_to(resolved)
351351
except ValueError:
@@ -422,9 +422,9 @@ def _relative_if_subdir(fn):
422422
absolute path otherwise.
423423
424424
"""
425-
fn = pathlib.Path(fn).resolve()
425+
fn = Path(fn).resolve()
426426
try:
427-
return fn.relative_to(pathlib.Path.cwd())
427+
return fn.relative_to(Path.cwd())
428428
except ValueError:
429429
return fn
430430

@@ -555,8 +555,6 @@ def _install_standard_file_trackers():
555555

556556
builtins.open = _file_tracker(builtins.open)
557557
io.open = _file_tracker(io.open)
558-
if hasattr(pathlib, "_normal_accessor"):
559-
pathlib._normal_accessor.open = _file_tracker(pathlib._normal_accessor.open)
560558
sys.meta_path.insert(0, ImportTracker())
561559

562560

@@ -627,7 +625,7 @@ def add_dependencies(*args):
627625
628626
"""
629627
for fn in args:
630-
_file_tracker.filenames.add(("r", pathlib.Path(fn)))
628+
_file_tracker.filenames.add(("r", Path(fn)))
631629

632630

633631
def _list_opened_files():
@@ -637,7 +635,7 @@ def _list_opened_files():
637635
-------
638636
list
639637
A list of tuples (mode, filename) of files in or under the current directory
640-
where each filename is a pathlib.Path instance. Entries with mode 'r' were
638+
where each filename is a Path instance. Entries with mode 'r' were
641639
opened for reading and are assumed to be dependencies of the generated figure.
642640
Entries with mode 'w' were opened for writing and are rasterised graphics
643641
included in the final figure. The list is sorted by mode and then filename.
@@ -692,7 +690,7 @@ def setup_figure(
692690
_config_reset()
693691

694692
# Load configuration from a local file if one exists.
695-
cfg_path = pathlib.Path("pgfutils.cfg")
693+
cfg_path = Path("pgfutils.cfg")
696694
if cfg_path.exists():
697695
if not cfg_path.is_file():
698696
raise RuntimeError("pgfutils.cfg exists but is not a file.")
@@ -763,7 +761,7 @@ def setup_figure(
763761
# Custom TeX preamble.
764762
preamble = _config["pgfutils"]["preamble"]
765763
if _config["pgfutils"].getboolean("preamble_substitute"):
766-
preamble = string.Template(preamble).substitute(basedir=str(pathlib.Path.cwd()))
764+
preamble = string.Template(preamble).substitute(basedir=str(Path.cwd()))
767765
matplotlib.rcParams["pgf.preamble"] = preamble
768766

769767
# Clear the existing lists of specific font names.
@@ -959,7 +957,7 @@ def save(figure=None):
959957
return
960958

961959
# Look at the next frame up for the name of the calling script.
962-
script = pathlib.Path(inspect.getfile(sys._getframe(1)))
960+
script = Path(inspect.getfile(sys._getframe(1)))
963961

964962
# The initial Matplotlib output file, and the final figure file.
965963
mpname = script.with_suffix(".pgf")
@@ -1006,7 +1004,7 @@ def save(figure=None):
10061004

10071005
# Only apply this if the figure is not in the top-level directory.
10081006
if not figdir.samefile("."):
1009-
prefix = figdir.relative_to(pathlib.Path.cwd())
1007+
prefix = figdir.relative_to(Path.cwd())
10101008
expr = re.compile(r"(\\(?:pgfimage|includegraphics)(?:\[.+?\])?{)(.+?)}")
10111009
repl = rf"\1{prefix}/\2}}"
10121010
pp_funcs.append(lambda s: re.sub(expr, repl, s))

0 commit comments

Comments
 (0)