Skip to content

Commit 4c4e335

Browse files
committed
use pathlib
1 parent be92bdc commit 4c4e335

File tree

6 files changed

+36
-42
lines changed

6 files changed

+36
-42
lines changed

test/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pathlib
23
import subprocess
34
import tempfile
45

@@ -41,8 +42,8 @@ def assert_equality(
4142
)
4243
plt.close()
4344

44-
this_dir = os.path.dirname(os.path.abspath(__file__))
45-
with open(os.path.join(this_dir, filename), encoding="utf-8") as f:
45+
this_dir = pathlib.Path(__file__).resolve().parent
46+
with open(this_dir / filename, encoding="utf-8") as f:
4647
reference = f.read()
4748
assert reference == code, filename + "\n" + _unidiff_output(reference, code)
4849

test/refresh_reference_files.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import importlib.util
3-
import os
3+
import pathlib
44

55
import matplotlib.pyplot as plt
66

@@ -11,16 +11,14 @@ def _main():
1111
parser = argparse.ArgumentParser(description="Refresh all reference TeX files.")
1212
parser.parse_args()
1313

14-
this_dir = os.path.dirname(os.path.abspath(__file__))
14+
this_dir = pathlib.Path(__file__).resolve().parent
1515

1616
test_files = [
1717
f
18-
for f in os.listdir(this_dir)
19-
if os.path.isfile(os.path.join(this_dir, f))
20-
and f[:5] == "test_"
21-
and f[-3:] == ".py"
18+
for f in this_dir.iterdir()
19+
if (this_dir / f).is_file() and f.name[:5] == "test_" and f.name[-3:] == ".py"
2220
]
23-
test_modules = [f[:-3] for f in test_files]
21+
test_modules = [f.name[:-3] for f in test_files]
2422

2523
# remove some edge cases
2624
test_modules.remove("test_rotated_labels")
@@ -36,7 +34,7 @@ def _main():
3634
plt.close()
3735

3836
tex_filename = mod + "_reference.tex"
39-
with open(os.path.join(this_dir, tex_filename), "w", encoding="utf8") as f:
37+
with open(this_dir / tex_filename, "w", encoding="utf8") as f:
4038
f.write(code)
4139

4240

test/test_image_plot_lower.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pathlib
2+
13
import matplotlib.pyplot as plt
24
from helpers import assert_equality
35

@@ -6,13 +8,11 @@
68

79

810
def plot():
9-
import os
10-
1111
import matplotlib.image as mpimg
1212
from matplotlib import rcParams
1313

14-
this_dir = os.path.dirname(os.path.realpath(__file__))
15-
img = mpimg.imread(os.path.join(this_dir, "lena.png"))
14+
this_dir = pathlib.Path(__file__).resolve().parent
15+
img = mpimg.imread(this_dir / "lena.png")
1616

1717
dpi = rcParams["figure.dpi"]
1818
figsize = img.shape[0] / dpi, img.shape[1] / dpi

test/test_image_plot_upper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import pathlib
2+
13
import matplotlib.pyplot as plt
24

35
# the picture 'lena.png' with origin='lower' is flipped upside-down.
46
# So it has to be upside-down in the pdf-file as well.
57

68

79
def plot():
8-
import os
9-
1010
import matplotlib.image as mpimg
1111
from matplotlib import rcParams
1212

13-
this_dir = os.path.dirname(os.path.realpath(__file__))
14-
img = mpimg.imread(os.path.join(this_dir, "lena.png"))
13+
this_dir = pathlib.Path(__file__).resolve().parent
14+
img = mpimg.imread(this_dir / "lena.png")
1515

1616
dpi = rcParams["figure.dpi"]
1717
figsize = img.shape[0] / dpi, img.shape[1] / dpi

tikzplotlib/_files.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import os
2-
import posixpath
1+
import pathlib
32

43

5-
def _gen_filename(data, nb_key, ext):
4+
def _gen_filepath(data, nb_key, ext):
65
name = data["base name"] + f"-{data[nb_key]:03d}{ext}"
7-
return os.path.join(data["output dir"], name), name
6+
return pathlib.Path(data["output dir"]) / name
87

98

10-
def new_filename(data, file_kind, ext):
11-
"""Returns an available filename.
9+
def new_filepath(data, file_kind, ext):
10+
"""Returns an available filepath.
1211
1312
:param file_kind: Name under which numbering is recorded, such as 'img' or
1413
'table'.
@@ -17,7 +16,7 @@ def new_filename(data, file_kind, ext):
1716
:param ext: Filename extension.
1817
:type ext: str
1918
20-
:returns: (filename, rel_filepath) where filename is a path in the
19+
:returns: (filepath, rel_filepath) where filepath is a path in the
2120
filesystem and rel_filepath is the path to be used in the tex
2221
code.
2322
"""
@@ -26,20 +25,19 @@ def new_filename(data, file_kind, ext):
2625
if nb_key not in data.keys():
2726
data[nb_key] = -1
2827

28+
data[nb_key] = data[nb_key] + 1
29+
filepath = _gen_filepath(data, nb_key, ext)
2930
if not data["override externals"]:
3031
# Make sure not to overwrite anything.
31-
file_exists = True
32+
file_exists = filepath.is_file()
3233
while file_exists:
3334
data[nb_key] = data[nb_key] + 1
34-
filename, name = _gen_filename(data, nb_key, ext)
35-
file_exists = os.path.isfile(filename)
36-
else:
37-
data[nb_key] = data[nb_key] + 1
38-
filename, name = _gen_filename(data, nb_key, ext)
35+
filepath = _gen_filepath(data, nb_key, ext)
36+
file_exists = filepath.is_file()
3937

4038
if data["rel data path"]:
41-
rel_filepath = posixpath.join(data["rel data path"], name)
39+
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
4240
else:
43-
rel_filepath = name
41+
rel_filepath = filepath.name
4442

45-
return filename, rel_filepath
43+
return filepath, rel_filepath

tikzplotlib/_save.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import codecs
22
import enum
3-
import os
3+
import pathlib
44
import tempfile
55
import warnings
66

@@ -154,14 +154,13 @@ def get_tikz_code(
154154
data["override externals"] = override_externals
155155

156156
if filepath:
157-
data["output dir"] = os.path.dirname(filepath)
157+
filepath = pathlib.Path(filepath)
158+
data["output dir"] = filepath.parent
158159
else:
159160
directory = tempfile.mkdtemp()
160-
data["output dir"] = directory
161+
data["output dir"] = pathlib.Path(directory)
161162

162-
data["base name"] = (
163-
os.path.splitext(os.path.basename(filepath))[0] if filepath else "tmp"
164-
)
163+
data["base name"] = filepath.stem if filepath else "tmp"
165164
data["strict"] = strict
166165
data["tikz libs"] = set()
167166
data["pgfplots libs"] = set()
@@ -254,7 +253,6 @@ def save(filepath, *args, encoding=None, **kwargs):
254253
file_handle = codecs.open(filepath, "w", encoding)
255254
file_handle.write(code)
256255
file_handle.close()
257-
return
258256

259257

260258
def _tex_comment(comment):
@@ -282,7 +280,6 @@ def _print_pgfplot_libs_message(data):
282280
print("Please add the following lines to your LaTeX preamble:\n")
283281
print(data["flavor"].preamble(data))
284282
print(70 * "=")
285-
return
286283

287284

288285
class _ContentManager:

0 commit comments

Comments
 (0)