Skip to content

Commit 857d6da

Browse files
authored
Merge pull request #865 from planetlabs/data-asset-reporter
Add AssetStatusBar reporter, use in data asset-wait cli command
2 parents 150f438 + 04d33fa commit 857d6da

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed

planet/cli/data.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import click
2020

21-
from planet.reporting import StateBar
21+
from planet.reporting import AssetStatusBar
2222
from planet import data_filter, DataClient, exceptions
2323
from planet.clients.data import (SEARCH_SORT,
2424
LIST_SEARCH_TYPE,
@@ -589,18 +589,19 @@ async def asset_activate(ctx, item_type, item_id, asset_type):
589589
async def asset_wait(ctx, item_type, item_id, asset_type, delay, max_attempts):
590590
'''Wait for an asset to be activated.
591591
592-
Returns when the asset state has reached "activated" and the asset is
592+
Returns when the asset status has reached "activated" and the asset is
593593
available.
594594
'''
595595
quiet = ctx.obj['QUIET']
596596
async with data_client(ctx) as cl:
597597
asset = await cl.get_asset(item_type, item_id, asset_type)
598-
with StateBar(order_id="my asset", disable=quiet) as bar:
599-
state = await cl.wait_asset(asset,
600-
delay,
601-
max_attempts,
602-
callback=bar.update_state)
603-
click.echo(state)
598+
with AssetStatusBar(item_type, item_id, asset_type,
599+
disable=quiet) as bar:
600+
status = await cl.wait_asset(asset,
601+
delay,
602+
max_attempts,
603+
callback=bar.update)
604+
click.echo(status)
604605

605606

606607
# @data.command()

planet/reporting.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,48 @@ def update(self,
110110

111111
if self.bar is not None:
112112
self.bar.refresh()
113+
114+
115+
class AssetStatusBar(ProgressBar):
116+
"""Bar reporter of asset status."""
117+
118+
def __init__(
119+
self,
120+
item_type,
121+
item_id,
122+
asset_type,
123+
disable: bool = False,
124+
):
125+
"""Initialize the object.
126+
"""
127+
self.item_type = item_type
128+
self.item_id = item_id
129+
self.asset_type = asset_type
130+
self.status = ''
131+
super().__init__(disable=disable)
132+
133+
def open_bar(self):
134+
"""Initialize and start the progress bar."""
135+
self.bar = tqdm(
136+
bar_format="{elapsed} - {desc} - {postfix[0]}: {postfix[1]}",
137+
desc=self.desc,
138+
postfix=["status", self.status],
139+
disable=self.disable)
140+
141+
@property
142+
def desc(self):
143+
return f'{self.item_type} {self.item_id} {self.asset_type}'
144+
145+
def update(self, status: str):
146+
self.status = status
147+
148+
if self.bar is not None:
149+
try:
150+
self.bar.postfix[1] = self.status
151+
except AttributeError:
152+
# If the bar is disabled, attempting to access self.bar.postfix
153+
# will result in an error. In this case, just skip it.
154+
pass
155+
156+
if self.bar is not None:
157+
self.bar.refresh()

tests/integration/test_data_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ def test_asset_wait(invoke,
10511051
runner=runner)
10521052

10531053
assert not result.exception
1054-
assert "state: active" in result.output
1054+
assert "status: active" in result.output
10551055

10561056

10571057
# @respx.mock

tests/unit/test_reporting.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_StateBar___init___stateandorder():
3737

3838

3939
def test_StateBar___init___disabled():
40+
"""Make sure it doesn't error out when disabled"""
4041
with reporting.StateBar(disable=True) as bar:
4142
assert bar.bar.disable
4243

@@ -56,3 +57,24 @@ def test_StateBar_update_state():
5657
expected_update = '..:.. - order - state: init'
5758
bar.update_state('init')
5859
assert (re.fullmatch(expected_update, str(bar)))
60+
61+
62+
def test_AssetStatusBar_disabled():
63+
"""Make sure it doesn't error out when disabled"""
64+
with reporting.AssetStatusBar('item-type',
65+
'item_id',
66+
'asset_type',
67+
disable=True) as bar:
68+
assert bar.bar.disable
69+
70+
# just make sure this doesn't error out
71+
bar.update(status='init')
72+
73+
74+
def test_AssetStatusBar_update():
75+
"""Status is changed with update"""
76+
with reporting.AssetStatusBar('item-type', 'item_id', 'asset_type') as bar:
77+
assert ('status: init') not in str(bar)
78+
79+
bar.update(status='init')
80+
assert ('status: init') in str(bar)

0 commit comments

Comments
 (0)