mirror of
https://github.com/hansjone/netx.git
synced 2026-10-09 06:40:45 +08:00
Periodic vendor-aware CLI sync stores zlib snapshots in Postgres with dashboard, retry, and config viewer under Network Management. Co-authored-by: Cursor <cursoragent@cursor.com>
20 lines
587 B
Python
20 lines
587 B
Python
"""zlib + sha256 helpers for config blobs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import zlib
|
|
|
|
|
|
def compress_text(text: str) -> tuple[bytes, str, int, int]:
|
|
"""Return (zlib_bytes, sha256_hex, plain_size, zlib_size)."""
|
|
raw = str(text or "").encode("utf-8", errors="replace")
|
|
digest = hashlib.sha256(raw).hexdigest()
|
|
blob = zlib.compress(raw, level=6)
|
|
return blob, digest, len(raw), len(blob)
|
|
|
|
|
|
def decompress_text(blob: bytes | None) -> str:
|
|
if not blob:
|
|
return ""
|
|
return zlib.decompress(bytes(blob)).decode("utf-8", errors="replace")
|