Skip to content

refactor(utils): extract Ghostscript compression into app/pdf_compress (R5.1) - #160

Open
nelsonduarte wants to merge 2 commits into
mainfrom
refactor/split-utils-compress
Open

refactor(utils): extract Ghostscript compression into app/pdf_compress (R5.1)#160
nelsonduarte wants to merge 2 commits into
mainfrom
refactor/split-utils-compress

Conversation

@nelsonduarte

Copy link
Copy Markdown
Owner

R5 (incremento 1) do plano de modularidade: parte o junk-drawer utils.py extraindo o cluster de compressao Ghostscript (_find_gs, _win_short_path, _compress_pdf, _COMPRESS_LEVELS, _GS_CACHE) para o novo modulo app/pdf_compress.py.

Detalhes

  • Dependencia unidirecional pdf_compress -> utils (sem ciclo).
  • Simbolos partilhados (CancelledError / WrongPasswordError / _is_valid_pdf) ficam em utils.py.
  • Importadores atualizados: app/tools/compress.py + 3 testes (tests/test_encrypted_pdf_tools.py, tests/test_audit_pr_j.py, tests/test_audit_mediums_lows.py).
  • ZERO alteracao de comportamento (cluster movido byte-identico, incl. _compress_pdf).
  • Suite: 585 passed / 0 failed.
  • Aprovado por revisao adversarial.

Nota

Primeiro de varios incrementos a partir de utils.py (as UI factories virao depois). Depende de R1/R2/R3 (ja em main).

Split the most cohesive cluster out of the app/utils.py 'junk drawer'
(R5 increment 1): the Ghostscript-backed compression pipeline.

app/pdf_compress.py now owns _find_gs, _win_short_path, _compress_pdf
plus the compression-only _COMPRESS_LEVELS and _GS_CACHE. The
implementations are copied faithfully with zero behaviour change.

Shared PDF helpers stay in app.utils and are imported by pdf_compress
(one-directional dependency, no import cycle):
  - CancelledError (also used by worker.py + tools/convert.py)
  - WrongPasswordError (shared error type, imported by tests)
  - _is_valid_pdf (general PDF-validity helper, tested independently)

Importers updated (option a, small surface):
  - app/tools/compress.py imports _compress_pdf / _find_gs from
    app.pdf_compress; utils-only helpers still from app.utils
  - tests/test_encrypted_pdf_tools.py, tests/test_audit_pr_j.py and
    tests/test_audit_mediums_lows.py updated (imports + the two
    source-reading assertions now target app/pdf_compress.py)

Full suite: 585 passed / 2 skipped. ruff (F,E9,B) clean on the three
app modules.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
Comment thread app/pdf_compress.py Fixed
…_CACHE to satisfy CodeQL

Resolves 17 CodeQL alerts introduced when the Ghostscript compression
pipeline was moved verbatim from app.utils into app.pdf_compress (R5.1);
they were previously grandfathered on the old file.

- py/empty-except (16x): add an honest comment to each 'except ... : pass'
  explaining why the exception is safely swallowed (best-effort temp
  cleanup, optional pass fallbacks, best-effort scrub/subset/rewrite,
  eager close to release Windows file handles). No exception type or
  control flow changed.
- py/unused-global-variable (_GS_CACHE): the negative-result store
  '_GS_CACHE = (True, None)' was flagged because its value is never read
  before the immediate 'return None'. It IS meaningful across calls (it
  short-circuits the slow Windows glob on machines without gs). Return
  through the cache tuple instead ('return _GS_CACHE[1]', which is None
  here) so the store is a read for the analyser, mirroring the other two
  return paths. Behaviour is identical.

Comments-only plus one behaviour-preserving return; verified via git diff.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 11, 2026

Copy link
Copy Markdown

Deploying pdfapps with  Cloudflare Pages  Cloudflare Pages

Latest commit: e067c37
Status: ✅  Deploy successful!
Preview URL: https://d483a16d.pdfapps.pages.dev
Branch Preview URL: https://refactor-split-utils-compres.pdfapps.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants