Skip to content

Commit 9038fb7

Browse files
authored
Merge pull request #530 from nschloe/filepaths-windows
try windows tests
2 parents 15f0bfc + 682d565 commit 9038fb7

File tree

7 files changed

+37
-27
lines changed

7 files changed

+37
-27
lines changed

.github/workflows/ci.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ jobs:
2020
uses: pre-commit/[email protected]
2121

2222
build:
23-
runs-on: ubuntu-latest
23+
runs-on: ${{ matrix.os }}
2424
strategy:
2525
matrix:
26+
os: [ubuntu-latest, windows-latest, macOS-latest]
2627
python-version: ["3.7", "3.8", "3.9", "3.10"]
2728
steps:
2829
- uses: actions/setup-python@v2
@@ -36,4 +37,4 @@ jobs:
3637
pip install tox
3738
tox -- --cov tikzplotlib --cov-report xml --cov-report term
3839
- uses: codecov/codecov-action@v1
39-
if: ${{ matrix.python-version == '3.9' }}
40+
if: ${{ matrix.python-version == '3.10' && matrix.os == 'ubuntu-latest' }}

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = tikzplotlib
3-
version = 0.9.15
3+
version = 0.9.16
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Convert matplotlib figures into TikZ/PGFPlots

src/tikzplotlib/_files.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import pathlib
1+
from pathlib import Path
22

33

44
def _gen_filepath(data, nb_key, ext):
5-
name = data["base name"] + f"-{data[nb_key]:03d}{ext}"
6-
return pathlib.Path(data["output dir"]) / name
5+
rel_filepath = Path(f"{data['base name']}-{data[nb_key]:03d}{ext}")
6+
7+
if data["rel data path"]:
8+
rel_filepath = data["rel data path"] / rel_filepath
9+
10+
return data["output dir"] / rel_filepath, rel_filepath
711

812

913
def new_filepath(data, file_kind, ext):
@@ -25,19 +29,12 @@ def new_filepath(data, file_kind, ext):
2529
if nb_key not in data.keys():
2630
data[nb_key] = -1
2731

28-
data[nb_key] = data[nb_key] + 1
29-
filepath = _gen_filepath(data, nb_key, ext)
32+
data[nb_key] += 1
33+
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)
3034
if not data["override externals"]:
3135
# Make sure not to overwrite anything.
32-
file_exists = filepath.is_file()
33-
while file_exists:
34-
data[nb_key] = data[nb_key] + 1
35-
filepath = _gen_filepath(data, nb_key, ext)
36-
file_exists = filepath.is_file()
37-
38-
if data["rel data path"]:
39-
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
40-
else:
41-
rel_filepath = filepath.name
36+
while filepath.is_file():
37+
data[nb_key] += 1
38+
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)
4239

4340
return filepath, rel_filepath

src/tikzplotlib/_image.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ def draw_image(data, obj):
5252
# Explicitly use \pgfimage as includegrapics command, as the default
5353
# \includegraphics fails unexpectedly in some cases
5454
ff = data["float format"]
55+
# Always use slash in file paths, see
56+
# <https://tex.stackexchange.com/a/18923/13262>
57+
# <https://github.com/nschloe/tikzplotlib/issues/509>
58+
posix_filepath = rel_filepath.as_posix()
5559
content.append(
5660
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
5761
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
58-
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
62+
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
5963
)
6064
return data, content

src/tikzplotlib/_quadmesh.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ def draw_quadmesh(data, obj):
5353
# Explicitly use \pgfimage as includegrapics command, as the default
5454
# \includegraphics fails unexpectedly in some cases
5555
ff = data["float format"]
56+
posix_filepath = rel_filepath.as_posix()
5657
content.append(
5758
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
5859
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
59-
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
60+
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
6061
)
6162

6263
return data, content

src/tikzplotlib/_save.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

33
import enum
4-
import pathlib
54
import tempfile
65
import warnings
6+
from pathlib import Path
77

88
import matplotlib as mpl
99
import matplotlib.pyplot as plt
@@ -18,7 +18,7 @@
1818

1919
def get_tikz_code(
2020
figure="gcf",
21-
filepath: str | pathlib.Path | None = None,
21+
filepath: str | Path | None = None,
2222
axis_width: str | None = None,
2323
axis_height: str | None = None,
2424
textsize: float = 10.0,
@@ -152,18 +152,20 @@ def get_tikz_code(
152152
data = {}
153153
data["axis width"] = axis_width
154154
data["axis height"] = axis_height
155-
data["rel data path"] = tex_relative_path_to_data
155+
data["rel data path"] = (
156+
None if tex_relative_path_to_data is None else Path(tex_relative_path_to_data)
157+
)
156158
data["externalize tables"] = externalize_tables
157159
data["override externals"] = override_externals
158160
data["externals search path"] = externals_search_path
159161

160162
if filepath:
161-
filepath = pathlib.Path(filepath)
163+
filepath = Path(filepath)
162164
data["output dir"] = filepath.parent
163165
data["base name"] = filepath.stem
164166
else:
165167
directory = tempfile.mkdtemp()
166-
data["output dir"] = pathlib.Path(directory)
168+
data["output dir"] = Path(directory)
167169
data["base name"] = "tmp"
168170

169171
data["strict"] = strict
@@ -247,7 +249,7 @@ def get_tikz_code(
247249
return code
248250

249251

250-
def save(filepath: str | pathlib.Path, *args, encoding: str | None = None, **kwargs):
252+
def save(filepath: str | Path, *args, encoding: str | None = None, **kwargs):
251253
"""Same as `get_tikz_code()`, but actually saves the code to a file.
252254
253255
:param filepath: The file to which the TikZ output will be written.

tests/test_viridis.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ def plot():
1616
def test():
1717
from .helpers import assert_equality
1818

19-
assert_equality(plot, __file__[:-3] + "_reference.tex")
19+
# test relative data path
20+
assert_equality(
21+
plot,
22+
__file__[:-3] + "_reference.tex",
23+
# tex_relative_path_to_data="data/files"
24+
)

0 commit comments

Comments
 (0)