Spaces:
Sleeping
Sleeping
File size: 7,999 Bytes
22c9c7b |
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File: data_persistence.py
# Location: /data_persistence.py
# Description: Robust data persistence and serialization for book writing project
import os
import json
import sqlite3
from typing import Dict, Any
import streamlit as st
class ProjectPersistenceManager:
def __init__(self, project_id: str = None):
"""
Initialize persistent storage for book writing projects
Args:
project_id (str, optional): Unique identifier for the project
"""
# Ensure data directory exists
os.makedirs('project_data', exist_ok=True)
# SQLite Database Setup
self.db_path = 'project_data/book_projects.db'
self.conn = sqlite3.connect(self.db_path)
self._create_tables()
def _create_tables(self):
"""
Create necessary tables for project persistence
"""
cursor = self.conn.cursor()
# Project Metadata Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS projects (
project_id TEXT PRIMARY KEY,
title TEXT,
genre TEXT,
total_chapters INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Chapters Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS chapters (
project_id TEXT,
chapter_number INTEGER,
content TEXT,
status TEXT,
generated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (project_id, chapter_number),
FOREIGN KEY (project_id) REFERENCES projects (project_id)
)
''')
# Book Concept Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS book_concepts (
project_id TEXT PRIMARY KEY,
concept_data TEXT,
FOREIGN KEY (project_id) REFERENCES projects (project_id)
)
''')
self.conn.commit()
def save_project_metadata(self, project_data: Dict[str, Any]):
"""
Save project metadata to persistent storage
Args:
project_data (Dict): Project metadata dictionary
"""
cursor = self.conn.cursor()
# Upsert project metadata
cursor.execute('''
INSERT OR REPLACE INTO projects
(project_id, title, genre, total_chapters)
VALUES (?, ?, ?, ?)
''', (
project_data.get('project_id', 'unknown'),
project_data.get('title', 'Untitled'),
project_data.get('genre', 'Unspecified'),
project_data.get('total_chapters', 0)
))
self.conn.commit()
def save_book_concept(self, project_id: str, concept_data: Dict[str, Any]):
"""
Save book concept to persistent storage
Args:
project_id (str): Unique project identifier
concept_data (Dict): Book concept details
"""
cursor = self.conn.cursor()
# Upsert book concept
cursor.execute('''
INSERT OR REPLACE INTO book_concepts
(project_id, concept_data)
VALUES (?, ?)
''', (
project_id,
json.dumps(concept_data)
))
self.conn.commit()
def save_chapter(
self,
project_id: str,
chapter_number: int,
content: str,
status: str = 'Generated'
):
"""
Save chapter content to persistent storage
Args:
project_id (str): Unique project identifier
chapter_number (int): Chapter number
content (str): Chapter content
status (str): Chapter status
"""
cursor = self.conn.cursor()
# Upsert chapter
cursor.execute('''
INSERT OR REPLACE INTO chapters
(project_id, chapter_number, content, status)
VALUES (?, ?, ?, ?)
''', (
project_id,
chapter_number,
content,
status
))
self.conn.commit()
def load_project_metadata(self, project_id: str) -> Dict[str, Any]:
"""
Load project metadata from persistent storage
Args:
project_id (str): Unique project identifier
Returns:
Dict containing project metadata
"""
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM projects WHERE project_id = ?', (project_id,))
project = cursor.fetchone()
if project:
return {
'project_id': project[0],
'title': project[1],
'genre': project[2],
'total_chapters': project[3]
}
return {}
def load_book_concept(self, project_id: str) -> Dict[str, Any]:
"""
Load book concept from persistent storage
Args:
project_id (str): Unique project identifier
Returns:
Dict containing book concept
"""
cursor = self.conn.cursor()
cursor.execute('SELECT concept_data FROM book_concepts WHERE project_id = ?', (project_id,))
concept = cursor.fetchone()
return json.loads(concept[0]) if concept else {}
def load_chapters(self, project_id: str) -> Dict[int, Dict]:
"""
Load all chapters for a project
Args:
project_id (str): Unique project identifier
Returns:
Dict of chapters with chapter number as key
"""
cursor = self.conn.cursor()
cursor.execute('SELECT chapter_number, content, status FROM chapters WHERE project_id = ?', (project_id,))
chapters = {}
for chapter_num, content, status in cursor.fetchall():
chapters[chapter_num] = {
'content': content,
'status': status
}
return chapters
def close(self):
"""
Close database connection
"""
self.conn.close()
# Test functionality
def test_persistence():
"""
Run comprehensive tests for data persistence
"""
# Initialize persistence manager
pm = ProjectPersistenceManager()
# Test project metadata
test_project = {
'project_id': 'test_project_1',
'title': 'Test Book',
'genre': 'Science Fiction',
'total_chapters': 10
}
# Save project metadata
pm.save_project_metadata(test_project)
# Load and verify project metadata
loaded_project = pm.load_project_metadata('test_project_1')
assert loaded_project == test_project, "Project metadata not saved/loaded correctly"
# Test book concept
test_concept = {
'narrative_premise': 'A story about AI and humanity',
'core_themes': ['Technology', 'Ethics']
}
pm.save_book_concept('test_project_1', test_concept)
# Load and verify book concept
loaded_concept = pm.load_book_concept('test_project_1')
assert loaded_concept == test_concept, "Book concept not saved/loaded correctly"
# Test chapter saving and loading
pm.save_chapter('test_project_1', 1, "Chapter 1 content", "Generated")
pm.save_chapter('test_project_1', 2, "Chapter 2 content", "In Review")
# Load chapters
loaded_chapters = pm.load_chapters('test_project_1')
assert len(loaded_chapters) == 2, "Chapters not saved/loaded correctly"
assert loaded_chapters[1]['content'] == "Chapter 1 content", "Chapter 1 content mismatch"
print("All persistence tests passed successfully!")
# Run tests if script is executed directly
if __name__ == "__main__":
test_persistence() |