From 0e24a5238aac5c9c1ab53ad40b967a5d7e9b3748 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 14 Oct 2022 13:30:03 +0800 Subject: [PATCH 1/3] Figure.savefig: Raise a FileNotFoundError if the parent directory doesn't exist --- pygmt/figure.py | 7 +++++++ pygmt/tests/test_figure.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/pygmt/figure.py b/pygmt/figure.py index 435bb26efd7..05f04fe9dbd 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -4,6 +4,7 @@ import base64 import os import warnings +from pathlib import Path from tempfile import TemporaryDirectory try: @@ -241,6 +242,12 @@ def psconvert(self, icc_gray=False, **kwargs): else: kwargs["N"] += "+i" + # check if the parent directory exists + if kwargs.get("F") and not Path(kwargs.get("F")).parent.exists(): + raise FileNotFoundError( + f"No such directory: '{Path(kwargs.get('F')).parent}'" + ) + # Manually handle prefix -F argument so spaces aren't converted to \040 # by build_arg_string function. For more information, see # https://github.com/GenericMappingTools/pygmt/pull/1487 diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 2d5949eaddb..900c70d74ad 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -89,6 +89,17 @@ def test_figure_savefig_exists(): os.remove(fname) +def test_figure_savefig_directory_nonexists(): + """ + Make sure that Figure.savefig() raises a FileNotFoundError when the parent + directory doesn't exist. + """ + fig = Figure() + fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af") + with pytest.raises(FileNotFoundError, match="No such directory:"): + fig.savefig("a-nonexist-directory/test_figure_savefig_directory_nonexists.png") + + def test_figure_savefig_unknown_extension(): """ Check that an error is raised when an unknown extension is passed. From 052c9e304a7f533a0ea82ee08841da16389e6a35 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 28 Oct 2022 13:36:20 +0800 Subject: [PATCH 2/3] Update pygmt/figure.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/figure.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 39b7645a00b..9e5d76ffe7c 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -244,8 +244,9 @@ def psconvert(self, icc_gray=False, **kwargs): # check if the parent directory exists if kwargs.get("F") and not Path(kwargs.get("F")).parent.exists(): + directory = Path(kwargs.get("F")).parent raise FileNotFoundError( - f"No such directory: '{Path(kwargs.get('F')).parent}'" + f"No such directory: '{directory}', please create it first." ) # Manually handle prefix -F argument so spaces aren't converted to \040 From eb136ace2e5d82627b70aca8b59e89a238d6e04a Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 28 Oct 2022 13:40:56 +0800 Subject: [PATCH 3/3] Move the codes after checking if prefix is valid --- pygmt/figure.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 9e5d76ffe7c..76d0b617bee 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -242,13 +242,6 @@ def psconvert(self, icc_gray=False, **kwargs): else: kwargs["N"] += "+i" - # check if the parent directory exists - if kwargs.get("F") and not Path(kwargs.get("F")).parent.exists(): - directory = Path(kwargs.get("F")).parent - raise FileNotFoundError( - f"No such directory: '{directory}', please create it first." - ) - # Manually handle prefix -F argument so spaces aren't converted to \040 # by build_arg_string function. For more information, see # https://github.com/GenericMappingTools/pygmt/pull/1487 @@ -259,6 +252,13 @@ def psconvert(self, icc_gray=False, **kwargs): ) prefix_arg = f'-F"{prefix}"' + # check if the parent directory exists + prefix_path = Path(prefix).parent + if not prefix_path.exists(): + raise FileNotFoundError( + f"No such directory: '{prefix_path}', please create it first." + ) + with Session() as lib: lib.call_module( module="psconvert", args=f"{prefix_arg} {build_arg_string(kwargs)}"