forked from planetlabs/planet-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting.py
157 lines (127 loc) · 4.34 KB
/
reporting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2021 Planet Labs, Inc.
# Copyright 2022 Planet Labs PBC.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Functionality for reporting progress."""
import logging
from typing import Optional
from tqdm.asyncio import tqdm
LOGGER = logging.getLogger(__name__)
class ProgressBar:
"""Abstract base class for progress bar reporters."""
def __init__(self, disable: bool = False):
self.bar = None
self.disable = disable
def __str__(self):
return str(self.bar)
def __enter__(self):
self.open_bar()
return self
def __exit__(self, *args):
self.bar.close()
def open_bar(self):
"""Initialize and start the progress bar."""
return NotImplementedError
class StateBar(ProgressBar):
"""Bar reporter of order state.
Example:
```python
from planet import reporting
with reporting.StateBar(state='creating') as bar:
bar.update(state='created', order_id='oid')
...
```
"""
def __init__(
self,
order_id: Optional[str] = None,
state: Optional[str] = None,
disable: bool = False,
):
"""Initialize the object.
Parameters:
order_id: Id of the order.
state: State of the order.
"""
self.state = state or ''
self.order_id = order_id or ''
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=["state", self.state],
disable=self.disable)
@property
def desc(self):
return f'order {self.order_id}'
def update_state(self, state: str):
"""Simple function to be used as a callback for state reporting"""
self.update(state=state)
def update(self,
state: Optional[str] = None,
order_id: Optional[str] = None):
if state:
self.state = state
if self.bar is not None:
try:
self.bar.postfix[1] = self.state
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 order_id:
self.order_id = order_id
if self.bar is not None:
self.bar.set_description_str(self.desc, refresh=False)
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()