Skip to content

Commit 2a01f84

Browse files
committed
add AssetStatusBar reporter, use in data asset-wait cli command
1 parent 6da207d commit 2a01f84

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
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_wait(ctx,
589589
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.pop(), item_id, asset_type_id)
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_id,
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,46 @@ 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+
try:
149+
self.bar.postfix[1] = self.status
150+
except AttributeError:
151+
# If the bar is disabled, attempting to access self.bar.postfix
152+
# will result in an error. In this case, just skip it.
153+
pass
154+
155+
self.bar.refresh()

tests/unit/test_reporting.py

Lines changed: 21 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,23 @@ 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+
with reporting.AssetStatusBar('item-type', 'item_id', 'asset_type') as bar:
76+
assert ('status: init') not in str(bar)
77+
78+
bar.update(status='init')
79+
assert ('status: init') in str(bar)

0 commit comments

Comments
 (0)