Skip to content

Commit 192ae7a

Browse files
committed
Wrapper for the 'which' module
Basic wrapper that only defines a single alias for the -G option (download the file). Fixes #92
1 parent 21e9d3c commit 192ae7a

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*~
33
*.so
44
*.pyd
5+
.pytest_cache/
56
build/
67
dist/
78
.*.swp

gmt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Import modules to make the high-level GMT Python API
1818
from .session_management import begin as _begin, end as _end
1919
from .figure import Figure
20-
from .modules import info
20+
from .modules import info, which
2121

2222

2323
# Get the version number through versioneer

gmt/modules.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Non-plot GMT modules.
33
"""
44
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
66

77

88
@fmt_docstring
@@ -25,3 +25,55 @@ def info(fname, **kwargs):
2525
with LibGMT() as lib:
2626
lib.call_module('info', arg_str)
2727
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

gmt/tests/test_which.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Tests for gmt.which
3+
"""
4+
import os
5+
6+
import pytest
7+
8+
from .. import which
9+
from ..helpers import unique_name
10+
11+
12+
def test_which():
13+
"Make sure which returns file paths for @files correctly without errors"
14+
for fname in 'tut_quakes.ngdc tut_bathy.nc'.split():
15+
cached_file = which('@{}'.format(fname), download='c')
16+
assert os.path.exists(cached_file)
17+
assert os.path.basename(cached_file) == fname
18+
19+
20+
def test_which_fails():
21+
"which should fail with a FileNotFoundError"
22+
bogus_file = unique_name()
23+
with pytest.raises(FileNotFoundError):
24+
which(bogus_file)

0 commit comments

Comments
 (0)