forked from stitionai/devika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
142 lines (119 loc) · 5.81 KB
/
project.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import json
import zipfile
from datetime import datetime
from typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine
from src.config import Config
class Projects(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
project: str
message_stack_json: str
class ProjectManager:
def __init__(self):
config = Config()
sqlite_path = config.get_sqlite_db()
self.project_path = config.get_projects_dir()
self.engine = create_engine(f"sqlite:///{sqlite_path}")
SQLModel.metadata.create_all(self.engine)
def new_message(self):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return {
"from_devika": True,
"message": None,
"timestamp": timestamp
}
def create_project(self, project: str):
with Session(self.engine) as session:
project_state = Projects(project=project, message_stack_json=json.dumps([]))
session.add(project_state)
session.commit()
def delete_project(self, project: str):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
session.delete(project_state)
session.commit()
def add_message_to_project(self, project: str, message: dict):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
message_stack = json.loads(project_state.message_stack_json)
message_stack.append(message)
project_state.message_stack_json = json.dumps(message_stack)
session.commit()
else:
message_stack = [message]
project_state = Projects(project=project, message_stack_json=json.dumps(message_stack))
session.add(project_state)
session.commit()
def add_message_from_devika(self, project: str, message: str):
new_message = self.new_message()
new_message["message"] = message
self.add_message_to_project(project, new_message)
def add_message_from_user(self, project: str, message: str):
new_message = self.new_message()
new_message["message"] = message
new_message["from_devika"] = False
self.add_message_to_project(project, new_message)
def get_messages(self, project: str):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
return json.loads(project_state.message_stack_json)
return None
def get_latest_message_from_user(self, project: str):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
message_stack = json.loads(project_state.message_stack_json)
for message in reversed(message_stack):
if not message["from_devika"]:
return message
return None
def validate_last_message_is_from_user(self, project: str):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
message_stack = json.loads(project_state.message_stack_json)
if message_stack:
return not message_stack[-1]["from_devika"]
return False
def get_latest_message_from_devika(self, project: str):
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
message_stack = json.loads(project_state.message_stack_json)
for message in reversed(message_stack):
if message["from_devika"]:
return message
return None
def get_project_list(self):
with Session(self.engine) as session:
projects = session.query(Projects).all()
return [project.project for project in projects]
def get_all_messages_formatted(self, project: str):
formatted_messages = []
with Session(self.engine) as session:
project_state = session.query(Projects).filter(Projects.project == project).first()
if project_state:
message_stack = json.loads(project_state.message_stack_json)
for message in message_stack:
if message["from_devika"]:
formatted_messages.append(f"Devika: {message['message']}")
else:
formatted_messages.append(f"User: {message['message']}")
return formatted_messages
def get_project_path(self, project: str):
return os.path.join(self.project_path, project.lower().replace(" ", "-"))
def project_to_zip(self, project: str):
project_path = self.get_project_path(project)
zip_path = f"{project_path}.zip"
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(project_path):
for file in files:
relative_path = os.path.relpath(os.path.join(root, file), os.path.join(project_path, '..'))
zipf.write(os.path.join(root, file), arcname=relative_path)
return zip_path
def get_zip_path(self, project: str):
return f"{self.get_project_path(project)}.zip"