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 from .errors import bp as errors_bp
app.register_blueprint(errors_bp) app.register_blueprint(errors_bp)
# Plugin auto-loader
# Plugin auto-loader # Plugin auto-loader
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins')) plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
for plugin in os.listdir(plugin_path): 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

BIN
files.zip Normal file

Binary file not shown.

BIN
new.zip

Binary file not shown.

View File

@ -0,0 +1,6 @@
import click
from flask import Flask
def register_cli(app: Flask):
# No CLI commands yet for admin plugin
pass

View File

@ -0,0 +1,6 @@
{
"name": "admin",
"version": "1.0.0",
"description": "Administration dashboard and plugin manager",
"entry_point": null
}

6
plugins/auth/__init__.py Normal file
View File

@ -0,0 +1,6 @@
import click
from flask import Flask
def register_cli(app: Flask):
# No CLI commands yet for auth plugin
pass

6
plugins/auth/plugin.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "auth",
"version": "1.0.0",
"description": "User authentication and authorization plugin",
"entry_point": null
}

6
plugins/cli/plugin.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "cli",
"version": "1.0.0",
"description": "Command-line interface plugin",
"entry_point": null
}

View File

@ -1 +1,6 @@
# plant plugin init import click
from flask import Flask
def register_cli(app: Flask):
# No CLI commands yet for plant plugin
pass

View File

@ -0,0 +1,6 @@
{
"name": "plant",
"version": "1.0.0",
"description": "Plant profile management plugin",
"entry_point": null
}

10
tests/test_app_init.py Normal file
View File

@ -0,0 +1,10 @@
import pytest
from app import create_app
def test_app_loads_plugins():
app = create_app({'TESTING': True})
# Assuming app.plugins is a dict of loaded plugin modules
assert hasattr(app, 'plugins'), "App object missing 'plugins' attribute"
for plugin in ["auth", "admin", "plant", "cli"]:
assert plugin in app.plugins, f"Plugin '{plugin}' not loaded into app.plugins"

View File

@ -0,0 +1,22 @@
import pytest
from app.hooks import EventDispatcher, listen_event
def test_register_and_dispatch():
results = []
def listener1(a, b=None):
results.append(('l1', a, b))
def listener2(a, b=None):
results.append(('l2', a, b))
EventDispatcher.register('test_event', listener1)
EventDispatcher.register('test_event', listener2)
EventDispatcher.dispatch('test_event', 1, b=2)
assert ('l1', 1, 2) in results
assert ('l2', 1, 2) in results
def test_listen_event_decorator():
results = []
@listen_event('decorated_event')
def handler(x):
results.append(x)
EventDispatcher.dispatch('decorated_event', 'hello')
assert results == ['hello']

7
tests/test_events.py Normal file
View File

@ -0,0 +1,7 @@
import app.events as events
def test_event_names_exist():
assert hasattr(events, 'ON_USER_CREATED')
assert hasattr(events, 'ON_SUBMISSION_SAVED')
assert hasattr(events, 'ON_PLANT_TRANSFERRED')
assert hasattr(events, 'ON_GROWLOG_UPDATED')

View File

@ -0,0 +1,25 @@
import os
import json
import pytest
import importlib
# Directory containing plugins
PLUGINS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
@pytest.mark.parametrize("plugin", ["auth", "admin", "plant", "cli"])
def test_plugin_metadata_and_init(plugin, tmp_path, monkeypatch):
# Construct plugin path
plugin_path = os.path.join(PLUGINS_DIR, plugin)
# Test plugin.json exists and contains required keys
meta_path = os.path.join(plugin_path, 'plugin.json')
assert os.path.isfile(meta_path), f"plugin.json missing for {plugin}"
meta = json.loads(open(meta_path).read())
for key in ("name", "version", "description"):
assert key in meta, f"{{key}} missing in {plugin}/plugin.json"
# Test __init__.py can be imported and has register_cli
module_name = f"plugins.{plugin}"
spec = importlib.util.spec_from_file_location(module_name, os.path.join(plugin_path, '__init__.py'))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
assert hasattr(module, "register_cli"), f"register_cli missing in {plugin}/__init__.py"