add copy to ZonedPluginRegistry
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
|
from abc import abstractmethod
|
||||||
|
|
||||||
|
|
||||||
class PluginBase():
|
class PluginBase():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def copy(self):
|
||||||
|
"""
|
||||||
|
Subclasses that use state must implement this method and return a new instance of themselves.
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return self
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from src.zonedpluginregistry import ZonedPluginRegistry
|
|||||||
|
|
||||||
|
|
||||||
class PluginRegistry():
|
class PluginRegistry():
|
||||||
plugins = ZonedPluginRegistry({})
|
plugins = ZonedPluginRegistry()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_plugin(plugin_name: str):
|
def load_plugin(plugin_name: str):
|
||||||
@@ -22,6 +22,10 @@ class PluginRegistry():
|
|||||||
return
|
return
|
||||||
raise RuntimeError("plugin %s not found" % plugin_name)
|
raise RuntimeError("plugin %s not found" % plugin_name)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create_zoned_plugin_registry():
|
||||||
|
return PluginRegistry.plugins.copy()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def execute_single(function_name: str, *args) -> Optional[any]:
|
def execute_single(function_name: str, *args) -> Optional[any]:
|
||||||
return PluginRegistry.plugins.execute_single(function_name, *args)
|
return PluginRegistry.plugins.execute_single(function_name, *args)
|
||||||
@@ -29,3 +33,7 @@ class PluginRegistry():
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def execute(function_name: str, *args) -> [any]:
|
def execute(function_name: str, *args) -> [any]:
|
||||||
return PluginRegistry.plugins.execute(function_name, *args)
|
return PluginRegistry.plugins.execute(function_name, *args)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def execute_flat_map(function_name: str, *args) -> [any]:
|
||||||
|
return PluginRegistry.plugins.execute_flat_map(function_name, *args)
|
||||||
|
|||||||
@@ -1,23 +1,47 @@
|
|||||||
|
from functools import reduce
|
||||||
from inspect import signature
|
from inspect import signature
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional, List
|
||||||
|
|
||||||
from src.pluginbase import PluginBase
|
from src.pluginbase import PluginBase
|
||||||
|
|
||||||
|
|
||||||
|
def flat_map(array: List[List]) -> List:
|
||||||
|
return reduce(list.__add__, array)
|
||||||
|
|
||||||
|
|
||||||
class ZonedPluginRegistry:
|
class ZonedPluginRegistry:
|
||||||
|
|
||||||
def __init__(self, plugins: Dict[str, PluginBase]):
|
def __init__(self):
|
||||||
self.plugins: Dict[str, PluginBase] = plugins
|
self.plugins: Dict[str, PluginBase] = {}
|
||||||
|
|
||||||
def register_plugin(self, name: str, plugin: PluginBase):
|
def register_plugin(self, name: str, plugin: PluginBase):
|
||||||
self.plugins[name] = plugin
|
self.plugins[name] = plugin
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
result = ZonedPluginRegistry()
|
||||||
|
for name in self.plugins.keys():
|
||||||
|
plugin = self.plugins[name].copy()
|
||||||
|
result.register_plugin(name, plugin)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def execute_single(self, function_name: str, *args) -> Optional[any]:
|
def execute_single(self, function_name: str, *args) -> Optional[any]:
|
||||||
return self._execute(function_name, True, *args)
|
return self._execute(function_name, True, *args)
|
||||||
|
|
||||||
def execute(self, function_name: str, *args) -> [any]:
|
def execute(self, function_name: str, *args) -> [any]:
|
||||||
return self._execute(function_name, False, *args)
|
return self._execute(function_name, False, *args)
|
||||||
|
|
||||||
|
def execute_flat_map(self, function_name: str, *args) -> [any]:
|
||||||
|
return flat_map(self.execute(function_name, *args))
|
||||||
|
|
||||||
|
def execute_remove_falsy(self, function_name: str, *args) -> [any]:
|
||||||
|
result = self.execute(function_name, *args)
|
||||||
|
return [r for r in result if r]
|
||||||
|
|
||||||
|
def execute_flat_map_remove_falsy(self, function_name: str, *args) -> [any]:
|
||||||
|
result = flat_map(self.execute(function_name, *args))
|
||||||
|
return [r for r in result if r]
|
||||||
|
|
||||||
def _execute(self, function_name: str, return_first: bool, *args) -> []:
|
def _execute(self, function_name: str, return_first: bool, *args) -> []:
|
||||||
result = []
|
result = []
|
||||||
for plugin in self.plugins.values():
|
for plugin in self.plugins.values():
|
||||||
|
|||||||
Reference in New Issue
Block a user