Files
ClipTrimApp/audio-service/src/metadata_manager.py
2026-02-14 11:24:09 -05:00

49 lines
1.7 KiB
Python

import os
import json
class MetaDataManager:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
# read metadata file from executing directory
self.metadata_file = os.path.join(os.getcwd(), "metadata.json")
if os.path.exists(self.metadata_file):
with open(self.metadata_file, "r") as f:
self.collections = json.load(f)
else:
self.collections = {}
def create_collection(self, name):
if name in self.collections:
raise ValueError(f"Collection '{name}' already exists.")
self.collections[name] = []
self.save_metadata()
def delete_collection(self, name):
if name not in self.collections:
raise ValueError(f"Collection '{name}' does not exist.")
del self.collections[name]
self.save_metadata()
def add_clip_to_collection(self, collection_name, clip_metadata):
if collection_name not in self.collections:
raise ValueError(f"Collection '{collection_name}' does not exist.")
self.collections[collection_name].append(clip_metadata)
self.save_metadata()
def get_collections(self):
return list(self.collections.keys())
def get_clips_in_collection(self, collection_name):
if collection_name not in self.collections:
raise ValueError(f"Collection '{collection_name}' does not exist.")
return self.collections[collection_name]
def save_metadata(self):
with open(self.metadata_file, "w") as f:
json.dump(self.collections, f, indent=4)