Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pygmt/src/which.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
which - Find the full path to specified files.
"""
from pygmt.clib import Session
from pygmt.helpers import GMTTempFile, build_arg_string, fmt_docstring, use_alias
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
fmt_docstring,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@use_alias(G="download", V="verbose")
@kwargs_to_strings(fname="sequence_space")
def which(fname, **kwargs):
"""
Find the full path to specified files.
Expand All @@ -27,8 +34,8 @@ def which(fname, **kwargs):

Parameters
----------
fname : str
The file name that you want to check.
fname : str or list
One or more file names of any data type (grids, tables, etc.).
download : bool or str
If the file is downloadable and not found, we will try to download the
it. Use True or 'l' (default) to download to the current directory. Use
Comment on lines 39 to 41
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to update the download (-G) docstring to include the 'a' option (added in GMT 6.1 I think). Should it be done in this PR or a separate one?

Suggested change
download : bool or str
If the file is downloadable and not found, we will try to download the
it. Use True or 'l' (default) to download to the current directory. Use
download : bool or str
If the file is downloadable and not found, we will try to download
it. Use True or 'l' (default) to download to the current directory. Use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate one is better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate one is better.

I agree.

Expand All @@ -38,8 +45,8 @@ def which(fname, **kwargs):

Returns
-------
path : str
The path of the file, depending on the options used.
path : str or list
The path(s) to the file(s), depending on the options used.

Raises
------
Expand All @@ -52,5 +59,6 @@ def which(fname, **kwargs):
lib.call_module("which", arg_str)
path = tmpfile.read().strip()
if not path:
raise FileNotFoundError("File '{}' not found.".format(fname))
return path
_fname = fname.replace(" ", "', '")
raise FileNotFoundError(f"File(s) '{_fname}' not found.")
return path.split("\n") if "\n" in path else path
17 changes: 15 additions & 2 deletions pygmt/tests/test_which.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@

def test_which():
"""
Make sure which returns file paths for @files correctly without errors.
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")
assert os.path.exists(cached_file)
assert os.path.basename(cached_file) == fname


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")
for cached_file in cached_files:
assert os.path.exists(cached_file)
assert os.path.basename(cached_file) in filenames


def test_which_fails():
"""
which should fail with a FileNotFoundError.
Make sure `which` will fail with a FileNotFoundError.
"""
bogus_file = unique_name()
with pytest.raises(FileNotFoundError):
which(bogus_file)
with pytest.raises(FileNotFoundError):
which(fname=[f"{bogus_file}.nc", f"{bogus_file}.txt"])