From 551b824c983c69eefa874c1236945e4d461980eb Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Mar 2021 00:38:29 -0400 Subject: [PATCH 1/3] Let Figure.savefig support filenames with spaces --- pygmt/figure.py | 3 +++ pygmt/tests/test_figure.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pygmt/figure.py b/pygmt/figure.py index acd448b9472..e897e6c810a 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -170,6 +170,9 @@ def psconvert(self, **kwargs): # Default cropping the figure to True if "A" not in kwargs: kwargs["A"] = "" + # allow for spaces in figure name + if kwargs.get("F"): + kwargs["F"] = f'"{kwargs.get("F")}"' with Session() as lib: lib.call_module("psconvert", build_arg_string(kwargs)) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 99cb7d8e73e..6bb136df53c 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -96,6 +96,18 @@ def test_figure_savefig_transparent(): os.remove(fname) +def test_figure_savefig_filename_with_spaces(): + """ + Check if savefig (or psconvert) supports filenames with spaces. + """ + fig = Figure() + fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) + fname = "test_figure_savefig filename with spaces.png" + fig.savefig(fname) + assert os.path.exists(fname) + os.remove(fname) + + def test_figure_savefig(): """ Check if the arguments being passed to psconvert are correct. From abaee2e9e5bb756566f83b310c6ed4e94538fef7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Mar 2021 22:18:11 -0400 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, 1 insertion(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index e897e6c810a..02b8c6a0777 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -171,8 +171,7 @@ def psconvert(self, **kwargs): if "A" not in kwargs: kwargs["A"] = "" # allow for spaces in figure name - if kwargs.get("F"): - kwargs["F"] = f'"{kwargs.get("F")}"' + kwargs["F"] = f'"{kwargs.get("F")}"' if kwargs.get("F") else None with Session() as lib: lib.call_module("psconvert", build_arg_string(kwargs)) From 4d86e7816cce4b6cf297278e56f20228ab976c4e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 12 May 2021 16:28:45 -0400 Subject: [PATCH 3/3] Rewrite the test using GMTTempFile --- pygmt/tests/test_figure.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index fa72d0926af..a01eecfd3c1 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -10,6 +10,7 @@ import pytest from pygmt import Figure, set_display from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile def test_figure_region(): @@ -102,10 +103,9 @@ def test_figure_savefig_filename_with_spaces(): """ fig = Figure() fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) - fname = "test_figure_savefig filename with spaces.png" - fig.savefig(fname) - assert os.path.exists(fname) - os.remove(fname) + with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: + fig.savefig(imgfile.name) + assert os.path.exists(imgfile.name) def test_figure_savefig():