forked from gpt-engineer-org/gpt-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
65 lines (50 loc) · 1.7 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import datetime
import shutil
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 __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
def archive(dbs: DBs):
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
shutil.move(
str(dbs.memory.path), str(dbs.archive.path / timestamp / dbs.memory.path.name)
)
shutil.move(
str(dbs.workspace.path),
str(dbs.archive.path / timestamp / dbs.workspace.path.name),
)
return []