add progress bar

This commit is contained in:
rmanach
2025-10-22 11:11:46 +02:00
parent 846e7cc4b4
commit 1144dae60b
6 changed files with 73 additions and 18 deletions
+1 -1
View File
@@ -113,7 +113,7 @@ def main():
try:
result = optimizer.optimize(fg)
except KeyboardInterrupt:
logging.info("optimizer stopped gracefully")
logging.error("oops, you stopped the optimizer abrutly...")
exit(0)
except Exception as e:
logging.fatal(
+20 -16
View File
@@ -3,10 +3,12 @@ import os
import signal
import subprocess
import time
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor, as_completed
from dataclasses import dataclass
from typing import Optional
from tqdm import tqdm
from .files import File, FileGroup, FileImgMimetype
__all__ = ["ImgOptimizer", "OptimizerResult"]
@@ -33,7 +35,7 @@ def _jpeg_optim(dest_dir: str, file: File) -> tuple["File", Optional["File"]] |
try:
_ = subprocess.run(cmd, shell=True, check=True)
except subprocess.CalledProcessError as e:
logging.error("error while running command: %s, err: %s", cmd, e.output)
logging.error("error while running command: %s, err: %s", cmd, e.stderr or "?")
return None
except Exception:
logging.error("unexpected error while running command: %s", cmd, exc_info=True)
@@ -130,20 +132,22 @@ class ImgOptimizer:
p.submit(_optim, self.dest_dir, f) for f in file_group.get_files()
]
file_group_optim = FileGroup()
optimized = 0
for f in futures:
if (res := f.result()) and res is not None:
match res:
case (orig, None):
logging.debug(f"no optimization for file: {orig}")
file_group_optim.add(orig)
case (orig, opti):
optimized += 1
logging.debug(
f"optimization for file: {orig} -> {(1 - (opti.size / orig.size)) * 100:.2f}%" # noqa
)
file_group_optim.add(opti)
file_group_optim = FileGroup()
optimized = 0
for f in tqdm(
as_completed(futures), total=len(futures), desc="Optimizing..."
):
if (res := f.result()) and res is not None:
match res:
case (orig, None):
logging.debug(f"no optimization for file: {orig}")
file_group_optim.add(orig)
case (orig, opti):
optimized += 1
logging.debug(
f"optimization for file: {orig} -> {(1 - (opti.size / orig.size)) * 100:.2f}%" # noqa
)
file_group_optim.add(opti)
logging.info(f"optimization finished in {time.perf_counter() - start:.2f}s")
return OptimizerResult(file_group, file_group_optim, optimized)