diff --git a/docs/docs/API.md b/docs/docs/API.md index 4f50ef5..2d2be1f 100644 --- a/docs/docs/API.md +++ b/docs/docs/API.md @@ -108,22 +108,28 @@ with MemoryFile() as mem_dst: client.upload_fileobj(mem_dst, "my-bucket", "my-key") ``` -3. Progress to TextIO +3. Output Progress to Alternative Text Buffer +Use Case: You may want to run your translation tasks in the background and keep +track of progress. To do so you can utilize an alternative text buffer and another +thread. By outputting the progress to a seperate text buffer you can then track +the translation progress without blocking the program. ```python from rio_cogeo.cogeo import cog_translate from rio_cogeo.profiles import cog_profiles -config = dict( - GDAL_NUM_THREADS="ALL_CPUS", - GDAL_TIFF_INTERNAL_MASK=True, - GDAL_TIFF_OVR_BLOCKSIZE="128", -) +config = { + "GDAL_NUM_THREADS": "ALL_CPUS", + "GDAL_TIFF_INTERNAL_MASK": True, + "GDAL_TIFF_OVR_BLOCKSIZE": "128", +} + +with open("logfile.txt", "w+") as buffer: + # Progress output buffer must be interactive + buffer.isatty = lambda: True -with open("logfile.txt", "w+") as example: - example.isatty = lambda: True # Enable Interactive File like terminal cog_translate( "example-input.tif", "example-output.tif", @@ -131,6 +137,18 @@ with open("logfile.txt", "w+") as example: config=config, in_memory=False, nodata=0, - quiet=example, + quiet=False, + progress_out=buffer, ) ``` + +Below is a snippet of code that allows you to grab the percentage complete a +translation is using the text buffer. + +```python +import re + +def getPercentage(buffer:str) -> float: + return int(re.findall("\d*%", buffer)[-1].replace("%", "")) / 100 +``` + diff --git a/rio_cogeo/cogeo.py b/rio_cogeo/cogeo.py index ba7c0ed..670a10f 100644 --- a/rio_cogeo/cogeo.py +++ b/rio_cogeo/cogeo.py @@ -92,7 +92,8 @@ def cog_translate( # noqa: C901 allow_intermediate_compression: bool = False, forward_band_tags: bool = False, forward_ns_tags: bool = False, - quiet: Union[bool, TextIO] = False, + quiet: bool = False, + progress_out: Optional[TextIO] = None, temporary_compression: str = "DEFLATE", colormap: Optional[Dict] = None, additional_cog_metadata: Optional[Dict] = None, @@ -153,8 +154,10 @@ def cog_translate( # noqa: C901 Ref: https://github.com/cogeotiff/rio-cogeo/issues/19 forward_ns_tags: bool, optional Forward namespaces tags to output dataset. - quiet: bool, TextIO, optional (default: False) - Mask processing steps. Define the output buffer for the progress bar. + quiet: bool, optional (default: False) + Mask processing steps. + progress_out: TextIO, optional + Output progress steps to alternative text buffer. Quiet must be False. temporary_compression: str, optional Compression used for the intermediate file, default is deflate. colormap: dict, optional @@ -301,11 +304,9 @@ def cog_translate( # noqa: C901 if not quiet: click.echo("Reading input: {}".format(source), err=True) - fout = sys.stderr - if quiet is True: - fout = ctx.enter_context(open(os.devnull, "w")) - elif quiet is not False: - fout = quiet + fout = ctx.enter_context(open(os.devnull, "w")) if quiet else sys.stderr + if quiet is False and progress_out: + fout = progress_out with click.progressbar(wind, file=fout, show_percent=True) as windows: # type: ignore for _, w in windows: