Skip to content

Commit

Permalink
Merge pull request #530 from nschloe/filepaths-windows
Browse files Browse the repository at this point in the history
try windows tests
  • Loading branch information
nschloe authored Dec 3, 2021
2 parents 15f0bfc + 682d565 commit 9038fb7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ jobs:
uses: pre-commit/[email protected]

build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
python-version: ["3.7", "3.8", "3.9", "3.10"]
steps:
- uses: actions/setup-python@v2
Expand All @@ -36,4 +37,4 @@ jobs:
pip install tox
tox -- --cov tikzplotlib --cov-report xml --cov-report term
- uses: codecov/codecov-action@v1
if: ${{ matrix.python-version == '3.9' }}
if: ${{ matrix.python-version == '3.10' && matrix.os == 'ubuntu-latest' }}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = tikzplotlib
version = 0.9.15
version = 0.9.16
author = Nico Schlömer
author_email = [email protected]
description = Convert matplotlib figures into TikZ/PGFPlots
Expand Down
27 changes: 12 additions & 15 deletions src/tikzplotlib/_files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import pathlib
from pathlib import Path


def _gen_filepath(data, nb_key, ext):
name = data["base name"] + f"-{data[nb_key]:03d}{ext}"
return pathlib.Path(data["output dir"]) / name
rel_filepath = Path(f"{data['base name']}-{data[nb_key]:03d}{ext}")

if data["rel data path"]:
rel_filepath = data["rel data path"] / rel_filepath

return data["output dir"] / rel_filepath, rel_filepath


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

data[nb_key] = data[nb_key] + 1
filepath = _gen_filepath(data, nb_key, ext)
data[nb_key] += 1
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)
if not data["override externals"]:
# Make sure not to overwrite anything.
file_exists = filepath.is_file()
while file_exists:
data[nb_key] = data[nb_key] + 1
filepath = _gen_filepath(data, nb_key, ext)
file_exists = filepath.is_file()

if data["rel data path"]:
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
else:
rel_filepath = filepath.name
while filepath.is_file():
data[nb_key] += 1
filepath, rel_filepath = _gen_filepath(data, nb_key, ext)

return filepath, rel_filepath
6 changes: 5 additions & 1 deletion src/tikzplotlib/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ def draw_image(data, obj):
# Explicitly use \pgfimage as includegrapics command, as the default
# \includegraphics fails unexpectedly in some cases
ff = data["float format"]
# Always use slash in file paths, see
# <https://tex.stackexchange.com/a/18923/13262>
# <https://github.com/nschloe/tikzplotlib/issues/509>
posix_filepath = rel_filepath.as_posix()
content.append(
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
)
return data, content
3 changes: 2 additions & 1 deletion src/tikzplotlib/_quadmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def draw_quadmesh(data, obj):
# Explicitly use \pgfimage as includegrapics command, as the default
# \includegraphics fails unexpectedly in some cases
ff = data["float format"]
posix_filepath = rel_filepath.as_posix()
content.append(
"\\addplot graphics [includegraphics cmd=\\pgfimage,"
f"xmin={extent[0]:{ff}}, xmax={extent[1]:{ff}}, "
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{rel_filepath}}};\n"
f"ymin={extent[2]:{ff}}, ymax={extent[3]:{ff}}] {{{posix_filepath}}};\n"
)

return data, content
14 changes: 8 additions & 6 deletions src/tikzplotlib/_save.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import enum
import pathlib
import tempfile
import warnings
from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
Expand All @@ -18,7 +18,7 @@

def get_tikz_code(
figure="gcf",
filepath: str | pathlib.Path | None = None,
filepath: str | Path | None = None,
axis_width: str | None = None,
axis_height: str | None = None,
textsize: float = 10.0,
Expand Down Expand Up @@ -152,18 +152,20 @@ def get_tikz_code(
data = {}
data["axis width"] = axis_width
data["axis height"] = axis_height
data["rel data path"] = tex_relative_path_to_data
data["rel data path"] = (
None if tex_relative_path_to_data is None else Path(tex_relative_path_to_data)
)
data["externalize tables"] = externalize_tables
data["override externals"] = override_externals
data["externals search path"] = externals_search_path

if filepath:
filepath = pathlib.Path(filepath)
filepath = Path(filepath)
data["output dir"] = filepath.parent
data["base name"] = filepath.stem
else:
directory = tempfile.mkdtemp()
data["output dir"] = pathlib.Path(directory)
data["output dir"] = Path(directory)
data["base name"] = "tmp"

data["strict"] = strict
Expand Down Expand Up @@ -247,7 +249,7 @@ def get_tikz_code(
return code


def save(filepath: str | pathlib.Path, *args, encoding: str | None = None, **kwargs):
def save(filepath: str | Path, *args, encoding: str | None = None, **kwargs):
"""Same as `get_tikz_code()`, but actually saves the code to a file.
:param filepath: The file to which the TikZ output will be written.
Expand Down
7 changes: 6 additions & 1 deletion tests/test_viridis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ def plot():
def test():
from .helpers import assert_equality

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

0 comments on commit 9038fb7

Please sign in to comment.