"""
File: ops_notes.py
Description:
Note Management and Formatting for openPypeline Studio.
Handles reading, parsing, and formatting JSON event notes.
Refactored from XML legacy formats to modern JSON.
Original Framework: openPipeline by Kickstand
License: Common Public License 1.0 (CPL-1.0)
"""
import os
import json
[docs]
def read_all_notes(input_path):
"""Read all notes of a specified item, returned in reverse order."""
if not os.path.isfile(input_path):
return []
try:
with open(input_path, "r", encoding="utf-8") as f:
content = json.load(f)
if isinstance(content, list):
# Return in reverse order (newest first)
return content[::-1]
return []
except Exception:
return []
[docs]
def count_notes(input_path):
"""Counts the number of note entries for a specific item."""
return len(read_all_notes(input_path))
[docs]
def read_individual_note(input_path, index):
"""Reads an individual note."""
notes = read_all_notes(input_path)
if 0 <= index < len(notes):
return notes[index]
return {}
[docs]
def read_notes_by_event(input_path, event_type):
"""Read all notes that describe a particular event."""
notes = read_all_notes(input_path)
return [n for n in notes if str(n.get("event", "")).lower() == str(event_type).lower()]
[docs]
def read_notes_by_type(input_path, note_type):
"""Read all notes that match a particular type."""
notes = read_all_notes(input_path)
return [n for n in notes if str(n.get("notetype", "")).lower() == str(note_type).lower()]
[docs]
def read_notes_by_version(input_path, version):
"""Read all notes that match a particular version."""
notes = read_all_notes(input_path)
return cull_notes_by_version(notes, version)
[docs]
def cull_notes_by_version(all_notes, version):
"""Filter notes that match a particular version."""
return [n for n in all_notes if str(n.get("version", "")) == str(version)]