Skip to content

Commit e262aa2

Browse files
committed
Handle configured paths as sets.
* Define each attribute as set[Path]. * Resolve and add each path to the sets when loading configuration.
1 parent 387d11c commit e262aa2

File tree

7 files changed

+38
-31
lines changed

7 files changed

+38
-31
lines changed

data/share/matplotlib-pgfutils/pgfutils.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ environment = ""
135135
# taken to be relative to the project's top-level directory. Note that these
136136
# are not added to any search paths, i.e., you still have to use the full path
137137
# when loading data files in a script.
138-
data = "."
138+
data = [
139+
".",
140+
]
139141

140142
# Entries to add to the start of sys.path. This will be done in the order
141143
# specified here, so the last path in this setting will be the first in
@@ -147,12 +149,12 @@ data = "."
147149
# subdir/pythonlib
148150
# /usr/share/myotherlib
149151
#
150-
pythonpath = ""
152+
pythonpath = []
151153

152154

153155
# Paths containing Python libraries which are already on the Python path, but
154156
# should be included in the file tracking.
155-
extra_imports = ""
157+
extra_imports = []
156158

157159

158160
# Specific Matplotlib rcParams to set.

pgfutils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ def parse_dimension(spec: str) -> float:
330330
class PathConfig(TypedDict):
331331
"""Path configuration options."""
332332

333-
data: str
334-
pythonpath: str
335-
extra_imports: str
333+
data: set[Path]
334+
pythonpath: set[Path]
335+
extra_imports: set[Path]
336336

337337

338338
class PGFUtilsConfig(TypedDict):
@@ -393,7 +393,9 @@ def __init__(self, load_file: bool = True) -> None:
393393
directory and load it after setting the defaults.
394394
395395
"""
396-
self.paths = dict(data=".", pythonpath="", extra_imports="")
396+
self.paths = dict(
397+
data={Path.cwd().resolve()}, pythonpath=set(), extra_imports=set()
398+
)
397399
self.pgfutils = dict(
398400
preamble="",
399401
preamble_substitute=False,
@@ -531,6 +533,17 @@ def update(self, new_settings: Mapping[str, Mapping[str, Any]]):
531533
)
532534
section[key] = val
533535

536+
# For a set, handle based on the type of the set items.
537+
elif origin is set:
538+
if not isinstance(val, (set, list, tuple)):
539+
raise ConfigError(name, key, "value must be a list")
540+
541+
if args == (Path,):
542+
for item in val:
543+
section[key].add(Path(item).resolve())
544+
else:
545+
raise RuntimeError(f"unhandled set type {args}")
546+
534547
# Assume a string.
535548
else:
536549
section[key] = val
@@ -557,19 +570,17 @@ def in_tracking_dir(type, fn):
557570
558571
"""
559572
if type == "data":
560-
paths = config.paths["data"].strip().splitlines()
573+
paths = config.paths["data"]
561574
elif type == "import":
562-
paths = config.paths["pythonpath"].strip().splitlines()
563-
paths.extend(config.paths["extra_imports"].strip().splitlines())
575+
paths = config.paths["pythonpath"].union(config.paths["extra_imports"])
564576
else:
565577
raise ValueError(f"Unknown tracking type {type}.")
566578

567579
# If we can compute a relative path, it must be within the directory.
568580
fn = Path(fn).resolve()
569581
for path in paths:
570-
resolved = Path(path).resolve()
571582
try:
572-
fn.relative_to(resolved)
583+
fn.relative_to(path)
573584
except ValueError:
574585
continue
575586
return True
@@ -737,10 +748,8 @@ def setup_figure(
737748
_interactive = True
738749

739750
# Add any desired entries to sys.path.
740-
for newpath in config.paths["pythonpath"].splitlines():
741-
newpath = newpath.strip()
742-
if newpath:
743-
sys.path.insert(0, newpath)
751+
for newpath in config.paths["pythonpath"]:
752+
sys.path.insert(0, str(newpath))
744753

745754
# Set the backend. We don't want to overwrite the current backend if this is an
746755
# interactive run as the PGF backend does not implement a GUI.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# SPDX-FileCopyrightText: Blair Bonnett
22
# SPDX-License-Identifier: BSD-3-Clause
33

4-
[paths]
5-
data = "."
6-
74
[post_processing]
85
fix_raster_paths = true
96
tikzpicture = false

tests/sources/pythonpath/pgfutils.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
[paths]
5-
pythonpath = "libdir"
5+
pythonpath = ["libdir"]

tests/sources/tracking/extra_dirs/pgfutils.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
[paths]
5-
data = """
6-
.
7-
..
8-
../..
9-
"""
5+
data = [
6+
"..",
7+
"../..",
8+
]

tests/sources/tracking/imports/cfg_extra/pgfutils.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
[paths]
5-
extra_imports = """
6-
../lib
7-
"""
5+
extra_imports = [
6+
"../lib"
7+
]

tests/sources/tracking/imports/cfg_pythonpath/pgfutils.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
[paths]
5-
pythonpath = """
6-
../lib
7-
"""
5+
pythonpath = [
6+
"../lib",
7+
]

0 commit comments

Comments
 (0)