Skip to content

Commit fc0e01a

Browse files
committed
Add entry_points
1 parent 28e9b9b commit fc0e01a

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

opus/cli.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import argparse
4+
5+
from .api import API
6+
7+
api = API()
8+
9+
def read(others):
10+
'''Extract optional arguments'''
11+
out = {}
12+
key = None
13+
for value in others:
14+
value = value.replace("'", '')
15+
if len(value) > 2:
16+
if value[:2] == '--':
17+
key = value[2:]
18+
value = None
19+
if '=' in key:
20+
key, value = key.split('=', 1)
21+
if key and value:
22+
out[key] = value
23+
key = None
24+
return out
25+
26+
27+
def data(argv=None, desc='Get Data from OPUS-SETI API', args=[], defaults={}, api=api):
28+
'''OPUS SETI API data entry point'''
29+
parser = argparse.ArgumentParser(description=desc)
30+
31+
parser.add_argument('-a', '--all', action='store_true',
32+
help='Get all the output at once (disable: --limit/--page)')
33+
parser.add_argument('-l', '--limit', default=10, type=int,
34+
help='Output limits (default: 10)')
35+
parser.add_argument('-p', '--page', default=1, type=int,
36+
help='Page offset (default: 1)')
37+
parser.add_argument('--field', help='Field key and value (sep. ` ` or `=`)', metavar='VALUE')
38+
39+
for arg in args:
40+
for key, msg in arg.items():
41+
parser.add_argument(key, **msg)
42+
43+
# Parse default args
44+
args, others = parser.parse_known_args(argv)
45+
46+
# Set defaults optiona values
47+
for key, value in defaults.items():
48+
setattr(args, key, value)
49+
50+
# Read other args
51+
for key, value in read(others).items():
52+
setattr(args, key, value)
53+
54+
# Disble limit if flag `all` is present
55+
if args.all:
56+
setattr(args, 'limit', None)
57+
delattr(args, 'page')
58+
delattr(args, 'all')
59+
delattr(args, 'field')
60+
61+
if args == argparse.Namespace(limit=10, page=1) or args == argparse.Namespace(limit=None):
62+
parser.print_help()
63+
os.sys.exit(2)
64+
65+
return api.data(**vars(args))
66+
67+
68+
def vims(argv=None, api=api):
69+
'''Cassini VIMS data entry point'''
70+
return data(argv, desc='Get Cassini VIMS Images and Cubes from OPUS API',
71+
args=[{'surfacegeometrytargetname': {'help': 'VIMS target name', 'metavar': 'TARGET_NAME'}},
72+
{'--cols': {'default': 'ringobsid,target,revno,time1,primaryfilespec,channel',
73+
'help': 'Output columns (default: `ringobsid,target,revno,time1,primaryfilespec,channel`)'}}],
74+
defaults={'instrumentid': 'Cassini+VIMS', 'typeid': 'Image,Cube'}, api=api)

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ universal = 1
3030

3131
[tool:pytest]
3232
addopts = -vv --cov-report term-missing --cov-report html --cov-report xml
33+
34+
[entry_points]
35+
console_scripts =
36+
opus = opus.cli:data
37+
opus-vims = opus.cli:vims

tests/api/data_vims.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"count": 10, "labels": ["Ring Observation ID", "Intended Target Name", "Saturn Orbit Number", "Observation Start Time (UTC)", "Primary File Spec", "Channel"], "order": "obs_general.time1", "limit": 10, "columns": ["Ring Observation ID", "Intended Target Name", "Saturn Orbit Number", "Observation Start Time (UTC)", "Primary File Spec", "Channel"], "page": [["S_CUBE_CO_VIMS_1463887830_IR", "TITAN", "000", "2004-05-22T03:06:38.211", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887830_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887846_IR", "TITAN", "000", "2004-05-22T03:06:54.202", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887846_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887879_IR", "TITAN", "000", "2004-05-22T03:07:27.210", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887879_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887895_IR", "TITAN", "000", "2004-05-22T03:07:43.201", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887895_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887928_IR", "TITAN", "000", "2004-05-22T03:08:16.211", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887928_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887944_IR", "TITAN", "000", "2004-05-22T03:08:32.202", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887944_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887977_IR", "TITAN", "000", "2004-05-22T03:09:05.211", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887977_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463887993_IR", "TITAN", "000", "2004-05-22T03:09:21.201", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463887993_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463888026_IR", "TITAN", "000", "2004-05-22T03:09:54.206", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463888026_1.QUB", "IR"], ["S_CUBE_CO_VIMS_1463888042_IR", "TITAN", "000", "2004-05-22T03:10:10.201", "COVIMS_0004/data/2004136T030633_2004143T032556/v1463888042_1.QUB", "IR"]], "page_no": 1}

tests/test_cli.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
import responses
4+
import six
5+
6+
from opus.api import API
7+
from opus.cli import read, data, vims
8+
9+
10+
@pytest.fixture
11+
def api():
12+
return API('http://localhost/')
13+
14+
def test_read():
15+
assert read(['foo']) == {}
16+
assert read(['--foo']) == {}
17+
18+
assert read(['--foo', 'bar']) == {'foo': 'bar'}
19+
assert read(['--foo=bar']) == {'foo': 'bar'}
20+
assert read(['--foo', '--bar']) == {}
21+
22+
assert read(['--foo=bar', '123']) == {'foo': 'bar'}
23+
assert read(['--foo=123', '--bar', 'abc']) == {'foo': '123', 'bar': 'abc'}
24+
25+
assert read(['--foo', '123', 'abc']) == {'foo': '123'}
26+
assert read(['--foo', '123', '--bar']) == {'foo': '123'}
27+
assert read(['--foo', '--bar', '123']) == {'bar': '123'}
28+
assert read(['123','--foo', '--bar']) == {}
29+
30+
assert read(["--foo='bar'"]) == {'foo': 'bar'}
31+
assert read(["--foo='123 abc'"]) == {'foo': '123 abc'}
32+
assert read(["--foo", "'123 abc'"]) == {'foo': '123 abc'}
33+
34+
@responses.activate
35+
def test_cli_data(api):
36+
json = open('tests/api/data.json', 'r').read()
37+
responses.add(responses.GET,
38+
'http://localhost/data.json',
39+
body=json)
40+
41+
argv = ['--limit', '100']
42+
resp = data(argv, api=api)
43+
44+
assert len(responses.calls) == 1
45+
if six.PY3:
46+
assert responses.calls[0].request.url == 'http://localhost/data.json?limit=100&page=1'
47+
else:
48+
assert 'limit=100' in responses.calls[0].request.url
49+
assert 'page=1' in responses.calls[0].request.url
50+
51+
assert responses.calls[0].response.text == json
52+
assert len(resp) == 10
53+
54+
@responses.activate
55+
def test_cli_data_limite_none(api):
56+
result_count = open('tests/api/meta/result_count.json', 'r').read()
57+
responses.add(responses.GET,
58+
'http://localhost/meta/result_count.json',
59+
body=result_count)
60+
61+
json = open('tests/api/data_all.json', 'r').read()
62+
responses.add(responses.GET,
63+
'http://localhost/data.json',
64+
body=json)
65+
66+
argv = ['--all', '--planet', 'Saturn']
67+
resp = data(argv, api=api)
68+
69+
assert len(responses.calls) == 2
70+
if six.PY3:
71+
assert responses.calls[0].request.url == 'http://localhost/meta/result_count.json?planet=Saturn'
72+
assert responses.calls[1].request.url == 'http://localhost/data.json?planet=Saturn&limit=1591'
73+
else:
74+
assert 'limit=1591' in responses.calls[1].request.url
75+
76+
assert responses.calls[1].response.text == json
77+
assert len(resp) == 1591
78+
79+
80+
@responses.activate
81+
def test_data_argv_none(api):
82+
responses.add(responses.GET, 'http://localhost/data.json',body='{}')
83+
argv = ['']
84+
with pytest.raises(SystemExit):
85+
resp = data(argv, api=api)
86+
assert len(responses.calls) == 0
87+
88+
89+
@responses.activate
90+
def test_cli_vims(api):
91+
json = open('tests/api/data_vims.json', 'r').read()
92+
responses.add(responses.GET,
93+
'http://localhost/data.json',
94+
body=json)
95+
96+
argv = ['TITAN']
97+
resp = vims(argv, api=api)
98+
99+
assert len(responses.calls) == 1
100+
if six.PY3:
101+
assert responses.calls[0].request.url == 'http://localhost/data.json?surfacegeometrytargetname=TITAN&cols=ringobsid,target,revno,time1,primaryfilespec,channel&instrumentid=Cassini+VIMS&typeid=Image,Cube&limit=10&page=1'
102+
else:
103+
assert 'surfacegeometrytargetname=TITAN' in responses.calls[0].request.url
104+
105+
assert responses.calls[0].response.text == json
106+
assert len(resp) == 10

0 commit comments

Comments
 (0)