Files
gpt-engineer/gpt_engineer/db.py
Anton Azarov 60e0a7e1dd Remove delete_existing option; Introduce archive (#409)
* Remove `delete_existing` option; Introduce archive

* Update gpt_engineer/db.py

* Update gpt_engineer/main.py

* Update gpt_engineer/main.py

* Update gpt_engineer/steps.py

* Update gpt_engineer/steps.py

---------

Co-authored-by: Anton Osika <anton.osika@gmail.com>
2023-07-02 15:56:31 +02:00

52 lines
1.4 KiB
Python

from dataclasses import dataclass
from pathlib import Path
from typing import Optional
# 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 __contains__(self, key):
return (self.path / key).is_file()
def __getitem__(self, key):
full_path = self.path / key
if not full_path.is_file():
raise KeyError(f"File '{key}' could not be found in '{self.path}'")
with full_path.open("r", encoding="utf-8") as f:
return f.read()
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
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
preprompts: DB
input: DB
workspace: DB
archive: DB