From 3417f89d7475bdb97169615689b730303142dbc8 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 9 Oct 2023 13:53:11 +0800 Subject: [PATCH 1/2] pygmt.which: Fix the bug when passing multiple files --- pygmt/src/which.py | 6 ++++-- pygmt/tests/test_which.py | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pygmt/src/which.py b/pygmt/src/which.py index d20059f0492..3fdcafa91f5 100644 --- a/pygmt/src/which.py +++ b/pygmt/src/which.py @@ -6,14 +6,13 @@ GMTTempFile, build_arg_string, fmt_docstring, - kwargs_to_strings, + is_nonstr_iter, use_alias, ) @fmt_docstring @use_alias(G="download", V="verbose") -@kwargs_to_strings(fname="sequence_space") def which(fname, **kwargs): r""" Find the full path to specified files. @@ -63,6 +62,9 @@ def which(fname, **kwargs): FileNotFoundError If the file is not found. """ + if is_nonstr_iter(fname): # Got a list of files + fname = " ".join(fname) + with GMTTempFile() as tmpfile: with Session() as lib: lib.call_module( diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 15cb48963c3..38da23c36e1 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -1,7 +1,7 @@ """ Test pygmt.which. """ -import os +from pathlib import Path import pytest from pygmt import which @@ -14,8 +14,8 @@ def test_which(): """ for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]: cached_file = which(f"@{fname}", download="c") - assert os.path.exists(cached_file) - assert os.path.basename(cached_file) == fname + assert Path(cached_file).exists() + assert Path(cached_file).name == fname def test_which_multiple(): @@ -23,10 +23,10 @@ def test_which_multiple(): Make sure `which` returns file paths for multiple @files correctly. """ filenames = ["ridge.txt", "tut_ship.xyz"] - cached_files = which(fname=[f"@{fname}" for fname in filenames], download="c") + cached_files = which([f"@{fname}" for fname in filenames], download="c") for cached_file in cached_files: - assert os.path.exists(cached_file) - assert os.path.basename(cached_file) in filenames + assert Path(cached_file).exists() + assert Path(cached_file).name in filenames def test_which_fails(): From a8943526b9f8ce3aa043b18998998d2e84440904 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 15 Oct 2023 16:13:31 +0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_which.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_which.py b/pygmt/tests/test_which.py index 38da23c36e1..b482b247b4d 100644 --- a/pygmt/tests/test_which.py +++ b/pygmt/tests/test_which.py @@ -13,7 +13,7 @@ def test_which(): Make sure `which` returns file paths for @files correctly without errors. """ for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]: - cached_file = which(f"@{fname}", download="c") + cached_file = which(fname=f"@{fname}", download="c") assert Path(cached_file).exists() assert Path(cached_file).name == fname