Files
colony/colony/core/storage.py
T
2026-01-25 10:23:02 +01:00

61 lines
1.3 KiB
Python

import os
import json
from enum import Enum
STORAGE_DIR = "storage"
class Storage:
def __init__(self, storage_dir):
self.storage_dir = storage_dir
os.makedirs(self.storage_dir, exist_ok=True)
def get_path(self, filename):
return os.path.join(self.storage_dir, filename)
def ls(self, subdir):
path = os.path.join(self.storage_dir, subdir)
if not os.path.exists(path):
return []
return os.listdir(path)
def save(self, filename, data):
path = self.get_path(filename)
with open(path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
return path
def open(self, filename):
path = self.get_path(filename)
if not os.path.exists(path):
return None
with open(path, 'rb') as f:
return json.loads(f.read())
def exists(self, filename):
return os.path.exists(self.get_path(filename))
def delete(self, filename):
path = self.get_path(filename)
if os.path.exists(path):
os.remove(path)
return True
return False
class DefaultStorage(Storage):
def __init__(self):
super().__init__(STORAGE_DIR)
class DEFAULT_STORAGE_FILES(Enum):
COLONY_SNAPSHOT = 'colony_snapshot.json'