Repository navigation
Expand file tree
/
Copy pathstorage.py
More file actions
58 lines (44 loc) · 1.64 KB
/
Copy pathstorage.py
File metadata and controls
58 lines (44 loc) · 1.64 KB
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
"""
Storage - Data persistence layer
Handles data storage and retrieval
"""
import json
import os
class Storage:
"""Storage management for Aletheia"""
def __init__(self, storage_path=".aletheia_data"):
"""Initialize storage"""
self.storage_path = storage_path
self.connected = False
def connect(self):
"""Connect to storage"""
if not os.path.exists(self.storage_path):
os.makedirs(self.storage_path)
self.connected = True
print(f"Storage: Connected to {self.storage_path}")
def save(self, data, key=None):
"""Save data to storage"""
if not self.connected:
raise RuntimeError("Storage not connected")
if key is None:
import time
key = f"data_{int(time.time() * 1000)}"
filepath = os.path.join(self.storage_path, f"{key}.json")
with open(filepath, 'w') as f:
json.dump(data, f, default=str)
return key
def load(self, key):
"""Load data from storage"""
if not self.connected:
raise RuntimeError("Storage not connected")
filepath = os.path.join(self.storage_path, f"{key}.json")
if not os.path.exists(filepath):
return None
with open(filepath, 'r') as f:
return json.load(f)
def list_keys(self):
"""List all stored keys"""
if not self.connected:
raise RuntimeError("Storage not connected")
files = os.listdir(self.storage_path)
return [f.replace('.json', '') for f in files if f.endswith('.json')]