butterrobot/butterrobot/plugins.py

39 lines
1006 B
Python
Raw Normal View History

2020-04-22 21:58:06 +00:00
import traceback
import pkg_resources
from abc import abstractclassmethod
import structlog
2020-08-11 10:57:56 +00:00
from butterrobot.objects import Message
2020-04-22 21:58:06 +00:00
logger = structlog.get_logger(__name__)
class Plugin:
@abstractclassmethod
2020-10-28 10:19:30 +00:00
def on_message(cls, message: Message):
2020-04-22 21:58:06 +00:00
pass
def get_available_plugins():
"""Retrieves every available plugin"""
plugins = {}
logger.debug("Loading plugins")
for ep in pkg_resources.iter_entry_points("butterrobot.plugins"):
try:
plugin_cls = ep.load()
plugins[plugin_cls.id] = plugin_cls
except Exception as error:
logger.error(
"Error loading plugin",
exception=str(error),
traceback=traceback.format_exc(),
plugin=ep.name,
project_name=ep.dist.project_name,
entry_point=ep,
module=ep.module_name,
)
logger.info(f"Plugins loaded", plugins=list(plugins.keys()))
return plugins