Skip to content

Commit

Permalink
Merge pull request #123 from developmentseed/json
Browse files Browse the repository at this point in the history
adds bare json support for search #96
  • Loading branch information
Scisco committed Oct 27, 2015
2 parents 8f97f06 + 973abe2 commit a4b1dcc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Commands
-c CLOUD, --cloud CLOUD
Maximum cloud percentage. Default: 20 perct

--json Returns a bare JSON response

-h, --help Show this help message and exit

Download:
Expand Down
3 changes: 3 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Search by latitude and longitude::

$: landsat search --lat 38.9004204 --lon -77.0237117

Search by latitude and longitude with pure json output::

$: landsat search --lat 38.9004204 --lon -77.0237117 --json

Download
++++++++
Expand Down
15 changes: 13 additions & 2 deletions landsat/landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Landsat Util
# License: CC0 1.0 Universal

import sys
import argparse
import textwrap
import json
Expand Down Expand Up @@ -56,6 +57,8 @@
-c CLOUD, --cloud CLOUD
Maximum cloud percentage. Default: 20 perct
--json Returns a bare JSON response
-h, --help Show this help message and exit
Download:
Expand Down Expand Up @@ -181,6 +184,7 @@ def args_options():
'Example: path,row,path,row 001,001,190,204')
parser_search.add_argument('--lat', type=float, help='The latitude')
parser_search.add_argument('--lon', type=float, help='The longitude')
parser_search.add_argument('--json', action='store_true', help='Returns a bare JSON response')

parser_download = subparsers.add_parser('download',
help='Download images from Google Storage')
Expand Down Expand Up @@ -313,6 +317,9 @@ def main(args):
cloud_max=args.cloud)

if result['status'] == 'SUCCESS':
if args.json:
return json.dumps(result)

if args.latest > 0:
datelist = []
for i in range(0, result['total_returned']):
Expand Down Expand Up @@ -431,8 +438,12 @@ def __main__():
global parser
parser = args_options()
args = parser.parse_args()
with timer():
exit(*main(args))
if args.json:
print main(args)
sys.exit(0)
else:
with timer():
exit(*main(args))

if __name__ == "__main__":
try:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_landsat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Tests for landsat"""

import json
import unittest
import subprocess
import errno
Expand Down Expand Up @@ -67,6 +68,15 @@ def test_search_pr_wrong_input(self):
self.assertEquals(landsat.main(self.parser.parse_args(args)),
['Check your request and try again', 1])

def test_search_json_output(self):
"""Test json output in search"""
args = ['search', '--latest', '10', '--json']

output = landsat.main(self.parser.parse_args(args))
j = json.loads(output)

self.assertEquals(type(j), dict)

@mock.patch('landsat.landsat.Downloader')
def test_download_correct(self, mock_downloader):
"""Test download command with correct input"""
Expand Down

0 comments on commit a4b1dcc

Please sign in to comment.