diff --git a/planet/cli/data.py b/planet/cli/data.py index 12d73e940..cbca75e6a 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_activate(ctx, item_type, item_id, asset_type): async def asset_wait(ctx, item_type, item_id, asset_type, delay, 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, item_id, asset_type) - 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, + 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..b2969e5ad 100644 --- a/planet/reporting.py +++ b/planet/reporting.py @@ -110,3 +110,48 @@ 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 + + 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/integration/test_data_cli.py b/tests/integration/test_data_cli.py index 5172d3ced..6150d25a8 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 diff --git a/tests/unit/test_reporting.py b/tests/unit/test_reporting.py index 620265e78..120c76024 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,24 @@ 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(): + """Status is changed with 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)