mirror of
https://github.com/aljazceru/gpt-engineer.git
synced 2025-12-17 12:45:26 +01:00
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
# This class represents a simple database that stores its data as files in a directory.
|
|
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()
|
|
|
|
self.path.mkdir(parents=True, exist_ok=True)
|
|
|
|
def __getitem__(self, key):
|
|
full_path = self.path / key
|
|
|
|
if full_path.is_file():
|
|
with full_path.open("r", encoding="utf-8") as f:
|
|
return f.read()
|
|
else:
|
|
raise KeyError(key)
|
|
|
|
def __setitem__(self, key, val):
|
|
full_path = self.path / key
|
|
full_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if isinstance(val, str):
|
|
full_path.write_text(val, encoding="utf-8")
|
|
else:
|
|
# If val is neither a string nor bytes, raise an error.
|
|
raise TypeError("val must be either a str or bytes")
|
|
|
|
|
|
# dataclass for all dbs:
|
|
@dataclass
|
|
class DBs:
|
|
memory: DB
|
|
logs: DB
|
|
identity: DB
|
|
input: DB
|
|
workspace: DB
|