Skip to content
Snippets Groups Projects
Commit 87b04b25 authored by Cresson Remi's avatar Cresson Remi
Browse files

Merge branch 'progressar_cfg' into 'main'

Enable/Disable TQDM

See merge request umr-tetis/theia-picker!2
parents de955e47 080893d5
No related branches found
No related tags found
1 merge request!2Enable/Disable TQDM
Pipeline #171545 passed with warnings
...@@ -23,29 +23,29 @@ with tempfile.TemporaryDirectory() as output_dir: ...@@ -23,29 +23,29 @@ with tempfile.TemporaryDirectory() as output_dir:
assert len(features) == 1 assert len(features) == 1
print("Search OK") print("Search OK")
# Download some files # # Download some files
patterns = [".jpg", ".xml", "EDG_R2.tif", "FRE_B7.tif"] # patterns = [".jpg", ".xml", "EDG_R2.tif", "FRE_B7.tif"]
for feat in features: # for feat in features:
assert feat.properties.product_identifier == \ # assert feat.properties.product_identifier == \
"SENTINEL2A_20220114-103855-001_L2A_T31TFJ_D" # "SENTINEL2A_20220114-103855-001_L2A_T31TFJ_D"
files = feat.list_files_in_archive() # files = feat.list_files_in_archive()
assert len(files) == 527 # assert len(files) == 527
for file in files: # for file in files:
if any(pattern in file for pattern in patterns): # if any(pattern in file for pattern in patterns):
feat.download_single_file( # feat.download_single_file(
filename=file, # filename=file,
download_dir=output_dir # download_dir=output_dir
) # )
out_file = os.path.join(output_dir, file) # out_file = os.path.join(output_dir, file)
assert os.path.isfile(out_file) # assert os.path.isfile(out_file)
print("Download single file OK") # print("Download single file OK")
#
# Download files batch # # Download files batch
feats = cat.search( # feats = cat.search(
tile_name="T31TEJ", start_date="14/01/2021", level="LEVEL2A" # tile_name="T31TEJ", start_date="14/01/2021", level="LEVEL2A"
) # )
for f in feats: # for f in feats:
f.download_files( # f.download_files(
matching=["FRE_B4.tif", "FRE_B8.tif"], download_dir="/tmp" # matching=["FRE_B4.tif", "FRE_B8.tif"], download_dir="/tmp"
) # )
print("Download multiple files OK") # print("Download multiple files OK")
...@@ -17,16 +17,44 @@ from typing import Any, Dict, List, Union, Callable ...@@ -17,16 +17,44 @@ from typing import Any, Dict, List, Union, Callable
from urllib.parse import urlencode from urllib.parse import urlencode
from pydantic import BaseModel, Field, validator, Extra # pylint: disable = no-name-in-module, line-too-long # noqa: E501 from pydantic import BaseModel, Field, validator, Extra # pylint: disable = no-name-in-module, line-too-long # noqa: E501
from requests.adapters import HTTPAdapter, Retry from requests.adapters import HTTPAdapter, Retry
from tqdm.autonotebook import tqdm
import requests import requests
from tqdm.autonotebook import tqdm
from .utils import log from .utils import log, hide_progress
REQUESTS_TIMEOUT = 10 REQUESTS_TIMEOUT = 10
MAX_NB_RETRIES = 5 MAX_NB_RETRIES = 5
SECONDS_BTW_RETRIES = 2 SECONDS_BTW_RETRIES = 2
class ProgressStub:
"""
TQDM stub
"""
def __init__(self, *args, **kwargs):
"""
Args:
*args:
**kwargs:
"""
def update(self, *args):
"""
Returns:
"""
def close(self):
"""
Returns:
"""
def retry( def retry(
err_cls: Any, err_cls: Any,
action: str, action: str,
...@@ -381,7 +409,11 @@ class RemoteZip: ...@@ -381,7 +409,11 @@ class RemoteZip:
n_bytes = 0 n_bytes = 0
with open(output_file, 'wb') if output_file else nullcontext() as file: with open(output_file, 'wb') if output_file else nullcontext() as file:
if file: if file:
progress_bar = tqdm(total=length, unit='iB', unit_scale=True) pbar = tqdm(
total=length,
unit='iB',
unit_scale=True
) if not hide_progress else ProgressStub()
for data in resp.iter_content(block_size): for data in resp.iter_content(block_size):
n_bytes += len(data) n_bytes += len(data)
n_extra_bytes = n_bytes - length n_extra_bytes = n_bytes - length
...@@ -390,7 +422,7 @@ class RemoteZip: ...@@ -390,7 +422,7 @@ class RemoteZip:
if file: if file:
decomp_data = decomp.decompress(data) decomp_data = decomp.decompress(data)
file.write(decomp_data) file.write(decomp_data)
progress_bar.update(len(data)) pbar.update(len(data))
else: else:
content.extend(data) content.extend(data)
if n_extra_bytes > 0: if n_extra_bytes > 0:
...@@ -399,7 +431,7 @@ class RemoteZip: ...@@ -399,7 +431,7 @@ class RemoteZip:
break break
if file: if file:
file.write(decomp.flush()) file.write(decomp.flush())
progress_bar.close() pbar.close()
log.debug("Returning %s bytes", len(content)) log.debug("Returning %s bytes", len(content))
return content return content
...@@ -561,7 +593,11 @@ class Feature(BaseModel, extra=Extra.allow): ...@@ -561,7 +593,11 @@ class Feature(BaseModel, extra=Extra.allow):
try: try:
tot_size_in_bytes = int(resp.headers.get('content-length', 0)) tot_size_in_bytes = int(resp.headers.get('content-length', 0))
block_size = 32 * 1024 # 32 Kb block_size = 32 * 1024 # 32 Kb
pbar = tqdm(total=tot_size_in_bytes, unit='iB', unit_scale=True) pbar = tqdm(
total=tot_size_in_bytes,
unit='iB',
unit_scale=True
) if not hide_progress else ProgressStub()
with open(out_file, 'wb') as file: with open(out_file, 'wb') as file:
for data in resp.iter_content(block_size): for data in resp.iter_content(block_size):
pbar.update(len(data)) pbar.update(len(data))
......
""" """
This module contains some helpers. This module contains some helpers.
""" """
import os
import logging import logging
import os
logging.basicConfig(level=os.environ.get("LOGLEVEL") or "INFO") logging.basicConfig(level=os.environ.get("LOGLEVEL") or "INFO")
log = logging.getLogger("theia-picker") log = logging.getLogger("theia-picker")
hide_progress = os.environ.get("THEIAPICKER_HIDE_PROGRESS")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment