Skip to content

Commit

Permalink
Migrate from os.path to pathlib (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman authored Mar 20, 2024
1 parent bfb641e commit 0cdb87d
Show file tree
Hide file tree
Showing 26 changed files with 131 additions and 149 deletions.
4 changes: 2 additions & 2 deletions examples/gallery/images/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

# %%
import os
from pathlib import Path

import pygmt

Expand All @@ -28,6 +28,6 @@
)

# clean up the downloaded image in the current directory
os.remove("gmt-logo.png")
Path("gmt-logo.png").unlink()

fig.show()
4 changes: 2 additions & 2 deletions examples/tutorials/basics/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

# %%
import os
from pathlib import Path

import pygmt

Expand Down Expand Up @@ -88,7 +88,7 @@
fig.text(textfiles="examples.txt", angle=True, font=True, justify=True)

# Cleanups
os.remove("examples.txt")
Path("examples.txt").unlink()

fig.show()

Expand Down
13 changes: 6 additions & 7 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class Figure:
>>> fig.basemap(region=[0, 360, -90, 90], projection="W15c", frame=True)
>>> fig.savefig("my-figure.png")
>>> # Make sure the figure file is generated and clean it up
>>> import os
>>> os.path.exists("my-figure.png")
True
>>> os.remove("my-figure.png")
>>> from pathlib import Path
>>> assert Path("my-figure.png").exists()
>>> Path("my-figure.png").unlink()
The plot region can be specified through ISO country codes (for example,
``"JP"`` for Japan):
Expand Down Expand Up @@ -380,8 +379,8 @@ def savefig( # noqa: PLR0912
# Remove the .pgw world file if exists
# Not necessary after GMT 6.5.0.
# See upstream fix https://github.com/GenericMappingTools/gmt/pull/7865
if ext == "tiff" and fname.with_suffix(".pgw").exists():
fname.with_suffix(".pgw").unlink()
if ext == "tiff":
fname.with_suffix(".pgw").unlink(missing_ok=True)

# Rename if file extension doesn't match the input file suffix
if ext != suffix[1:]:
Expand Down Expand Up @@ -495,7 +494,7 @@ def _preview(self, fmt, dpi, as_bytes=False, **kwargs):
If ``as_bytes=False``, this is the file name of the preview image
file. Else, it is the file content loaded as a bytes string.
"""
fname = os.path.join(self._preview_dir.name, f"{self._name}.{fmt}")
fname = Path(self._preview_dir.name) / f"{self._name}.{fmt}"
self.savefig(fname, dpi=dpi, **kwargs)
if as_bytes:
with open(fname, "rb") as image:
Expand Down
9 changes: 4 additions & 5 deletions pygmt/helpers/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Utilities for dealing with temporary file management.
"""

import os
import uuid
from contextlib import contextmanager
from pathlib import Path
from tempfile import NamedTemporaryFile

import numpy as np
Expand Down Expand Up @@ -72,8 +72,7 @@ def __exit__(self, *args):
"""
Remove the temporary file.
"""
if os.path.exists(self.name):
os.remove(self.name)
Path(self.name).unlink(missing_ok=True)

def read(self, keep_tabs=False):
"""
Expand Down Expand Up @@ -133,7 +132,7 @@ def tempfile_from_geojson(geojson):
with GMTTempFile(suffix=".gmt") as tmpfile:
import geopandas as gpd

os.remove(tmpfile.name) # ensure file is deleted first
Path(tmpfile.name).unlink() # Ensure file is deleted first
ogrgmt_kwargs = {"filename": tmpfile.name, "driver": "OGR_GMT", "mode": "w"}
try:
# OGR_GMT only supports 32-bit integers. We need to map int/int64
Expand Down Expand Up @@ -185,7 +184,7 @@ def tempfile_from_image(image):
A temporary GeoTIFF file holding the image data. E.g. '1a2b3c4d5.tif'.
"""
with GMTTempFile(suffix=".tif") as tmpfile:
os.remove(tmpfile.name) # ensure file is deleted first
Path(tmpfile.name).unlink() # Ensure file is deleted first
try:
image.rio.to_raster(raster_path=tmpfile.name)
except AttributeError as e: # object has no attribute 'rio'
Expand Down
26 changes: 12 additions & 14 deletions pygmt/helpers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import importlib
import inspect
import os
import string
from pathlib import Path

from pygmt.exceptions import GMTImageComparisonFailure
from pygmt.io import load_dataarray
Expand Down Expand Up @@ -39,6 +39,7 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
>>> import pytest
>>> import shutil
>>> from pygmt import Figure
>>> from pathlib import Path
>>> @check_figures_equal(result_dir="tmp_result_images")
... def test_check_figures_equal():
Expand All @@ -50,7 +51,7 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
... )
... return fig_ref, fig_test
>>> test_check_figures_equal()
>>> assert len(os.listdir("tmp_result_images")) == 0
>>> assert len(list(Path("tmp_result_images").iterdir())) == 0
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
>>> @check_figures_equal(result_dir="tmp_result_images")
Expand All @@ -63,12 +64,9 @@ def check_figures_equal(*, extensions=("png",), tol=0.0, result_dir="result_imag
>>> with pytest.raises(GMTImageComparisonFailure):
... test_check_figures_unequal()
>>> for suffix in ["", "-expected", "-failed-diff"]:
... assert os.path.exists(
... os.path.join(
... "tmp_result_images",
... f"test_check_figures_unequal{suffix}.png",
... )
... )
... assert (
... Path("tmp_result_images") / f"test_check_figures_unequal{suffix}.png"
... ).exists()
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
"""
allowed_chars = set(string.digits + string.ascii_letters + "_-[]()")
Expand All @@ -78,7 +76,7 @@ def decorator(func):
import pytest
from matplotlib.testing.compare import compare_images

os.makedirs(result_dir, exist_ok=True)
Path(result_dir).mkdir(parents=True, exist_ok=True)
old_sig = inspect.signature(func)

@pytest.mark.parametrize("ext", extensions)
Expand All @@ -93,8 +91,8 @@ def wrapper(*args, ext="png", request=None, **kwargs):
file_name = func.__name__
try:
fig_ref, fig_test = func(*args, **kwargs)
ref_image_path = os.path.join(result_dir, f"{file_name}-expected.{ext}")
test_image_path = os.path.join(result_dir, f"{file_name}.{ext}")
ref_image_path = Path(result_dir) / f"{file_name}-expected.{ext}"
test_image_path = Path(result_dir) / f"{file_name}.{ext}"
fig_ref.savefig(ref_image_path)
fig_test.savefig(test_image_path)

Expand All @@ -107,11 +105,11 @@ def wrapper(*args, ext="png", request=None, **kwargs):
in_decorator=True,
)
if err is None: # Images are the same
os.remove(ref_image_path)
os.remove(test_image_path)
ref_image_path.unlink()
test_image_path.unlink()
else: # Images are not the same
for key in ["actual", "expected", "diff"]:
err[key] = os.path.relpath(err[key])
err[key] = Path(err[key]).relative_to(".")
raise GMTImageComparisonFailure(
f"images not close (RMS {err['rms']:.3f}):\n"
f"\t{err['actual']}\n"
Expand Down
4 changes: 2 additions & 2 deletions pygmt/src/grdfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def grdfilter(grid, **kwargs):
Examples
--------
>>> import os
>>> from pathlib import Path
>>> import pygmt
>>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file
>>> # and return a filtered field (saved as netCDF)
Expand All @@ -126,7 +126,7 @@ def grdfilter(grid, **kwargs):
... spacing=0.5,
... outgrid="filtered_pacific.nc",
... )
>>> os.remove("filtered_pacific.nc") # Cleanup file
>>> Path("filtered_pacific.nc").unlink() # Cleanup file
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray
>>> # and return a filtered DataArray with the smoothed field
>>> grid = pygmt.datasets.load_earth_relief()
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def tempfile_from_dftrack(track, suffix):
)
yield tmpfilename
finally:
os.remove(tmpfilename)
Path(tmpfilename).unlink()


@fmt_docstring
Expand Down
3 changes: 1 addition & 2 deletions pygmt/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test the behaviour of the GMTDataArrayAccessor class.
"""

import os
import sys
from pathlib import Path

Expand Down Expand Up @@ -101,7 +100,7 @@ def test_accessor_sliced_datacube():
assert grid.gmt.registration == 0 # gridline registration
assert grid.gmt.gtype == 1 # geographic coordinate type
finally:
os.remove(fname)
Path(fname).unlink()


def test_accessor_grid_source_file_not_exist():
Expand Down
6 changes: 2 additions & 4 deletions pygmt/tests/test_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test the wrappers for the C API.
"""

import os
from contextlib import contextmanager
from pathlib import Path

Expand All @@ -22,7 +21,7 @@
)
from pygmt.helpers import GMTTempFile

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = Path(__file__).parent / "data" / "points.txt"


@contextmanager
Expand Down Expand Up @@ -136,11 +135,10 @@ def test_call_module():
"""
Run a command to see if call_module works.
"""
data_fname = os.path.join(TEST_DATA_DIR, "points.txt")
out_fname = "test_call_module.txt"
with clib.Session() as lib:
with GMTTempFile() as out_fname:
lib.call_module("info", f"{data_fname} -C ->{out_fname.name}")
lib.call_module("info", f"{POINTS_DATA} -C ->{out_fname.name}")
assert Path(out_fname.name).stat().st_size > 0
output = out_fname.read().strip()
assert output == "11.5309 61.7074 -2.9289 7.8648 0.1412 0.9338"
Expand Down
5 changes: 2 additions & 3 deletions pygmt/tests/test_clib_virtualfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Test the C API functions related to virtual files.
"""

import os
from importlib.util import find_spec
from itertools import product
from pathlib import Path

import numpy as np
import pandas as pd
Expand All @@ -15,8 +15,7 @@
from pygmt.helpers import GMTTempFile
from pygmt.tests.test_clib import mock

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
POINTS_DATA = Path(__file__).parent / "data" / "points.txt"


@pytest.fixture(scope="module", name="data")
Expand Down
5 changes: 2 additions & 3 deletions pygmt/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Test Figure.contour.
"""

import os
from pathlib import Path

import numpy as np
import pandas as pd
import pytest
import xarray as xr
from pygmt import Figure

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
POINTS_DATA = Path(__file__).parent / "data" / "points.txt"


@pytest.fixture(scope="module", name="data")
Expand Down
16 changes: 7 additions & 9 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import importlib
import os
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -82,10 +81,8 @@ def test_figure_savefig_exists():
fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af")
prefix = "test_figure_savefig_exists"
for fmt in "bmp eps jpg jpeg pdf png ppm tif PNG JPG JPEG Png".split():
fname = f"{prefix}.{fmt}"
fname = Path(f"{prefix}.{fmt}")
fig.savefig(fname)

fname = Path(fname)
assert fname.exists()
fname.unlink()

Expand Down Expand Up @@ -201,10 +198,10 @@ def test_figure_savefig_transparent():
with pytest.raises(GMTInvalidInput):
fig.savefig(fname, transparent=True)
# png should not raise an error
fname = f"{prefix}.png"
fname = Path(f"{prefix}.png")
fig.savefig(fname, transparent=True)
assert os.path.exists(fname)
os.remove(fname)
assert fname.exists()
fname.unlink()


def test_figure_savefig_filename_with_spaces():
Expand All @@ -215,8 +212,9 @@ def test_figure_savefig_filename_with_spaces():
fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True)
with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile:
fig.savefig(fname=imgfile.name)
assert r"\040" not in os.path.abspath(imgfile.name)
assert Path(imgfile.name).stat().st_size > 0
imgpath = Path(imgfile.name).resolve()
assert r"\040" not in str(imgpath)
assert imgpath.stat().st_size > 0


def test_figure_savefig():
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_grd2cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Test pygmt.grd2cpt.
"""

import os
from pathlib import Path

import pytest
from pygmt import Figure, grd2cpt
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_grd2cpt_output_to_cpt_file(grid):
"""
with GMTTempFile(suffix=".cpt") as cptfile:
grd2cpt(grid=grid, output=cptfile.name)
assert os.path.getsize(cptfile.name) > 0
assert Path(cptfile.name).stat().st_size > 0


def test_grd2cpt_unrecognized_data_type():
Expand Down
5 changes: 2 additions & 3 deletions pygmt/tests/test_grdcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Test Figure.grdcontour.
"""

import os
from pathlib import Path

import numpy as np
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers.testing import load_static_earth_relief

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
TEST_CONTOUR_FILE = os.path.join(TEST_DATA_DIR, "contours.txt")
TEST_CONTOUR_FILE = Path(__file__).parent / "data" / "contours.txt"


@pytest.fixture(scope="module", name="grid")
Expand Down
Loading

0 comments on commit 0cdb87d

Please sign in to comment.