fix python instance init, remove and edit clip meta, add meta on record

This commit is contained in:
michalcourson
2026-02-14 11:57:12 -05:00
parent f3b883602e
commit f9fdfb629b
14 changed files with 116 additions and 30 deletions

View File

@ -7,8 +7,9 @@ class MetaDataManager:
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.init()
return cls._instance
def __init__(self):
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):
@ -16,6 +17,9 @@ class MetaDataManager:
self.collections = json.load(f)
else:
self.collections = {}
if(collections := self.collections.get("Uncategorized")) is None:
self.collections["Uncategorized"] = []
self.save_metadata()
def create_collection(self, name):
if name in self.collections:
@ -34,6 +38,31 @@ class MetaDataManager:
raise ValueError(f"Collection '{collection_name}' does not exist.")
self.collections[collection_name].append(clip_metadata)
self.save_metadata()
def remove_clip_from_collection(self, collection_name, clip_metadata):
if collection_name not in self.collections:
raise ValueError(f"Collection '{collection_name}' does not exist.")
# Remove all clips with the same file name as clip_metadata["file_name"]
in_list = any(clip.get("filename") == clip_metadata.get("filename") for clip in self.collections[collection_name])
if not in_list:
raise ValueError(f"Clip with filename '{clip_metadata.get('filename')}' not found in collection '{collection_name}'.")
self.collections[collection_name] = [
clip for clip in self.collections[collection_name]
if clip.get("filename") != clip_metadata.get("filename")
]
self.save_metadata()
def edit_clip_in_collection(self, collection_name, new_clip_metadata):
if collection_name not in self.collections:
raise ValueError(f"Collection '{collection_name}' does not exist.")
# Find the index of the clip with the same file name as old_clip_metadata["file_name"]
index = next((i for i, clip in enumerate(self.collections[collection_name]) if clip.get("filename") == new_clip_metadata.get("filename")), None)
if index is None:
raise ValueError(f"Clip with filename '{new_clip_metadata.get('filename')}' not found in collection '{collection_name}'.")
self.collections[collection_name][index] = new_clip_metadata
self.save_metadata()
def get_collections(self):
return list(self.collections.keys())
@ -43,6 +72,18 @@ class MetaDataManager:
raise ValueError(f"Collection '{collection_name}' does not exist.")
return self.collections[collection_name]
def reorder_clips_in_collection(self, collection_name, new_order):
if collection_name not in self.collections:
raise ValueError(f"Collection '{collection_name}' does not exist.")
existing_filenames = {clip.get("filename") for clip in self.collections[collection_name]}
new_filenames = {clip.get("filename") for clip in new_order}
if not new_filenames.issubset(existing_filenames):
raise ValueError("New order contains clips that do not exist in the collection.")
self.collections[collection_name] = new_order
self.save_metadata()
def save_metadata(self):
with open(self.metadata_file, "w") as f:
json.dump(self.collections, f, indent=4)