|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | from pygmt.clib import Session
|
6 |
| -from pygmt.helpers import ( |
7 |
| - GMTTempFile, |
8 |
| - build_arg_string, |
9 |
| - fmt_docstring, |
10 |
| - kwargs_to_strings, |
11 |
| - use_alias, |
12 |
| -) |
| 6 | +from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias |
13 | 7 |
|
14 | 8 |
|
15 | 9 | @fmt_docstring
|
16 | 10 | @use_alias(G="download", V="verbose")
|
17 | 11 | @kwargs_to_strings(fname="sequence_space")
|
18 |
| -def which(fname, **kwargs): |
| 12 | +def which(fname, **kwargs) -> str | list[str]: |
19 | 13 | r"""
|
20 | 14 | Find the full path to specified files.
|
21 | 15 |
|
@@ -56,22 +50,27 @@ def which(fname, **kwargs):
|
56 | 50 |
|
57 | 51 | Returns
|
58 | 52 | -------
|
59 |
| - path : str or list |
| 53 | + path |
60 | 54 | The path(s) to the file(s), depending on the parameters used.
|
61 | 55 |
|
62 | 56 | Raises
|
63 | 57 | ------
|
64 | 58 | FileNotFoundError
|
65 | 59 | If the file is not found.
|
66 | 60 | """
|
67 |
| - with GMTTempFile() as tmpfile: |
68 |
| - with Session() as lib: |
| 61 | + with Session() as lib: |
| 62 | + with lib.virtualfile_out(kind="dataset") as vouttbl: |
69 | 63 | lib.call_module(
|
70 | 64 | module="which",
|
71 |
| - args=build_arg_string(kwargs, infile=fname, outfile=tmpfile.name), |
| 65 | + args=build_arg_string(kwargs, infile=fname, outfile=vouttbl), |
72 | 66 | )
|
73 |
| - path = tmpfile.read().strip() |
74 |
| - if not path: |
75 |
| - _fname = fname.replace(" ", "', '") |
76 |
| - raise FileNotFoundError(f"File(s) '{_fname}' not found.") |
77 |
| - return path.split("\n") if "\n" in path else path |
| 67 | + paths = lib.virtualfile_to_dataset(vfname=vouttbl, output_type="strings") |
| 68 | + |
| 69 | + match paths.size: |
| 70 | + case 0: |
| 71 | + _fname = fname.replace(" ", "', '") |
| 72 | + raise FileNotFoundError(f"File(s) '{_fname}' not found.") |
| 73 | + case 1: |
| 74 | + return paths[0] |
| 75 | + case _: |
| 76 | + return paths.tolist() |
0 commit comments