Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions planet/cli/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions planet/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Comment on lines +120 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth doing this:

Suggested change
item_type,
item_id,
asset_type,
item_type: str,
item_id: str,
asset_type: str,

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}'
Copy link
Contributor

@kevinlacaille kevinlacaille Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: This is a bug which is resolved when pulling in the most recent changes from your base branch. The bug stems from item_type being read in as a CommaSeparatedString, but it will now be read in as a string.

Is the bar supposed to read time - item_type item_ID asset_type - status: <status>? I currently seems to have some square brackets where item_type should be.

When I run it on my computer I get this:

❯ planet data asset-wait PSScene 20221003_002705_38_2461 basic_udm2                
00:00 - [] 20221003_002705_38_2461 basic_udm2 - status: active


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()
2 changes: 1 addition & 1 deletion tests/integration/test_data_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check to see if all things you want to report end up in your reporting bar. Maybe something like this? This currently fails on my laptop, since item_type isn't showing up.

Suggested change
assert all([item in result.output for item in [item_type, item_id, asset_type_id]])


# @respx.mock
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)