From 2a01f84184f330eac5541e51616781c6d405d459 Mon Sep 17 00:00:00 2001 From: Jennifer Reiber Kyle Date: Thu, 2 Mar 2023 20:39:12 -0800 Subject: [PATCH 1/3] add AssetStatusBar reporter, use in data asset-wait cli command --- planet/cli/data.py | 17 +++++++------- planet/reporting.py | 43 ++++++++++++++++++++++++++++++++++++ tests/unit/test_reporting.py | 21 ++++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/planet/cli/data.py b/planet/cli/data.py index 1bd17fa95..efb9111b6 100644 --- a/planet/cli/data.py +++ b/planet/cli/data.py @@ -18,7 +18,7 @@ import click -from planet.reporting import StateBar +from planet.reporting import AssetStatusBar from planet import data_filter, DataClient, exceptions from planet.clients.data import (SEARCH_SORT, LIST_SEARCH_TYPE, @@ -589,18 +589,19 @@ async def asset_wait(ctx, max_attempts): '''Wait for an asset to be activated. - Returns when the asset state has reached "activated" and the asset is + Returns when the asset status has reached "activated" and the asset is available. ''' quiet = ctx.obj['QUIET'] async with data_client(ctx) as cl: asset = await cl.get_asset(item_type.pop(), item_id, asset_type_id) - with StateBar(order_id="my asset", disable=quiet) as bar: - state = await cl.wait_asset(asset, - delay, - max_attempts, - callback=bar.update_state) - click.echo(state) + with AssetStatusBar(item_type, item_id, asset_type_id, + disable=quiet) as bar: + status = await cl.wait_asset(asset, + delay, + max_attempts, + callback=bar.update) + click.echo(status) # @data.command() diff --git a/planet/reporting.py b/planet/reporting.py index 049e4f562..b70a9d507 100644 --- a/planet/reporting.py +++ b/planet/reporting.py @@ -110,3 +110,46 @@ def update(self, if self.bar is not None: self.bar.refresh() + + +class AssetStatusBar(ProgressBar): + """Bar reporter of asset status.""" + + def __init__( + self, + item_type, + item_id, + asset_type, + disable: bool = False, + ): + """Initialize the object. + """ + self.item_type = item_type + self.item_id = item_id + self.asset_type = asset_type + self.status = '' + super().__init__(disable=disable) + + def open_bar(self): + """Initialize and start the progress bar.""" + self.bar = tqdm( + bar_format="{elapsed} - {desc} - {postfix[0]}: {postfix[1]}", + desc=self.desc, + postfix=["status", self.status], + disable=self.disable) + + @property + def desc(self): + return f'{self.item_type} {self.item_id} {self.asset_type}' + + def update(self, status: str): + self.status = status + + try: + self.bar.postfix[1] = self.status + except AttributeError: + # If the bar is disabled, attempting to access self.bar.postfix + # will result in an error. In this case, just skip it. + pass + + self.bar.refresh() diff --git a/tests/unit/test_reporting.py b/tests/unit/test_reporting.py index 620265e78..5e11307b8 100644 --- a/tests/unit/test_reporting.py +++ b/tests/unit/test_reporting.py @@ -37,6 +37,7 @@ def test_StateBar___init___stateandorder(): def test_StateBar___init___disabled(): + """Make sure it doesn't error out when disabled""" with reporting.StateBar(disable=True) as bar: assert bar.bar.disable @@ -56,3 +57,23 @@ def test_StateBar_update_state(): expected_update = '..:.. - order - state: init' bar.update_state('init') assert (re.fullmatch(expected_update, str(bar))) + + +def test_AssetStatusBar_disabled(): + """Make sure it doesn't error out when disabled""" + with reporting.AssetStatusBar('item-type', + 'item_id', + 'asset_type', + disable=True) as bar: + assert bar.bar.disable + + # just make sure this doesn't error out + bar.update(status='init') + + +def test_AssetStatusBar_update(): + with reporting.AssetStatusBar('item-type', 'item_id', 'asset_type') as bar: + assert ('status: init') not in str(bar) + + bar.update(status='init') + assert ('status: init') in str(bar) From 35274f2aae00287fba522400e58258b1e63bcf94 Mon Sep 17 00:00:00 2001 From: Jennifer Reiber Kyle Date: Thu, 2 Mar 2023 20:51:15 -0800 Subject: [PATCH 2/3] address bar not being opened --- planet/reporting.py | 18 ++++++++++-------- tests/unit/test_reporting.py | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/planet/reporting.py b/planet/reporting.py index b70a9d507..b2969e5ad 100644 --- a/planet/reporting.py +++ b/planet/reporting.py @@ -145,11 +145,13 @@ def desc(self): def update(self, status: str): self.status = status - try: - self.bar.postfix[1] = self.status - except AttributeError: - # If the bar is disabled, attempting to access self.bar.postfix - # will result in an error. In this case, just skip it. - pass - - self.bar.refresh() + if self.bar is not None: + try: + self.bar.postfix[1] = self.status + except AttributeError: + # If the bar is disabled, attempting to access self.bar.postfix + # will result in an error. In this case, just skip it. + pass + + if self.bar is not None: + self.bar.refresh() diff --git a/tests/unit/test_reporting.py b/tests/unit/test_reporting.py index 5e11307b8..120c76024 100644 --- a/tests/unit/test_reporting.py +++ b/tests/unit/test_reporting.py @@ -72,6 +72,7 @@ def test_AssetStatusBar_disabled(): def test_AssetStatusBar_update(): + """Status is changed with update""" with reporting.AssetStatusBar('item-type', 'item_id', 'asset_type') as bar: assert ('status: init') not in str(bar) From ed0be25f77f433ebf8f7381495416bd796a25288 Mon Sep 17 00:00:00 2001 From: Jennifer Reiber Kyle Date: Thu, 2 Mar 2023 20:53:02 -0800 Subject: [PATCH 3/3] test looks for status instead of state --- tests/integration/test_data_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_data_cli.py b/tests/integration/test_data_cli.py index 9c69ee964..246bc12c7 100644 --- a/tests/integration/test_data_cli.py +++ b/tests/integration/test_data_cli.py @@ -1051,7 +1051,7 @@ def test_asset_wait(invoke, runner=runner) assert not result.exception - assert "state: active" in result.output + assert "status: active" in result.output # @respx.mock