Skip to content

Commit de86028

Browse files
committed
Add files download to CLI
1 parent d602113 commit de86028

3 files changed

Lines changed: 85 additions & 21 deletions

File tree

opus/cli/cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ def image(argv=None, api=api):
8686
if args.download or args.output is not None:
8787
return img.download(out=args.output)
8888
return img.url
89+
90+
def files(argv=None, api=api):
91+
'''OPUS SETI API files entry point'''
92+
parser = argparse.ArgumentParser(description='Get files for a single observation from OPUS-SETI API')
93+
parser.add_argument('ring_obs_id', help='Valid ring_obs_id')
94+
parser.add_argument('-f', '--fmt', help='File format', default=None, nargs=2, metavar=('GROUP','FORMAT'))
95+
parser.add_argument('-d', '--download', help='Download the image', action='store_true')
96+
parser.add_argument('-o', '--output', help='Output folder/filename for download', default=None)
97+
args, others = parser.parse_known_args(argv)
98+
99+
files = api.file(args.ring_obs_id)
100+
if args.fmt is not None:
101+
files = files[args.fmt[0]][args.fmt[1]]
102+
if args.download or args.output is not None:
103+
return files.download(out=args.output)
104+
return repr(files)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ console_scripts =
3636
opus = opus.cli:data
3737
opus-meta = opus.cli:metadata
3838
opus-preview = opus.cli:image
39+
opus-files = opus.cli:files
3940
opus-vims = opus.cli.cassini:vims

tests/test_cli.py

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@
55
import os
66

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

1010

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

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

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

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

26-
assert read(['--foo', '123', 'abc']) == {'foo': '123'}
27-
assert read(['--foo', '123', '--bar']) == {'foo': '123'}
28-
assert read(['--foo', '--bar', '123']) == {'bar': '123'}
29-
assert read(['123','--foo', '--bar']) == {}
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()) == {}
3030

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

@@ -39,7 +39,7 @@ def test_cli_data(api):
3939
'http://localhost/data.json',
4040
body=json)
4141

42-
argv = ['--limit', '100']
42+
argv = '--limit 100'.split()
4343
resp = data(argv, api=api)
4444

4545
assert len(responses.calls) == 1
@@ -64,7 +64,7 @@ def test_cli_data_limite_none(api):
6464
'http://localhost/data.json',
6565
body=json)
6666

67-
argv = ['--all', '--planet', 'Saturn']
67+
argv = '--all --planet Saturn'.split()
6868
resp = data(argv, api=api)
6969

7070
assert len(responses.calls) == 2
@@ -94,7 +94,7 @@ def test_cli_metadata(api):
9494
'http://localhost/metadata/S_IMG_CO_ISS_1459551972_N.json',
9595
body=json)
9696

97-
argv = ['S_IMG_CO_ISS_1459551972_N']
97+
argv = 'S_IMG_CO_ISS_1459551972_N'.split()
9898
resp = metadata(argv, api=api)
9999

100100
assert len(responses.calls) == 1
@@ -111,7 +111,7 @@ def test_cli_image(api):
111111
'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json',
112112
body=img)
113113

114-
argv = ['S_IMG_CO_ISS_1459551972_N', '--size', 'med']
114+
argv = 'S_IMG_CO_ISS_1459551972_N --size med'.split()
115115
resp = image(argv, api=api)
116116

117117
assert len(responses.calls) == 1
@@ -123,22 +123,22 @@ def test_cli_image(api):
123123

124124
@responses.activate
125125
def test_cli_image_download(api):
126-
fname = 'N1459551972_1_med.jpg'
127-
jpg = 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
128-
129126
img = open('tests/api/image/med/S_IMG_CO_ISS_1459551972_N.json', 'r').read()
130127
responses.add(responses.GET,
131128
'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json',
132129
body=img)
133130

131+
fname = 'N1459551972_1_med.jpg'
132+
jpg = 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
133+
134134
with open('tests/api/image/med/'+fname, 'rb') as img:
135135
responses.add(responses.GET, jpg,
136136
body=img.read(), status=200,
137137
content_type='image/jpeg',
138138
stream=True
139139
)
140140

141-
argv = ['S_IMG_CO_ISS_1459551972_N', '--output', 'tests/test.jpg']
141+
argv = 'S_IMG_CO_ISS_1459551972_N --output tests/test.jpg'.split()
142142
resp = image(argv, api=api)
143143

144144
assert len(responses.calls) == 2
@@ -148,3 +148,50 @@ def test_cli_image_download(api):
148148
assert resp == 'tests/test.jpg'
149149
assert os.path.isfile('tests/test.jpg')
150150
os.remove('tests/test.jpg')
151+
152+
153+
@responses.activate
154+
def test_cli_files(api):
155+
json = open('tests/api/files/S_IMG_CO_ISS_1459551972_N.json', 'r').read()
156+
responses.add(responses.GET,
157+
'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json',
158+
body=json)
159+
160+
argv = 'S_IMG_CO_ISS_1459551972_N'.split()
161+
resp = files(argv, api=api)
162+
163+
assert len(responses.calls) == 1
164+
assert responses.calls[0].request.url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
165+
assert responses.calls[0].response.text == json
166+
167+
assert 'OPUS API Files for observation: S_IMG_CO_ISS_1459551972_N' in resp
168+
assert 'LBL: https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.LBL' in resp
169+
170+
171+
@responses.activate
172+
def test_cli_files_downlad(api):
173+
json = open('tests/api/files/S_IMG_CO_ISS_1459551972_N.json', 'r').read()
174+
responses.add(responses.GET,
175+
'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json',
176+
body=json)
177+
178+
fname = 'N1459551972_1.LBL'
179+
lbl = 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.LBL'
180+
181+
with open('tests/data/'+fname, 'rb') as img:
182+
responses.add(responses.GET, lbl,
183+
body=img.read(), status=200,
184+
content_type='image/txt',
185+
stream=True
186+
)
187+
188+
argv = 'S_IMG_CO_ISS_1459551972_N -f RAW_IMAGE LBL --output tests/test.lbl'.split()
189+
resp = files(argv, api=api)
190+
191+
assert len(responses.calls) == 2
192+
assert responses.calls[0].request.url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
193+
assert responses.calls[1].request.url == lbl
194+
195+
assert resp == 'tests/test.lbl'
196+
assert os.path.isfile('tests/test.lbl')
197+
os.remove('tests/test.lbl')

0 commit comments

Comments
 (0)