pygeobox 1.0.3__py3-none-any.whl → 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygeobox/__init__.py +63 -63
- pygeobox/api.py +2517 -2517
- pygeobox/apikey.py +232 -232
- pygeobox/attachment.py +297 -297
- pygeobox/base.py +364 -364
- pygeobox/basemap.py +162 -162
- pygeobox/dashboard.py +316 -316
- pygeobox/enums.py +354 -354
- pygeobox/exception.py +47 -47
- pygeobox/field.py +309 -309
- pygeobox/map.py +858 -858
- pygeobox/model3d.py +334 -334
- pygeobox/mosaic.py +647 -647
- pygeobox/plan.py +260 -260
- pygeobox/query.py +668 -668
- pygeobox/raster.py +756 -756
- pygeobox/route.py +63 -63
- pygeobox/scene.py +317 -317
- pygeobox/settings.py +165 -165
- pygeobox/task.py +354 -354
- pygeobox/tile3d.py +294 -294
- pygeobox/tileset.py +585 -585
- pygeobox/user.py +423 -423
- pygeobox/utils.py +43 -43
- pygeobox/vectorlayer.py +1149 -1149
- pygeobox/version.py +248 -248
- pygeobox/view.py +859 -859
- pygeobox/workflow.py +315 -315
- {pygeobox-1.0.3.dist-info → pygeobox-1.0.4.dist-info}/METADATA +3 -5
- pygeobox-1.0.4.dist-info/RECORD +37 -0
- {pygeobox-1.0.3.dist-info → pygeobox-1.0.4.dist-info}/licenses/LICENSE +21 -21
- pygeobox-1.0.3.dist-info/RECORD +0 -37
- {pygeobox-1.0.3.dist-info → pygeobox-1.0.4.dist-info}/WHEEL +0 -0
- {pygeobox-1.0.3.dist-info → pygeobox-1.0.4.dist-info}/top_level.txt +0 -0
pygeobox/task.py
CHANGED
@@ -1,354 +1,354 @@
|
|
1
|
-
from urllib.parse import urljoin, urlencode
|
2
|
-
from typing import Optional, Dict, List, Optional, Union, TYPE_CHECKING
|
3
|
-
import time
|
4
|
-
|
5
|
-
from .base import Base
|
6
|
-
from .enums import TaskStatus
|
7
|
-
|
8
|
-
if TYPE_CHECKING:
|
9
|
-
from . import GeoboxClient
|
10
|
-
from .vectorlayer import VectorLayer
|
11
|
-
from .raster import Raster
|
12
|
-
from .model3d import Model
|
13
|
-
|
14
|
-
class Task(Base):
|
15
|
-
"""
|
16
|
-
A class to represent a task in Geobox.
|
17
|
-
|
18
|
-
This class provides functionality to interact with tasks in Geobox.
|
19
|
-
It supports various operations including getting tasks, as well as advanced operations like waiting for a task to finish.
|
20
|
-
It also provides properties to access the task data, and a method to abort the task.
|
21
|
-
"""
|
22
|
-
BASE_ENDPOINT: str = 'tasks/'
|
23
|
-
|
24
|
-
def __init__(self,
|
25
|
-
api: 'GeoboxClient',
|
26
|
-
uuid: str,
|
27
|
-
data: Optional[Dict] = {}):
|
28
|
-
"""
|
29
|
-
Constructs all the necessary attributes for the Task object.
|
30
|
-
|
31
|
-
Args:
|
32
|
-
api (GeoboxClient): The API instance.
|
33
|
-
uuid (str): The UUID of the task.
|
34
|
-
data (Dict, optional): The task data.
|
35
|
-
"""
|
36
|
-
super().__init__(api, uuid=uuid, data=data)
|
37
|
-
self._data = data if isinstance(data, dict) else {}
|
38
|
-
|
39
|
-
|
40
|
-
def refresh_data(self) -> None:
|
41
|
-
"""
|
42
|
-
Updates the task data.
|
43
|
-
"""
|
44
|
-
self._data = self.get_task(self.api, self.uuid).data
|
45
|
-
|
46
|
-
|
47
|
-
@property
|
48
|
-
def publised_asset(self) -> Union['VectorLayer', 'Raster', 'Model', None]:
|
49
|
-
"""
|
50
|
-
Published asset property
|
51
|
-
|
52
|
-
Returns:
|
53
|
-
VectorLayer | Raster | Model | None: if task type is publish, it returns the published layer
|
54
|
-
|
55
|
-
Example:
|
56
|
-
>>> from geobox import GeoboxClient
|
57
|
-
>>> from geobox.task import Task
|
58
|
-
>>> client = GeoboxClient()
|
59
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
60
|
-
>>> task.publised_asset
|
61
|
-
"""
|
62
|
-
if self.name == 'import:publish-file-to-raster-dataset':
|
63
|
-
return self.api.get_raster(uuid=self.data['result']['raster_uuid'])
|
64
|
-
elif self.name == 'import:publish-file-to-vector-layer':
|
65
|
-
return self.api.get_vector(uuid=self.data['result']['layer_uuid'])
|
66
|
-
elif self.name == 'import:publish-file-to-3d-model':
|
67
|
-
return self.api.get_model(uuid=self.data['result']['model_uuid'])
|
68
|
-
else:
|
69
|
-
return None
|
70
|
-
|
71
|
-
|
72
|
-
@property
|
73
|
-
def data(self) -> Dict:
|
74
|
-
"""
|
75
|
-
Returns the task data.
|
76
|
-
|
77
|
-
Returns:
|
78
|
-
Dict: the task data as a dictionary
|
79
|
-
|
80
|
-
Example:
|
81
|
-
>>> from geobox import GeoboxClient
|
82
|
-
>>> from geobox.task import Task
|
83
|
-
>>> client = GeoboxClient()
|
84
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
85
|
-
>>> task.data
|
86
|
-
"""
|
87
|
-
return self._data
|
88
|
-
|
89
|
-
|
90
|
-
@data.setter
|
91
|
-
def data(self, value: Dict) -> None:
|
92
|
-
"""
|
93
|
-
Sets the task data.
|
94
|
-
|
95
|
-
Example:
|
96
|
-
>>> from geobox import GeoboxClient
|
97
|
-
>>> from geobox.task import Task
|
98
|
-
>>> client = GeoboxClient()
|
99
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
100
|
-
>>> task.data = {'name': 'test'}
|
101
|
-
"""
|
102
|
-
self._data = value if isinstance(value, dict) else {}
|
103
|
-
|
104
|
-
|
105
|
-
@property
|
106
|
-
def status(self) -> 'TaskStatus':
|
107
|
-
"""
|
108
|
-
Returns the status of the task. (auto refresh)
|
109
|
-
|
110
|
-
Returns:
|
111
|
-
TaskStatus: the status of the task(SUCCESS, FAILURE, ABORTED, PENDING, PROGRESS)
|
112
|
-
|
113
|
-
Example:
|
114
|
-
>>> from geobox import GeoboxClient
|
115
|
-
>>> from geobox.task import Task
|
116
|
-
>>> client = GeoboxClient()
|
117
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
118
|
-
>>> task.status
|
119
|
-
"""
|
120
|
-
self.refresh_data()
|
121
|
-
return TaskStatus(self._data.get('state'))
|
122
|
-
|
123
|
-
|
124
|
-
@property
|
125
|
-
def errors(self) -> Union[Dict, None]:
|
126
|
-
"""
|
127
|
-
Get the task errors.
|
128
|
-
|
129
|
-
Returns:
|
130
|
-
Dict | None: if there are any errors
|
131
|
-
|
132
|
-
Example:
|
133
|
-
>>> from geobox import GeoboxClient
|
134
|
-
>>> from geobox.task import Task
|
135
|
-
>>> client = GeoboxClient()
|
136
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
137
|
-
>>> task.errors
|
138
|
-
"""
|
139
|
-
result = self.data.get('result', {})
|
140
|
-
if result.get('errors') or result.get('detail', {}).get('msg'):
|
141
|
-
return result
|
142
|
-
else:
|
143
|
-
return None
|
144
|
-
|
145
|
-
|
146
|
-
@property
|
147
|
-
def progress(self) -> Union[int, None]:
|
148
|
-
"""
|
149
|
-
Returns the progress of the task.
|
150
|
-
|
151
|
-
Returns:
|
152
|
-
int | None: the progress of the task in percentage or None if the task is not running
|
153
|
-
|
154
|
-
Example:
|
155
|
-
>>> from geobox import GeoboxClient
|
156
|
-
>>> from geobox.task import Task
|
157
|
-
>>> client = GeoboxClient()
|
158
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
159
|
-
>>> task.progress
|
160
|
-
"""
|
161
|
-
endpoint = urljoin(self.endpoint, 'status/')
|
162
|
-
response = self.api.get(endpoint)
|
163
|
-
|
164
|
-
current = response.get('current')
|
165
|
-
total = response.get('total')
|
166
|
-
if not total or not current:
|
167
|
-
return None
|
168
|
-
|
169
|
-
return int((current / total) * 100)
|
170
|
-
|
171
|
-
|
172
|
-
def wait(self, timeout: Union[int, None] = None, interval: int = 1, progress_bar: bool = True) -> 'TaskStatus':
|
173
|
-
"""
|
174
|
-
Wait for the task to finish.
|
175
|
-
|
176
|
-
Args:
|
177
|
-
timeout (int, optional): Maximum time to wait in seconds.
|
178
|
-
interval (int, optional): Time between status checks in seconds.
|
179
|
-
progress_bar (bool, optional): Whether to show a progress bar. default: True
|
180
|
-
|
181
|
-
Returns:
|
182
|
-
TaskStatus: the status of the task(SUCCESS, FAILURE, ABORTED, PENDING, PROGRESS)
|
183
|
-
|
184
|
-
Raises:
|
185
|
-
TimeoutError: If the task doesn't complete within timeout seconds.
|
186
|
-
|
187
|
-
Example:
|
188
|
-
>>> from geobox import GeoboxClient
|
189
|
-
>>> from geobox.task import Task
|
190
|
-
>>> client = GeoboxClient()
|
191
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
192
|
-
>>> task.wait() # return the status of the task
|
193
|
-
"""
|
194
|
-
start_time = time.time()
|
195
|
-
last_progress = 0
|
196
|
-
pbar = self._create_progress_bar() if progress_bar else None
|
197
|
-
|
198
|
-
try:
|
199
|
-
while True:
|
200
|
-
self._check_timeout(start_time, timeout)
|
201
|
-
status = self.status
|
202
|
-
|
203
|
-
if self._is_final_state(status):
|
204
|
-
self._update_progress_bar(pbar, last_progress, status)
|
205
|
-
return TaskStatus(status)
|
206
|
-
|
207
|
-
if pbar:
|
208
|
-
last_progress = self._update_progress_bar(pbar, last_progress)
|
209
|
-
|
210
|
-
time.sleep(interval)
|
211
|
-
finally:
|
212
|
-
if pbar:
|
213
|
-
pbar.close()
|
214
|
-
|
215
|
-
|
216
|
-
def _create_progress_bar(self) -> 'tqdm':
|
217
|
-
"""Creates a progress bar for the task."""
|
218
|
-
try:
|
219
|
-
from tqdm.auto import tqdm
|
220
|
-
except ImportError:
|
221
|
-
from .api import logger
|
222
|
-
logger.warning("[tqdm] extra is required to show the progress bar. install with: pip insatll pygeobox[tqdm]")
|
223
|
-
return None
|
224
|
-
|
225
|
-
return tqdm(total=100, colour='green', desc=f"Task: {self.name}", unit="%", leave=True)
|
226
|
-
|
227
|
-
|
228
|
-
def _check_timeout(self, start_time: float, timeout: Union[int, None]) -> None:
|
229
|
-
"""Checks if the task has exceeded the timeout period."""
|
230
|
-
if timeout and time.time() - start_time > timeout:
|
231
|
-
raise TimeoutError(f"Task {self.name} timed out after {timeout} seconds")
|
232
|
-
|
233
|
-
|
234
|
-
def _is_final_state(self, status: 'TaskStatus') -> bool:
|
235
|
-
"""Checks if the task has reached a final state."""
|
236
|
-
return status in [TaskStatus.FAILURE, TaskStatus.SUCCESS, TaskStatus.ABORTED]
|
237
|
-
|
238
|
-
|
239
|
-
def _update_progress_bar(self, pbar: Union['tqdm', None], last_progress: int, status: 'TaskStatus' = None) -> int:
|
240
|
-
"""
|
241
|
-
Updates the progress bar with current progress and returns the new last_progress.
|
242
|
-
|
243
|
-
Args:
|
244
|
-
pbar (tqdm | None): The progress bar to update
|
245
|
-
last_progress (int): The last progress value
|
246
|
-
status (TaskStatus, optional): The task status. If provided and SUCCESS, updates to 100%
|
247
|
-
|
248
|
-
Returns:
|
249
|
-
int: The new last_progress value
|
250
|
-
"""
|
251
|
-
if not pbar:
|
252
|
-
return last_progress
|
253
|
-
|
254
|
-
if status == TaskStatus.SUCCESS:
|
255
|
-
pbar.update(100 - last_progress)
|
256
|
-
return 100
|
257
|
-
|
258
|
-
current_progress = self.progress
|
259
|
-
if current_progress is not None:
|
260
|
-
progress_diff = current_progress - last_progress
|
261
|
-
if progress_diff > 0:
|
262
|
-
pbar.update(progress_diff)
|
263
|
-
return current_progress
|
264
|
-
return last_progress
|
265
|
-
|
266
|
-
|
267
|
-
def abort(self) -> None:
|
268
|
-
"""
|
269
|
-
Aborts the task.
|
270
|
-
|
271
|
-
Returns:
|
272
|
-
None
|
273
|
-
|
274
|
-
Example:
|
275
|
-
>>> from geobox import GeoboxClient
|
276
|
-
>>> from geobox.task import Task
|
277
|
-
>>> client = GeoboxClient()
|
278
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
279
|
-
>>> task.abort()
|
280
|
-
"""
|
281
|
-
endpoint = urljoin(self.endpoint, 'abort/')
|
282
|
-
self.api.post(endpoint)
|
283
|
-
|
284
|
-
|
285
|
-
@classmethod
|
286
|
-
def get_tasks(cls, api: 'GeoboxClient', **kwargs) -> Union[List['Task'], int]:
|
287
|
-
"""
|
288
|
-
Get a list of tasks
|
289
|
-
|
290
|
-
Args:
|
291
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
292
|
-
|
293
|
-
Keyword Args:
|
294
|
-
state (TaskStatus): Available values : TaskStatus.PENDING, TaskStatus.PROGRESS, TaskStatus.SUCCESS, TaskStatus.FAILURE, TaskStatus.ABORTED
|
295
|
-
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
296
|
-
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
297
|
-
search_fields (str): comma separated list of fields for searching.
|
298
|
-
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
299
|
-
return_count (bool): The count of the tasks. default is False.
|
300
|
-
skip (int): The skip of the task. default is 0.
|
301
|
-
limit (int): The limit of the task. default is 10.
|
302
|
-
user_id (int): Specific user. privileges required.
|
303
|
-
shared (bool): Whether to return shared tasks. default is False.
|
304
|
-
|
305
|
-
Returns:
|
306
|
-
List[Task] | int: The list of task objects or the count of the tasks if return_count is True.
|
307
|
-
|
308
|
-
Example:
|
309
|
-
>>> from geobox import GeoboxClient
|
310
|
-
>>> from geobox.task import Task
|
311
|
-
>>> client = GeoboxClient()
|
312
|
-
>>> tasks = Task.get_tasks(client)
|
313
|
-
or
|
314
|
-
>>> tasks = client.get_tasks()
|
315
|
-
"""
|
316
|
-
params = {
|
317
|
-
'f': 'json',
|
318
|
-
'state': kwargs.get('state').value if kwargs.get('state') else None,
|
319
|
-
'q': kwargs.get('q'),
|
320
|
-
'search': kwargs.get('search'),
|
321
|
-
'search_fields': kwargs.get('search_fields'),
|
322
|
-
'order_by': kwargs.get('order_by'),
|
323
|
-
'return_count': kwargs.get('return_count', False),
|
324
|
-
'skip': kwargs.get('skip'),
|
325
|
-
'limit': kwargs.get('limit'),
|
326
|
-
'user_id': kwargs.get('user_id'),
|
327
|
-
'shared': kwargs.get('shared', False)
|
328
|
-
}
|
329
|
-
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: Task(api, item['uuid'], item))
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
@classmethod
|
334
|
-
def get_task(cls, api: 'GeoboxClient', uuid: str) -> 'Task':
|
335
|
-
"""
|
336
|
-
Gets a task.
|
337
|
-
|
338
|
-
Args:
|
339
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
340
|
-
uuid (str): The UUID of the task.
|
341
|
-
|
342
|
-
Returns:
|
343
|
-
Task: The task object.
|
344
|
-
|
345
|
-
Example:
|
346
|
-
>>> from geobox import GeoboxClient
|
347
|
-
>>> from geobox.task import Task
|
348
|
-
>>> client = GeoboxClient()
|
349
|
-
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
350
|
-
or
|
351
|
-
>>> task = client.get_task(uuid="12345678-1234-5678-1234-567812345678")
|
352
|
-
"""
|
353
|
-
return super()._get_detail(api, cls.BASE_ENDPOINT, uuid, factory_func=lambda api, item: Task(api, item['uuid'], item))
|
354
|
-
|
1
|
+
from urllib.parse import urljoin, urlencode
|
2
|
+
from typing import Optional, Dict, List, Optional, Union, TYPE_CHECKING
|
3
|
+
import time
|
4
|
+
|
5
|
+
from .base import Base
|
6
|
+
from .enums import TaskStatus
|
7
|
+
|
8
|
+
if TYPE_CHECKING:
|
9
|
+
from . import GeoboxClient
|
10
|
+
from .vectorlayer import VectorLayer
|
11
|
+
from .raster import Raster
|
12
|
+
from .model3d import Model
|
13
|
+
|
14
|
+
class Task(Base):
|
15
|
+
"""
|
16
|
+
A class to represent a task in Geobox.
|
17
|
+
|
18
|
+
This class provides functionality to interact with tasks in Geobox.
|
19
|
+
It supports various operations including getting tasks, as well as advanced operations like waiting for a task to finish.
|
20
|
+
It also provides properties to access the task data, and a method to abort the task.
|
21
|
+
"""
|
22
|
+
BASE_ENDPOINT: str = 'tasks/'
|
23
|
+
|
24
|
+
def __init__(self,
|
25
|
+
api: 'GeoboxClient',
|
26
|
+
uuid: str,
|
27
|
+
data: Optional[Dict] = {}):
|
28
|
+
"""
|
29
|
+
Constructs all the necessary attributes for the Task object.
|
30
|
+
|
31
|
+
Args:
|
32
|
+
api (GeoboxClient): The API instance.
|
33
|
+
uuid (str): The UUID of the task.
|
34
|
+
data (Dict, optional): The task data.
|
35
|
+
"""
|
36
|
+
super().__init__(api, uuid=uuid, data=data)
|
37
|
+
self._data = data if isinstance(data, dict) else {}
|
38
|
+
|
39
|
+
|
40
|
+
def refresh_data(self) -> None:
|
41
|
+
"""
|
42
|
+
Updates the task data.
|
43
|
+
"""
|
44
|
+
self._data = self.get_task(self.api, self.uuid).data
|
45
|
+
|
46
|
+
|
47
|
+
@property
|
48
|
+
def publised_asset(self) -> Union['VectorLayer', 'Raster', 'Model', None]:
|
49
|
+
"""
|
50
|
+
Published asset property
|
51
|
+
|
52
|
+
Returns:
|
53
|
+
VectorLayer | Raster | Model | None: if task type is publish, it returns the published layer
|
54
|
+
|
55
|
+
Example:
|
56
|
+
>>> from geobox import GeoboxClient
|
57
|
+
>>> from geobox.task import Task
|
58
|
+
>>> client = GeoboxClient()
|
59
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
60
|
+
>>> task.publised_asset
|
61
|
+
"""
|
62
|
+
if self.name == 'import:publish-file-to-raster-dataset':
|
63
|
+
return self.api.get_raster(uuid=self.data['result']['raster_uuid'])
|
64
|
+
elif self.name == 'import:publish-file-to-vector-layer':
|
65
|
+
return self.api.get_vector(uuid=self.data['result']['layer_uuid'])
|
66
|
+
elif self.name == 'import:publish-file-to-3d-model':
|
67
|
+
return self.api.get_model(uuid=self.data['result']['model_uuid'])
|
68
|
+
else:
|
69
|
+
return None
|
70
|
+
|
71
|
+
|
72
|
+
@property
|
73
|
+
def data(self) -> Dict:
|
74
|
+
"""
|
75
|
+
Returns the task data.
|
76
|
+
|
77
|
+
Returns:
|
78
|
+
Dict: the task data as a dictionary
|
79
|
+
|
80
|
+
Example:
|
81
|
+
>>> from geobox import GeoboxClient
|
82
|
+
>>> from geobox.task import Task
|
83
|
+
>>> client = GeoboxClient()
|
84
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
85
|
+
>>> task.data
|
86
|
+
"""
|
87
|
+
return self._data
|
88
|
+
|
89
|
+
|
90
|
+
@data.setter
|
91
|
+
def data(self, value: Dict) -> None:
|
92
|
+
"""
|
93
|
+
Sets the task data.
|
94
|
+
|
95
|
+
Example:
|
96
|
+
>>> from geobox import GeoboxClient
|
97
|
+
>>> from geobox.task import Task
|
98
|
+
>>> client = GeoboxClient()
|
99
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
100
|
+
>>> task.data = {'name': 'test'}
|
101
|
+
"""
|
102
|
+
self._data = value if isinstance(value, dict) else {}
|
103
|
+
|
104
|
+
|
105
|
+
@property
|
106
|
+
def status(self) -> 'TaskStatus':
|
107
|
+
"""
|
108
|
+
Returns the status of the task. (auto refresh)
|
109
|
+
|
110
|
+
Returns:
|
111
|
+
TaskStatus: the status of the task(SUCCESS, FAILURE, ABORTED, PENDING, PROGRESS)
|
112
|
+
|
113
|
+
Example:
|
114
|
+
>>> from geobox import GeoboxClient
|
115
|
+
>>> from geobox.task import Task
|
116
|
+
>>> client = GeoboxClient()
|
117
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
118
|
+
>>> task.status
|
119
|
+
"""
|
120
|
+
self.refresh_data()
|
121
|
+
return TaskStatus(self._data.get('state'))
|
122
|
+
|
123
|
+
|
124
|
+
@property
|
125
|
+
def errors(self) -> Union[Dict, None]:
|
126
|
+
"""
|
127
|
+
Get the task errors.
|
128
|
+
|
129
|
+
Returns:
|
130
|
+
Dict | None: if there are any errors
|
131
|
+
|
132
|
+
Example:
|
133
|
+
>>> from geobox import GeoboxClient
|
134
|
+
>>> from geobox.task import Task
|
135
|
+
>>> client = GeoboxClient()
|
136
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
137
|
+
>>> task.errors
|
138
|
+
"""
|
139
|
+
result = self.data.get('result', {})
|
140
|
+
if result.get('errors') or result.get('detail', {}).get('msg'):
|
141
|
+
return result
|
142
|
+
else:
|
143
|
+
return None
|
144
|
+
|
145
|
+
|
146
|
+
@property
|
147
|
+
def progress(self) -> Union[int, None]:
|
148
|
+
"""
|
149
|
+
Returns the progress of the task.
|
150
|
+
|
151
|
+
Returns:
|
152
|
+
int | None: the progress of the task in percentage or None if the task is not running
|
153
|
+
|
154
|
+
Example:
|
155
|
+
>>> from geobox import GeoboxClient
|
156
|
+
>>> from geobox.task import Task
|
157
|
+
>>> client = GeoboxClient()
|
158
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
159
|
+
>>> task.progress
|
160
|
+
"""
|
161
|
+
endpoint = urljoin(self.endpoint, 'status/')
|
162
|
+
response = self.api.get(endpoint)
|
163
|
+
|
164
|
+
current = response.get('current')
|
165
|
+
total = response.get('total')
|
166
|
+
if not total or not current:
|
167
|
+
return None
|
168
|
+
|
169
|
+
return int((current / total) * 100)
|
170
|
+
|
171
|
+
|
172
|
+
def wait(self, timeout: Union[int, None] = None, interval: int = 1, progress_bar: bool = True) -> 'TaskStatus':
|
173
|
+
"""
|
174
|
+
Wait for the task to finish.
|
175
|
+
|
176
|
+
Args:
|
177
|
+
timeout (int, optional): Maximum time to wait in seconds.
|
178
|
+
interval (int, optional): Time between status checks in seconds.
|
179
|
+
progress_bar (bool, optional): Whether to show a progress bar. default: True
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
TaskStatus: the status of the task(SUCCESS, FAILURE, ABORTED, PENDING, PROGRESS)
|
183
|
+
|
184
|
+
Raises:
|
185
|
+
TimeoutError: If the task doesn't complete within timeout seconds.
|
186
|
+
|
187
|
+
Example:
|
188
|
+
>>> from geobox import GeoboxClient
|
189
|
+
>>> from geobox.task import Task
|
190
|
+
>>> client = GeoboxClient()
|
191
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
192
|
+
>>> task.wait() # return the status of the task
|
193
|
+
"""
|
194
|
+
start_time = time.time()
|
195
|
+
last_progress = 0
|
196
|
+
pbar = self._create_progress_bar() if progress_bar else None
|
197
|
+
|
198
|
+
try:
|
199
|
+
while True:
|
200
|
+
self._check_timeout(start_time, timeout)
|
201
|
+
status = self.status
|
202
|
+
|
203
|
+
if self._is_final_state(status):
|
204
|
+
self._update_progress_bar(pbar, last_progress, status)
|
205
|
+
return TaskStatus(status)
|
206
|
+
|
207
|
+
if pbar:
|
208
|
+
last_progress = self._update_progress_bar(pbar, last_progress)
|
209
|
+
|
210
|
+
time.sleep(interval)
|
211
|
+
finally:
|
212
|
+
if pbar:
|
213
|
+
pbar.close()
|
214
|
+
|
215
|
+
|
216
|
+
def _create_progress_bar(self) -> 'tqdm':
|
217
|
+
"""Creates a progress bar for the task."""
|
218
|
+
try:
|
219
|
+
from tqdm.auto import tqdm
|
220
|
+
except ImportError:
|
221
|
+
from .api import logger
|
222
|
+
logger.warning("[tqdm] extra is required to show the progress bar. install with: pip insatll pygeobox[tqdm]")
|
223
|
+
return None
|
224
|
+
|
225
|
+
return tqdm(total=100, colour='green', desc=f"Task: {self.name}", unit="%", leave=True)
|
226
|
+
|
227
|
+
|
228
|
+
def _check_timeout(self, start_time: float, timeout: Union[int, None]) -> None:
|
229
|
+
"""Checks if the task has exceeded the timeout period."""
|
230
|
+
if timeout and time.time() - start_time > timeout:
|
231
|
+
raise TimeoutError(f"Task {self.name} timed out after {timeout} seconds")
|
232
|
+
|
233
|
+
|
234
|
+
def _is_final_state(self, status: 'TaskStatus') -> bool:
|
235
|
+
"""Checks if the task has reached a final state."""
|
236
|
+
return status in [TaskStatus.FAILURE, TaskStatus.SUCCESS, TaskStatus.ABORTED]
|
237
|
+
|
238
|
+
|
239
|
+
def _update_progress_bar(self, pbar: Union['tqdm', None], last_progress: int, status: 'TaskStatus' = None) -> int:
|
240
|
+
"""
|
241
|
+
Updates the progress bar with current progress and returns the new last_progress.
|
242
|
+
|
243
|
+
Args:
|
244
|
+
pbar (tqdm | None): The progress bar to update
|
245
|
+
last_progress (int): The last progress value
|
246
|
+
status (TaskStatus, optional): The task status. If provided and SUCCESS, updates to 100%
|
247
|
+
|
248
|
+
Returns:
|
249
|
+
int: The new last_progress value
|
250
|
+
"""
|
251
|
+
if not pbar:
|
252
|
+
return last_progress
|
253
|
+
|
254
|
+
if status == TaskStatus.SUCCESS:
|
255
|
+
pbar.update(100 - last_progress)
|
256
|
+
return 100
|
257
|
+
|
258
|
+
current_progress = self.progress
|
259
|
+
if current_progress is not None:
|
260
|
+
progress_diff = current_progress - last_progress
|
261
|
+
if progress_diff > 0:
|
262
|
+
pbar.update(progress_diff)
|
263
|
+
return current_progress
|
264
|
+
return last_progress
|
265
|
+
|
266
|
+
|
267
|
+
def abort(self) -> None:
|
268
|
+
"""
|
269
|
+
Aborts the task.
|
270
|
+
|
271
|
+
Returns:
|
272
|
+
None
|
273
|
+
|
274
|
+
Example:
|
275
|
+
>>> from geobox import GeoboxClient
|
276
|
+
>>> from geobox.task import Task
|
277
|
+
>>> client = GeoboxClient()
|
278
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
279
|
+
>>> task.abort()
|
280
|
+
"""
|
281
|
+
endpoint = urljoin(self.endpoint, 'abort/')
|
282
|
+
self.api.post(endpoint)
|
283
|
+
|
284
|
+
|
285
|
+
@classmethod
|
286
|
+
def get_tasks(cls, api: 'GeoboxClient', **kwargs) -> Union[List['Task'], int]:
|
287
|
+
"""
|
288
|
+
Get a list of tasks
|
289
|
+
|
290
|
+
Args:
|
291
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
292
|
+
|
293
|
+
Keyword Args:
|
294
|
+
state (TaskStatus): Available values : TaskStatus.PENDING, TaskStatus.PROGRESS, TaskStatus.SUCCESS, TaskStatus.FAILURE, TaskStatus.ABORTED
|
295
|
+
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
296
|
+
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
297
|
+
search_fields (str): comma separated list of fields for searching.
|
298
|
+
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
299
|
+
return_count (bool): The count of the tasks. default is False.
|
300
|
+
skip (int): The skip of the task. default is 0.
|
301
|
+
limit (int): The limit of the task. default is 10.
|
302
|
+
user_id (int): Specific user. privileges required.
|
303
|
+
shared (bool): Whether to return shared tasks. default is False.
|
304
|
+
|
305
|
+
Returns:
|
306
|
+
List[Task] | int: The list of task objects or the count of the tasks if return_count is True.
|
307
|
+
|
308
|
+
Example:
|
309
|
+
>>> from geobox import GeoboxClient
|
310
|
+
>>> from geobox.task import Task
|
311
|
+
>>> client = GeoboxClient()
|
312
|
+
>>> tasks = Task.get_tasks(client)
|
313
|
+
or
|
314
|
+
>>> tasks = client.get_tasks()
|
315
|
+
"""
|
316
|
+
params = {
|
317
|
+
'f': 'json',
|
318
|
+
'state': kwargs.get('state').value if kwargs.get('state') else None,
|
319
|
+
'q': kwargs.get('q'),
|
320
|
+
'search': kwargs.get('search'),
|
321
|
+
'search_fields': kwargs.get('search_fields'),
|
322
|
+
'order_by': kwargs.get('order_by'),
|
323
|
+
'return_count': kwargs.get('return_count', False),
|
324
|
+
'skip': kwargs.get('skip'),
|
325
|
+
'limit': kwargs.get('limit'),
|
326
|
+
'user_id': kwargs.get('user_id'),
|
327
|
+
'shared': kwargs.get('shared', False)
|
328
|
+
}
|
329
|
+
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: Task(api, item['uuid'], item))
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
@classmethod
|
334
|
+
def get_task(cls, api: 'GeoboxClient', uuid: str) -> 'Task':
|
335
|
+
"""
|
336
|
+
Gets a task.
|
337
|
+
|
338
|
+
Args:
|
339
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
340
|
+
uuid (str): The UUID of the task.
|
341
|
+
|
342
|
+
Returns:
|
343
|
+
Task: The task object.
|
344
|
+
|
345
|
+
Example:
|
346
|
+
>>> from geobox import GeoboxClient
|
347
|
+
>>> from geobox.task import Task
|
348
|
+
>>> client = GeoboxClient()
|
349
|
+
>>> task = Task.get_task(client, uuid="12345678-1234-5678-1234-567812345678")
|
350
|
+
or
|
351
|
+
>>> task = client.get_task(uuid="12345678-1234-5678-1234-567812345678")
|
352
|
+
"""
|
353
|
+
return super()._get_detail(api, cls.BASE_ENDPOINT, uuid, factory_func=lambda api, item: Task(api, item['uuid'], item))
|
354
|
+
|