Refactor compression flow with routing seam
Introduce compress_image_with_routing and compress_raster_image to prepare extensible backend routing while keeping existing behavior unchanged. Add unit test that verifies routing currently delegates to raster compressor. Co-Authored-By: Abacus.AI CLI <agent@abacus.ai>
This commit is contained in:
+37
-1
@@ -218,6 +218,42 @@ def run_compressor(
|
|||||||
return compressor(original, out_dir, caesium_threads, quality, min_savings)
|
return compressor(original, out_dir, caesium_threads, quality, min_savings)
|
||||||
|
|
||||||
|
|
||||||
|
def compress_raster_image(
|
||||||
|
compressor: Callable[..., Path | None],
|
||||||
|
original: Path,
|
||||||
|
out_dir: Path,
|
||||||
|
caesium_threads: int | None,
|
||||||
|
quality: int,
|
||||||
|
min_savings: str,
|
||||||
|
) -> Path | None:
|
||||||
|
return run_compressor(
|
||||||
|
compressor=compressor,
|
||||||
|
original=original,
|
||||||
|
out_dir=out_dir,
|
||||||
|
caesium_threads=caesium_threads,
|
||||||
|
quality=quality,
|
||||||
|
min_savings=min_savings,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def compress_image_with_routing(
|
||||||
|
compressor: Callable[..., Path | None],
|
||||||
|
original: Path,
|
||||||
|
out_dir: Path,
|
||||||
|
caesium_threads: int | None,
|
||||||
|
quality: int,
|
||||||
|
min_savings: str,
|
||||||
|
) -> Path | None:
|
||||||
|
return compress_raster_image(
|
||||||
|
compressor=compressor,
|
||||||
|
original=original,
|
||||||
|
out_dir=out_dir,
|
||||||
|
caesium_threads=caesium_threads,
|
||||||
|
quality=quality,
|
||||||
|
min_savings=min_savings,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def update_relationship_targets(work_dir: Path, old_name: str, new_name: str) -> None:
|
def update_relationship_targets(work_dir: Path, old_name: str, new_name: str) -> None:
|
||||||
rels_namespace = "{http://schemas.openxmlformats.org/package/2006/relationships}Relationship"
|
rels_namespace = "{http://schemas.openxmlformats.org/package/2006/relationships}Relationship"
|
||||||
for rels_file in work_dir.rglob("*.rels"):
|
for rels_file in work_dir.rglob("*.rels"):
|
||||||
@@ -321,7 +357,7 @@ def process_image_file(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
out_sub = scratch_dir / f"img_{idx:06d}"
|
out_sub = scratch_dir / f"img_{idx:06d}"
|
||||||
caesium_out = run_compressor(
|
caesium_out = compress_image_with_routing(
|
||||||
compressor=compressor,
|
compressor=compressor,
|
||||||
original=img_path,
|
original=img_path,
|
||||||
out_dir=out_sub,
|
out_dir=out_sub,
|
||||||
|
|||||||
@@ -244,5 +244,34 @@ class TestPptxImageCompress(unittest.TestCase):
|
|||||||
self.assertIn("png_jpg", log_text)
|
self.assertIn("png_jpg", log_text)
|
||||||
|
|
||||||
|
|
||||||
|
def test_compress_image_with_routing_delegates_to_raster(self):
|
||||||
|
with tempfile.TemporaryDirectory() as td:
|
||||||
|
root = Path(td)
|
||||||
|
original = root / "image1.png"
|
||||||
|
original.write_bytes(b"A" * 100)
|
||||||
|
out_dir = root / "out"
|
||||||
|
|
||||||
|
def fake_compressor(original_path: Path, out_subdir: Path, caesium_threads: int | None, quality: int, min_savings: str):
|
||||||
|
out_subdir.mkdir(parents=True, exist_ok=True)
|
||||||
|
out = out_subdir / original_path.name
|
||||||
|
out.write_bytes(b"B" * 80)
|
||||||
|
return out
|
||||||
|
|
||||||
|
out = pic.compress_image_with_routing(
|
||||||
|
compressor=fake_compressor,
|
||||||
|
original=original,
|
||||||
|
out_dir=out_dir,
|
||||||
|
caesium_threads=1,
|
||||||
|
quality=90,
|
||||||
|
min_savings="2%",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIsNotNone(out)
|
||||||
|
if out is None:
|
||||||
|
self.fail("Output should not be None")
|
||||||
|
self.assertEqual(out.name, "image1.png")
|
||||||
|
self.assertEqual(out.stat().st_size, 80)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user