Files
gpt-engineer/gpt_engineer/db.py
Ryan Jalynski 6f63b8039d Update db.py
Ensure directory exists before attempting to write file in db.py
2023-06-16 19:01:27 -04:00

37 lines
835 B
Python

from dataclasses import dataclass
import os
from pathlib import Path
class DB:
"""A simple key-value store, where keys are filenames and values are file contents."""
def __init__(self, path):
self.path = Path(path).absolute()
os.makedirs(self.path, exist_ok=True)
def __getitem__(self, key):
with open(self.path / key, encoding='utf-8') as f:
return f.read()
def __setitem__(self, key, val):
# Ensure the directory exists
os.makedirs(self.path, exist_ok=True)
with open(self.path / key, 'w', encoding='utf-8') as f:
f.write(val)
def __contains__(self, key):
return (self.path / key).exists()
@dataclass
class DBs:
"""A dataclass for all dbs"""
memory: DB
logs: DB
identity: DB
input: DB
workspace: DB