added hooks and plugins files missing

This commit is contained in:
2025-05-27 04:54:41 -05:00
parent 381f78b4e2
commit d920660366
16 changed files with 145 additions and 1 deletions

View File

@ -33,6 +33,7 @@ def create_app():
from .errors import bp as errors_bp
app.register_blueprint(errors_bp)
# Plugin auto-loader
# Plugin auto-loader
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
for plugin in os.listdir(plugin_path):

6
app/events.py Normal file
View File

@ -0,0 +1,6 @@
# Event name definitions
ON_USER_CREATED = 'on_user_created'
ON_SUBMISSION_SAVED = 'on_submission_saved'
ON_PLANT_TRANSFERRED = 'on_plant_transferred'
ON_GROWLOG_UPDATED = 'on_growlog_updated'

32
app/hooks.py Normal file
View File

@ -0,0 +1,32 @@
from typing import Callable, Dict, List
import threading
class EventDispatcher:
"""Central event dispatcher for registering and firing events."""
_listeners: Dict[str, List[Callable]] = {}
_lock = threading.Lock()
@classmethod
def register(cls, event_name: str, func: Callable):
"""Register a listener for a specific event."""
with cls._lock:
cls._listeners.setdefault(event_name, []).append(func)
@classmethod
def dispatch(cls, event_name: str, *args, **kwargs):
"""Dispatch an event to all registered listeners."""
with cls._lock:
listeners = list(cls._listeners.get(event_name, []))
for listener in listeners:
try:
listener(*args, **kwargs)
except Exception as e:
# Optionally log the exception
pass
def listen_event(event_name: str):
"""Decorator to register a function as an event listener."""
def decorator(func: Callable):
EventDispatcher.register(event_name, func)
return func
return decorator