Browse Source

added IBasePlugin.get_default_config()

master
Niklas Rosenstein 1 year ago
parent
commit
e1804eef32
Signed by: NiklasRosenstein GPG Key ID: 06D269B33D25F6C6
2 changed files with 17 additions and 8 deletions
  1. + 11
    - 3
      src/shore/core/plugins.py
  2. + 6
    - 5
      src/shore/model.py

+ 11
- 3
src/shore/core/plugins.py

@ -139,6 +139,11 @@ class IBasePlugin(Interface):
Config = staticattr(None)
config = attr(default=None)
@default
@classmethod
def get_default_config(cls):
return cls.Config()
@default
@classmethod
def new_instance(cls, config: 'Config') -> 'IBasePlugin':
@ -149,9 +154,12 @@ class IBasePlugin(Interface):
if cls.Config is None and config is not None:
raise ValueError('{} does not expect a config, got {}'.format(
cls.__name__, type(config).__name__))
elif cls.Config and not isinstance(config, cls.Config):
raise ValueError('{} expects a config of type {}, got {}'.format(
cls.__name__, cls.Config.__name__, type(config).__name__))
elif cls.Config is not None:
if config is None:
config = cls.get_default_config()
elif not isinstance(config, cls.Config):
raise ValueError('{} expects a config of type {}, got {}'.format(
cls.__name__, cls.Config.__name__, type(config).__name__))
instance = cls()
instance.config = config

+ 6
- 5
src/shore/model.py

@ -357,18 +357,19 @@ class PluginConfig(Struct):
def _deserialize(cls, mapper, node):
if isinstance(node.value, str):
plugin_name = node.value
config = {}
config = None
elif isinstance(node.value, dict):
if len(node.value) != 1:
raise ValueError('expected only one key')
plugin_name, config = next(iter(node.value.items()))
if 'type' not in node.value:
node.value_error('missing "type" key')
config = node.value.copy()
plugin_name = config.pop('type')
else:
raise TypeError('expected str or dict')
try:
plugin_cls = load_plugin(plugin_name)
except PluginNotFound as exc:
raise ValueError('plugin "{}" not found'.format(exc))
if plugin_cls.Config is not None:
if plugin_cls.Config is not None and config is not None:
config = mapper.deserialize_node(node.make_child(plugin_name, plugin_cls.Config, config))
elif plugin_cls.Config is None and config:
raise TypeError('plugin {} expects no configuration'.format(plugin_name))

Loading…
Cancel
Save