-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
107 lines (83 loc) · 3.59 KB
/
plugin.py
File metadata and controls
107 lines (83 loc) · 3.59 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
ToniesLoader plugin implementation.
"""
import sys
from pathlib import Path
from TonieToolbox.core.plugins.base import ToolPlugin, PluginManifest, PluginContext, load_manifest_from_json
# Add plugin directory to sys.path to allow importing service
_plugin_dir = Path(__file__).parent
if str(_plugin_dir) not in sys.path:
sys.path.insert(0, str(_plugin_dir))
from service import ToniesDataService
class ToniesLoaderPlugin(ToolPlugin):
"""
Plugin that loads and provides access to Tonies JSON data.
Registers the ToniesDataService as a shared service that other
plugins and components can use to query tonie information.
Usage from other plugins:
tonies_service = context.get_service('tonies_loader')
tonie = tonies_service.get_v1_by_audio_id(1490952835)
tonies = tonies_service.get_v1_by_series("Bibi & Tina")
"""
SERVICE_NAME = "tonies_loader"
def __init__(self):
"""Initialize the plugin."""
super().__init__()
self._service: ToniesDataService | None = None
def get_manifest(self) -> PluginManifest:
"""Get the plugin manifest from manifest.json (single source of truth)."""
return load_manifest_from_json(Path(__file__).parent / "manifest.json")
def initialize(self, context: PluginContext) -> bool:
"""
Initialize the plugin.
Args:
context: Plugin context with app services
Returns:
True if initialization succeeded
"""
try:
self._context = context
# Create service with context (now uses centralized helpers)
self._service = ToniesDataService(context=context)
# Load data (download or use cache/fallback resources)
context.log_info("Loading Tonies data...")
if not self._service.load_data():
context.log_error("Failed to load Tonies data")
return False
# Register as shared service
context.register_service(self.SERVICE_NAME, self._service)
context.log_info(f"ToniesLoader service registered as '{self.SERVICE_NAME}'")
return True
except Exception as e:
context.log_error(f"Failed to initialize ToniesLoader: {e}", exc_info=True)
return False
def register_tool(self, tool_registry) -> None:
"""
Register tool with the application.
Args:
tool_registry: Tool registry (not used - service is registered via context)
"""
# Service is already registered via context.register_service()
# This method is required by ToolPlugin but we don't need additional registration
pass
def enable(self) -> bool:
"""Enable the plugin."""
enabled = super().enable()
if enabled and self._context:
self._context.log_info("ToniesLoader plugin enabled - service available for queries")
return enabled
def disable(self) -> bool:
"""Disable the plugin."""
disabled = super().disable()
if disabled and self._context:
self._context.log_info("ToniesLoader plugin disabled")
return disabled
def cleanup(self) -> None:
"""Clean up plugin resources."""
if self._service:
self._service.cleanup()
self._service = None
if self._context:
self._context.log_info("ToniesLoader plugin cleaned up")
super().cleanup()