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:
assert len(features) == 1
print("Search OK")
# Download some files
patterns = [".jpg", ".xml", "EDG_R2.tif", "FRE_B7.tif"]
for feat in features:
assert feat.properties.product_identifier == \
"SENTINEL2A_20220114-103855-001_L2A_T31TFJ_D"
files = feat.list_files_in_archive()
assert len(files) == 527
for file in files:
if any(pattern in file for pattern in patterns):
feat.download_single_file(
filename=file,
download_dir=output_dir
)
out_file = os.path.join(output_dir, file)
assert os.path.isfile(out_file)
print("Download single file OK")
# Download files batch
feats = cat.search(
tile_name="T31TEJ", start_date="14/01/2021", level="LEVEL2A"
)
for f in feats:
f.download_files(
matching=["FRE_B4.tif", "FRE_B8.tif"], download_dir="/tmp"
)
print("Download multiple files OK")
# # Download some files
# patterns = [".jpg", ".xml", "EDG_R2.tif", "FRE_B7.tif"]
# for feat in features:
# assert feat.properties.product_identifier == \
# "SENTINEL2A_20220114-103855-001_L2A_T31TFJ_D"
# files = feat.list_files_in_archive()
# assert len(files) == 527
# for file in files:
# if any(pattern in file for pattern in patterns):
# feat.download_single_file(
# filename=file,
# download_dir=output_dir
# )
# out_file = os.path.join(output_dir, file)
# assert os.path.isfile(out_file)
# print("Download single file OK")
#
# # Download files batch
# feats = cat.search(
# tile_name="T31TEJ", start_date="14/01/2021", level="LEVEL2A"
# )
# for f in feats:
# f.download_files(
# matching=["FRE_B4.tif", "FRE_B8.tif"], download_dir="/tmp"
# )
# print("Download multiple files OK")
......@@ -17,16 +17,44 @@ from typing import Any, Dict, List, Union, Callable
from urllib.parse import urlencode
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 tqdm.autonotebook import tqdm
import requests
from tqdm.autonotebook import tqdm
from .utils import log
from .utils import log, hide_progress
REQUESTS_TIMEOUT = 10
MAX_NB_RETRIES = 5
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(
err_cls: Any,
action: str,
......@@ -381,7 +409,11 @@ class RemoteZip:
n_bytes = 0
with open(output_file, 'wb') if output_file else nullcontext() as 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):
n_bytes += len(data)
n_extra_bytes = n_bytes - length
......@@ -390,7 +422,7 @@ class RemoteZip:
if file:
decomp_data = decomp.decompress(data)
file.write(decomp_data)
progress_bar.update(len(data))
pbar.update(len(data))
else:
content.extend(data)
if n_extra_bytes > 0:
......@@ -399,7 +431,7 @@ class RemoteZip:
break
if file:
file.write(decomp.flush())
progress_bar.close()
pbar.close()
log.debug("Returning %s bytes", len(content))
return content
......@@ -561,7 +593,11 @@ class Feature(BaseModel, extra=Extra.allow):
try:
tot_size_in_bytes = int(resp.headers.get('content-length', 0))
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:
for data in resp.iter_content(block_size):
pbar.update(len(data))
......
"""
This module contains some helpers.
"""
import os
import logging
import os
logging.basicConfig(level=os.environ.get("LOGLEVEL") or "INFO")
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