Skip to content

Commit e8530db

Browse files
committed
Add Field search to CLI
1 parent de86028 commit e8530db

3 files changed

Lines changed: 123 additions & 24 deletions

File tree

opus/cli/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,20 @@ def files(argv=None, api=api):
102102
if args.download or args.output is not None:
103103
return files.download(out=args.output)
104104
return repr(files)
105+
106+
def field(argv=None, api=api):
107+
'''OPUS SETI API field entry point'''
108+
parser = argparse.ArgumentParser(description='Get information about field for OPUS-SETI API')
109+
parser.add_argument('field', help='Field name', nargs='?', metavar='name')
110+
parser.add_argument('--all', help='List all available fields', action='store_true')
111+
args, others = parser.parse_known_args(argv)
112+
113+
if args.all:
114+
return api.fields()
115+
try:
116+
return api.field(args.field)
117+
except KeyError:
118+
try:
119+
return api.fields()[args.field]
120+
except KeyError:
121+
return 'Unknown field. To list all the available fields, run: `opus-field --all`'

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ console_scripts =
3737
opus-meta = opus.cli:metadata
3838
opus-preview = opus.cli:image
3939
opus-files = opus.cli:files
40-
opus-vims = opus.cli.cassini:vims
40+
opus-field = opus.cli:field
41+
cassini-vims = opus.cli.cassini:vims

tests/test_cli.py

Lines changed: 104 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@
55
import os
66

77
from opus.api import API
8-
from opus.cli import read, data, metadata, image, files
8+
import opus.cli as cli
99

1010

1111
@pytest.fixture
1212
def api():
1313
return API('http://localhost/')
1414

1515
def test_read():
16-
assert read('foo'.split()) == {}
17-
assert read('--foo'.split()) == {}
16+
assert cli.read('foo'.split()) == {}
17+
assert cli.read('--foo'.split()) == {}
1818

19-
assert read('--foo bar'.split()) == {'foo': 'bar'}
20-
assert read('--foo=bar'.split()) == {'foo': 'bar'}
21-
assert read('--foo --bar'.split()) == {}
19+
assert cli.read('--foo bar'.split()) == {'foo': 'bar'}
20+
assert cli.read('--foo=bar'.split()) == {'foo': 'bar'}
21+
assert cli.read('--foo --bar'.split()) == {}
2222

23-
assert read('--foo=bar 123'.split()) == {'foo': 'bar'}
24-
assert read('--foo=123 --bar abc'.split()) == {'foo': '123', 'bar': 'abc'}
23+
assert cli.read('--foo=bar 123'.split()) == {'foo': 'bar'}
24+
assert cli.read('--foo=123 --bar abc'.split()) == {'foo': '123', 'bar': 'abc'}
2525

26-
assert read('--foo 123 abc'.split()) == {'foo': '123'}
27-
assert read('--foo 123 --bar'.split()) == {'foo': '123'}
28-
assert read('--foo --bar 123'.split()) == {'bar': '123'}
29-
assert read('123 --foo --bar'.split()) == {}
26+
assert cli.read('--foo 123 abc'.split()) == {'foo': '123'}
27+
assert cli.read('--foo 123 --bar'.split()) == {'foo': '123'}
28+
assert cli.read('--foo --bar 123'.split()) == {'bar': '123'}
29+
assert cli.read('123 --foo --bar'.split()) == {}
3030

31-
assert read("--foo='bar'".split()) == {'foo': 'bar'}
32-
assert read(["--foo='123 abc'"]) == {'foo': '123 abc'}
33-
assert read(["--foo", "'123 abc'"]) == {'foo': '123 abc'}
31+
assert cli.read("--foo='bar'".split()) == {'foo': 'bar'}
32+
assert cli.read(["--foo='123 abc'"]) == {'foo': '123 abc'}
33+
assert cli.read(["--foo", "'123 abc'"]) == {'foo': '123 abc'}
3434

3535
@responses.activate
3636
def test_cli_data(api):
@@ -40,7 +40,7 @@ def test_cli_data(api):
4040
body=json)
4141

4242
argv = '--limit 100'.split()
43-
resp = data(argv, api=api)
43+
resp = cli.data(argv, api=api)
4444

4545
assert len(responses.calls) == 1
4646
if six.PY3:
@@ -65,7 +65,7 @@ def test_cli_data_limite_none(api):
6565
body=json)
6666

6767
argv = '--all --planet Saturn'.split()
68-
resp = data(argv, api=api)
68+
resp = cli.data(argv, api=api)
6969

7070
assert len(responses.calls) == 2
7171
if six.PY3:
@@ -83,7 +83,7 @@ def test_data_argv_none(api):
8383
responses.add(responses.GET, 'http://localhost/data.json',body='{}')
8484
argv = ['']
8585
with pytest.raises(SystemExit):
86-
resp = data(argv, api=api)
86+
resp = cli.data(argv, api=api)
8787
assert len(responses.calls) == 0
8888

8989

@@ -95,7 +95,7 @@ def test_cli_metadata(api):
9595
body=json)
9696

9797
argv = 'S_IMG_CO_ISS_1459551972_N'.split()
98-
resp = metadata(argv, api=api)
98+
resp = cli.metadata(argv, api=api)
9999

100100
assert len(responses.calls) == 1
101101
assert responses.calls[0].request.url == 'http://localhost/metadata/S_IMG_CO_ISS_1459551972_N.json'
@@ -112,7 +112,7 @@ def test_cli_image(api):
112112
body=img)
113113

114114
argv = 'S_IMG_CO_ISS_1459551972_N --size med'.split()
115-
resp = image(argv, api=api)
115+
resp = cli.image(argv, api=api)
116116

117117
assert len(responses.calls) == 1
118118
assert responses.calls[0].request.url == 'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json'
@@ -139,7 +139,7 @@ def test_cli_image_download(api):
139139
)
140140

141141
argv = 'S_IMG_CO_ISS_1459551972_N --output tests/test.jpg'.split()
142-
resp = image(argv, api=api)
142+
resp = cli.image(argv, api=api)
143143

144144
assert len(responses.calls) == 2
145145
assert responses.calls[0].request.url == 'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json'
@@ -158,7 +158,7 @@ def test_cli_files(api):
158158
body=json)
159159

160160
argv = 'S_IMG_CO_ISS_1459551972_N'.split()
161-
resp = files(argv, api=api)
161+
resp = cli.files(argv, api=api)
162162

163163
assert len(responses.calls) == 1
164164
assert responses.calls[0].request.url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
@@ -186,7 +186,7 @@ def test_cli_files_downlad(api):
186186
)
187187

188188
argv = 'S_IMG_CO_ISS_1459551972_N -f RAW_IMAGE LBL --output tests/test.lbl'.split()
189-
resp = files(argv, api=api)
189+
resp = cli.files(argv, api=api)
190190

191191
assert len(responses.calls) == 2
192192
assert responses.calls[0].request.url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
@@ -195,3 +195,84 @@ def test_cli_files_downlad(api):
195195
assert resp == 'tests/test.lbl'
196196
assert os.path.isfile('tests/test.lbl')
197197
os.remove('tests/test.lbl')
198+
199+
200+
@responses.activate
201+
def test_cli_field(api):
202+
json = open('tests/api/fields/target.json', 'r').read()
203+
responses.add(responses.GET,
204+
'http://localhost/fields/target.json',
205+
body=json)
206+
207+
argv = 'target'.split()
208+
resp = cli.field(argv, api=api)
209+
210+
assert len(responses.calls) == 1
211+
assert responses.calls[0].request.url == 'http://localhost/fields/target.json'
212+
assert responses.calls[0].response.text == json
213+
214+
assert resp.label == 'Intended Target Name'
215+
216+
217+
@responses.activate
218+
def test_cli_field_all(api):
219+
json = open('tests/api/fields.json', 'r').read()
220+
responses.add(responses.GET,
221+
'http://localhost/fields.json',
222+
body=json)
223+
224+
argv = '--all'.split()
225+
resp = cli.field(argv, api=api)
226+
227+
assert len(responses.calls) == 1
228+
assert responses.calls[0].request.url == 'http://localhost/fields.json'
229+
assert responses.calls[0].response.text == json
230+
231+
r = repr(resp)
232+
assert 'OPUS API list of all fields available (304):' in r
233+
assert 'GLOBAL' in r
234+
assert 'TITAN' in r
235+
236+
@responses.activate
237+
def test_cli_field_group(api):
238+
json = open('tests/api/fields.json', 'r').read()
239+
responses.add(responses.GET,
240+
'http://localhost/fields/TITAN.json',
241+
body='{}')
242+
responses.add(responses.GET,
243+
'http://localhost/fields.json',
244+
body=json)
245+
246+
argv = 'TITAN'.split()
247+
resp = cli.field(argv, api=api)
248+
249+
assert len(responses.calls) == 2
250+
assert responses.calls[0].request.url == 'http://localhost/fields/TITAN.json'
251+
assert responses.calls[0].response.text == '{}'
252+
assert responses.calls[1].request.url == 'http://localhost/fields.json'
253+
assert responses.calls[1].response.text == json
254+
255+
r = repr(resp)
256+
assert 'OPUS API Surface Geometry fields (31) for `Titan`:' in r
257+
assert 'Sub-Solar IAU West Longitude (subsolarIAUlongitude)' in r
258+
259+
@responses.activate
260+
def test_cli_field_err(api):
261+
json = open('tests/api/fields.json', 'r').read()
262+
responses.add(responses.GET,
263+
'http://localhost/fields/abc.json',
264+
body='{}')
265+
responses.add(responses.GET,
266+
'http://localhost/fields.json',
267+
body=json)
268+
269+
argv = 'abc'.split()
270+
resp = cli.field(argv, api=api)
271+
272+
assert len(responses.calls) == 2
273+
assert responses.calls[0].request.url == 'http://localhost/fields/abc.json'
274+
assert responses.calls[0].response.text == '{}'
275+
assert responses.calls[1].request.url == 'http://localhost/fields.json'
276+
assert responses.calls[1].response.text == json
277+
278+
assert resp == 'Unknown field. To list all the available fields, run: `opus-field --all`'

0 commit comments

Comments
 (0)