Ahmed Tarek
changes
80f913d
raw
history blame contribute delete
858 Bytes
from tinydb import TinyDB
from filelock import FileLock
import os
import json
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class BaseDB:
def __init__(self):
self.db_path = "/.cache/huggingface/hub/my_app_data/db/database.json"
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
try:
with FileLock(f"{self.db_path}.lock"):
# Handle corruption
try:
self.db = TinyDB(self.db_path)
except json.JSONDecodeError:
logger.warning("DB corrupted - resetting")
os.rename(self.db_path, f"{self.db_path}.bak")
self.db = TinyDB(self.db_path)
except Exception as e:
logger.error(f"DB init failed: {e}")
raise