cryogenic22 commited on
Commit
3bd2f1c
·
verified ·
1 Parent(s): f2be5a7

Create data_manager.py

Browse files
Files changed (1) hide show
  1. data_manager.py +123 -0
data_manager.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # File: data_manager.py
2
+
3
+ import os
4
+ import json
5
+ import datetime
6
+ from typing import Dict, Any, Optional
7
+
8
+ class DataManager:
9
+ def __init__(self, base_path: str = '/data'):
10
+ """Initialize the data manager"""
11
+ self.base_path = base_path
12
+ self._ensure_directories()
13
+
14
+ def _ensure_directories(self):
15
+ """Create necessary directory structure"""
16
+ directories = [
17
+ self.base_path,
18
+ os.path.join(self.base_path, 'blueprints'),
19
+ os.path.join(self.base_path, 'book_content'),
20
+ os.path.join(self.base_path, 'exports'),
21
+ os.path.join(self.base_path, 'chapters')
22
+ ]
23
+
24
+ for dir_path in directories:
25
+ os.makedirs(dir_path, exist_ok=True)
26
+
27
+ def save_blueprint(self, blueprint: str) -> str:
28
+ """Save blueprint to file"""
29
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
30
+ filename = f"blueprint_{timestamp}.txt"
31
+ filepath = os.path.join(self.base_path, 'blueprints', filename)
32
+
33
+ try:
34
+ with open(filepath, 'w') as f:
35
+ f.write(blueprint)
36
+ return filename
37
+ except Exception as e:
38
+ raise Exception(f"Error saving blueprint: {e}")
39
+
40
+ def load_latest_blueprint(self) -> Optional[str]:
41
+ """Load most recent blueprint"""
42
+ blueprint_dir = os.path.join(self.base_path, 'blueprints')
43
+ try:
44
+ files = os.listdir(blueprint_dir)
45
+ if not files:
46
+ return None
47
+
48
+ latest_file = max(files, key=lambda f: os.path.getctime(
49
+ os.path.join(blueprint_dir, f)
50
+ ))
51
+
52
+ with open(os.path.join(blueprint_dir, latest_file), 'r') as f:
53
+ return f.read()
54
+ except Exception as e:
55
+ raise Exception(f"Error loading blueprint: {e}")
56
+
57
+ def save_chapter(self, part_idx: int, chapter_idx: int, content: Dict[str, Any]):
58
+ """Save individual chapter content"""
59
+ filename = f"part_{part_idx}_chapter_{chapter_idx}.json"
60
+ filepath = os.path.join(self.base_path, 'chapters', filename)
61
+
62
+ try:
63
+ with open(filepath, 'w') as f:
64
+ json.dump(content, f, indent=4)
65
+ except Exception as e:
66
+ raise Exception(f"Error saving chapter: {e}")
67
+
68
+ def save_book_content(self, content: Dict[str, Any]) -> str:
69
+ """Save complete book content"""
70
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
71
+ filename = f"book_content_{timestamp}.json"
72
+ filepath = os.path.join(self.base_path, 'book_content', filename)
73
+
74
+ try:
75
+ with open(filepath, 'w') as f:
76
+ json.dump(content, f, indent=4)
77
+ return filename
78
+ except Exception as e:
79
+ raise Exception(f"Error saving book content: {e}")
80
+
81
+ def load_latest_book_content(self) -> Optional[Dict[str, Any]]:
82
+ """Load most recent book content"""
83
+ content_dir = os.path.join(self.base_path, 'book_content')
84
+ try:
85
+ files = os.listdir(content_dir)
86
+ if not files:
87
+ return None
88
+
89
+ latest_file = max(files, key=lambda f: os.path.getctime(
90
+ os.path.join(content_dir, f)
91
+ ))
92
+
93
+ with open(os.path.join(content_dir, latest_file), 'r') as f:
94
+ return json.load(f)
95
+ except Exception as e:
96
+ raise Exception(f"Error loading book content: {e}")
97
+
98
+ def export_markdown(self, content: Dict[str, Any]) -> str:
99
+ """Export book content as markdown"""
100
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
101
+ filename = f"self_api_book_{timestamp}.md"
102
+ filepath = os.path.join(self.base_path, 'exports', filename)
103
+
104
+ try:
105
+ markdown_content = "# Self.api: A Modern Spiritual Operating System\n\n"
106
+
107
+ # Add introduction
108
+ markdown_content += "## Introduction\n\n"
109
+ markdown_content += content.get('introduction', '') + "\n\n"
110
+
111
+ # Add parts and chapters
112
+ for part_idx, part in enumerate(content.get('parts', [])):
113
+ markdown_content += f"# Part {part_idx + 1}: {part['title']}\n\n"
114
+ for chapter in part.get('chapters', []):
115
+ markdown_content += f"## {chapter['title']}\n\n"
116
+ markdown_content += chapter.get('content', '') + "\n\n"
117
+
118
+ with open(filepath, 'w') as f:
119
+ f.write(markdown_content)
120
+
121
+ return filepath
122
+ except Exception as e:
123
+ raise Exception(f"Error exporting markdown: {e}")