fmartingr
/
jeeves
Archived
1
0
Fork 0
This repository has been archived on 2021-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
jeeves/jeeves/core/registry.py

78 lines
2.7 KiB
Python

import logging
from typing import Any, Dict, Text, Type, Optional
from jeeves.core.actions import PROVIDED_ACTIONS
from jeeves.core.actions.base import Action
class Singleton(type):
def __init__(cls, name, bases, attrs, **kwargs):
super().__init__(name, bases, attrs, **kwargs)
cls._instance = None
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__call__(*args, **kwargs)
return cls._instance
class ActionRegistry(metaclass=Singleton):
actions: Dict[str, str] = {}
def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
@classmethod
def autodiscover(cls):
"""Loads all provided actions."""
# TODO: Third party plugins
registry = cls()
for action_namespace in PROVIDED_ACTIONS:
registry.register_action(cls.get_action_cls(action_namespace))
@classmethod
def register_action(cls, action_cls):
registry = cls()
# namespace = action_cls.id
namespace = f"{action_cls.__module__}:{action_cls.__name__}"
if namespace in registry.actions:
raise cls.ActionNamespaceConflict(
f"Namespace {namespace} is already registered"
)
registry.actions[namespace] = action_cls
@classmethod
def get_action_cls(cls, namespace) -> Type[Action]:
"""Returns the class for the provided action namespace"""
# Right now actions are being imported and returned dinamically because it's easier,
# but we will need a way of autodiscover all (or register them manually) and
# referencing them on a list so the user knows which actions are available.
modulename, clsname = namespace.split(":")
try:
module = __import__(f"{modulename}", fromlist=(clsname,), level=0)
action_cls = getattr(module, clsname)
return action_cls
except ModuleNotFoundError as error:
raise cls.ActionDoesNotExist(f"Error importing action {namespace}: {error}")
@classmethod
def get_action(
cls, namespace: Text, parameters: Optional[Dict[Any, Any]] = None
) -> Action:
"""Returns the instanced action for the provided namespace"""
action_cls: Type[Action] = cls.get_action_cls(namespace)
return action_cls(parameters=parameters or {})
class ActionNamespaceConflict(Exception):
"""Raised when an action is defined with an already registered namespace"""
pass
class ActionDoesNotExist(Exception):
"""Raised when there's a problem retrieving an action. More info will be available on the message."""
pass