2
2
Non-plot GMT modules.
3
3
"""
4
4
from .clib import LibGMT
5
- from .helpers import build_arg_string , fmt_docstring , GMTTempFile
5
+ from .helpers import build_arg_string , fmt_docstring , GMTTempFile , use_alias
6
6
7
7
8
8
@fmt_docstring
@@ -25,3 +25,55 @@ def info(fname, **kwargs):
25
25
with LibGMT () as lib :
26
26
lib .call_module ('info' , arg_str )
27
27
return tmpfile .read ()
28
+
29
+
30
+ @fmt_docstring
31
+ @use_alias (G = 'download' )
32
+ def which (fname , ** kwargs ):
33
+ """
34
+ Find the full path to specified files.
35
+
36
+ Reports the full paths to the files given through *fname*. We look for
37
+ the file in (1) the current directory, (2) in $GMT_USERDIR (if defined),
38
+ (3) in $GMT_DATADIR (if defined), or (4) in $GMT_CACHEDIR (if defined).
39
+
40
+ *fname* can also be a downloadable file (either a full URL, a
41
+ `@file` special file for downloading from the GMT Site Cache, or
42
+ `@earth_relief_*` topography grids). In these cases, use option *download*
43
+ to set the desired behavior. If *download* is not used (or False), the file
44
+ will not be found.
45
+
46
+ {gmt_module_docs}
47
+
48
+ {aliases}
49
+
50
+ Parameters
51
+ ----------
52
+ fname : str
53
+ The file name that you want to check.
54
+ G : bool or str
55
+ If the file is downloadable and not found, we will try to download the
56
+ it. Use True or 'l' (default) to download to the current directory. Use
57
+ 'c' to place in the user cache directory or 'u' user data directory
58
+ instead.
59
+
60
+ Returns
61
+ -------
62
+ path : str
63
+ The path of the file, depending on the options used.
64
+
65
+ Raises
66
+ ------
67
+ FileNotFoundError
68
+ If the file is not found.
69
+
70
+ """
71
+ with GMTTempFile () as tmpfile :
72
+ arg_str = ' ' .join ([fname , build_arg_string (kwargs ),
73
+ "->" + tmpfile .name ])
74
+ with LibGMT () as lib :
75
+ lib .call_module ('which' , arg_str )
76
+ path = tmpfile .read ().strip ()
77
+ if not path :
78
+ raise FileNotFoundError ("File '{}' not found." .format (fname ))
79
+ return path
0 commit comments