Repository: planetlabs/planet-client-python
Pull request #74 (2015-09-09 12:18:24)

Fixes


Patch:
diff --git a/planet/api/_fatomic.py b/planet/api/_fatomic.py
index 449a54a..324dd8f 100644
--- a/planet/api/_fatomic.py
+++ b/planet/api/_fatomic.py
@@ -73,5 +73,7 @@ def atomic_open(filename, mode, *args, **kwargs):
         raise
     finally:
         f.close()
-        if not _discard[0]:
+        if _discard[0]:
+            os.unlink(f.name)
+        else:
             _replace_file(f.name, filename)
diff --git a/planet/scripts/__init__.py b/planet/scripts/__init__.py
index 1133fe1..1993ea3 100644
--- a/planet/scripts/__init__.py
+++ b/planet/scripts/__init__.py
@@ -127,7 +127,7 @@ def echo_json_response(response, pretty, limit=None):
     '''Wrapper to echo JSON with optional 'pretty' printing. If pretty is not
     provided explicity and stdout is a terminal (and not redirected or piped),
     the default will be to indent and sort keys'''
-    indent = 0
+    indent = None
     sort_keys = False
     if pretty or (pretty is None and sys.stdout.isatty()):
         indent = 2
@@ -298,8 +298,7 @@ def fetch_scene_geotiff(scene_ids, scene_type, product, dest):
     """
     Download full scene image(s).
     """
-
-    scene_ids = read(scene_ids, split=True)
+    scene_ids = read(scene_ids or '@-', split=True)
     if not scene_ids:
         return
 
@@ -321,7 +320,7 @@ def fetch_scene_geotiff(scene_ids, scene_type, product, dest):
 def fetch_scene_thumbnails(scene_ids, scene_type, size, fmt, dest):
     '''Fetch scene thumbnail(s)'''
 
-    scene_ids = read(scene_ids, split=True)
+    scene_ids = read(scene_ids or '@-', split=True)
     if not scene_ids:
         return
 


Test patch:
diff --git a/tests/test_atomic.py b/tests/test_atomic.py
index 615b777..e1afc6c 100644
--- a/tests/test_atomic.py
+++ b/tests/test_atomic.py
@@ -11,11 +11,13 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import os
 from planet.api._fatomic import atomic_open
 
 
 def test_atomic_open(tmpdir):
     outfile = str(tmpdir.join('foo'))
+    lsdir = lambda: os.listdir(str(tmpdir))
 
     def assert_content_is(expected):
         with open(outfile, 'r') as fp:
@@ -25,6 +27,8 @@ def test_atomic_open(tmpdir):
     with atomic_open(outfile, 'w') as fp:
         fp.write('bar')
     assert_content_is('bar')
+    # no tmp files remain
+    assert ['foo'] == lsdir()
 
     # exception during write, assert file remains untouched
     try:
@@ -35,9 +39,13 @@ def test_atomic_open(tmpdir):
         assert_content_is('bar')
     else:
         assert False
+    # no tmp files remain
+    assert ['foo'] == lsdir()
 
     # manual discarding
     with atomic_open(outfile, 'w') as fp:
             fp.write('bazzy')
             fp.discard()
     assert_content_is('bar')
+    # no tmp files remain
+    assert ['foo'] == lsdir()
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 8d46ff9..e9cbdb9 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -180,9 +180,55 @@ def test_metadata():
 
 
 def test_download():
-
-    result = runner.invoke(scripts.cli, ['download', '20150615_190229_0905'])
-    assert result.exit_code == 0
+    response = MagicMock(spec=models.Image)
+    client.fetch_scene_geotiffs.return_value = response
+
+    def test(ids, as_std_in=False):
+        args, input = ['download'], None
+        if as_std_in:
+            input = '\n'.join(ids)
+        else:
+            args.extend(ids)
+        assert_correct(runner.invoke(scripts.cli,
+                       args, input=input), ids)
+
+    def assert_correct(result, expected_ids):
+        assert result.exit_code == 0
+        called_with = client.fetch_scene_geotiffs.call_args[0]
+        assert list(expected_ids) == list(called_with[0])
+        assert 'ortho' == called_with[1]
+        assert 'visual' == called_with[2]
+
+    test(['20150615_190229_0905'])
+    test(['20150615_190229_0905', '20150615_190229_0906'])
+    test(['20150615_190229_0905'], as_std_in=True)
+    test(['20150615_190229_0905', '20150615_190229_0906'], as_std_in=True)
+
+
+def test_thumbs():
+    response = MagicMock(spec=models.Image)
+    client.fetch_scene_thumbnails.return_value = response
+
+    def test(ids, as_std_in=False):
+        args, input = ['thumbnails'], None
+        if as_std_in:
+            input = '\n'.join(ids)
+        else:
+            args.extend(ids)
+        assert_correct(runner.invoke(scripts.cli,
+                       args, input=input), ids)
+
+    def assert_correct(result, expected_ids):
+        assert result.exit_code == 0
+        called_with = client.fetch_scene_thumbnails.call_args[0]
+        assert list(expected_ids) == list(called_with[0])
+        assert 'ortho' == called_with[1]
+        assert 'md' == called_with[2]
+
+    test(['20150615_190229_0905'])
+    test(['20150615_190229_0905', '20150615_190229_0906'])
+    test(['20150615_190229_0905'], as_std_in=True)
+    test(['20150615_190229_0905', '20150615_190229_0906'], as_std_in=True)
 
 
 def test_init():