feat(01-02): add Peewee models, database init, and driver storage
- imptune/db/database.py: deferred SqliteDatabase with WAL + foreign_keys pragmas and init_db() - imptune/db/models.py: full schema (Client, Driver, Printer, Icon) for phases 1-5 - imptune/storage/driver_store.py: SHA256 content-addressed DriverStore with dedup - tests/test_db.py: 7 TDD tests covering all db and storage behaviors
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# Storage package
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Content-addressed file storage for driver packages."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class DriverStore:
|
||||
"""SHA256 content-addressed file storage for printer driver packages.
|
||||
|
||||
Files are stored as DRIVERS_DIR/{sha256} so identical uploads are
|
||||
deduplicated automatically.
|
||||
"""
|
||||
|
||||
def __init__(self, base_dir: str) -> None:
|
||||
self._base = Path(base_dir)
|
||||
self._base.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def save(self, data: bytes) -> str:
|
||||
"""Persist *data* and return its SHA256 hex digest.
|
||||
|
||||
If the file already exists the write is skipped (deduplication).
|
||||
"""
|
||||
digest = hashlib.sha256(data).hexdigest()
|
||||
dest = self._base / digest
|
||||
if not dest.exists():
|
||||
dest.write_bytes(data)
|
||||
return digest
|
||||
|
||||
def get_path(self, sha256: str) -> Path:
|
||||
"""Return the filesystem path for a given SHA256 digest."""
|
||||
return self._base / sha256
|
||||
|
||||
def exists(self, sha256: str) -> bool:
|
||||
"""Return True if the file for *sha256* exists on disk."""
|
||||
return (self._base / sha256).exists()
|
||||
Reference in New Issue
Block a user