Commit abd401f8 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez

Enhance plugin metaclass

* Change names of plugins to avoid repetitions (we may have to revert this)
* Make subprocess log private
parent bfc588a9
from senpy.plugins import SentimentPlugin from senpy.plugins import SentimentPlugin
class DummyPlugin(SentimentPlugin): class DummyNoInfo(SentimentPlugin):
description = 'This is a dummy self-contained plugin' description = 'This is a dummy self-contained plugin'
author = '@balkian' author = '@balkian'
...@@ -23,5 +23,5 @@ class DummyPlugin(SentimentPlugin): ...@@ -23,5 +23,5 @@ class DummyPlugin(SentimentPlugin):
if __name__ == '__main__': if __name__ == '__main__':
d = DummyPlugin() d = DummyNoInfo()
d.test() d.test()
from senpy.plugins import SentimentPlugin from senpy.plugins import SentimentPlugin
class DummyPlugin(SentimentPlugin): class NoOp(SentimentPlugin):
import noop import noop
...@@ -23,6 +23,7 @@ logger = logging.getLogger(__name__) ...@@ -23,6 +23,7 @@ logger = logging.getLogger(__name__)
class PluginMeta(models.BaseMeta): class PluginMeta(models.BaseMeta):
_classes = {}
def __new__(mcs, name, bases, attrs, **kwargs): def __new__(mcs, name, bases, attrs, **kwargs):
plugin_type = [] plugin_type = []
...@@ -30,7 +31,16 @@ class PluginMeta(models.BaseMeta): ...@@ -30,7 +31,16 @@ class PluginMeta(models.BaseMeta):
plugin_type += bases[0].plugin_type plugin_type += bases[0].plugin_type
plugin_type.append(name) plugin_type.append(name)
attrs['plugin_type'] = plugin_type attrs['plugin_type'] = plugin_type
return super(PluginMeta, mcs).__new__(mcs, name, bases, attrs) cls = super(PluginMeta, mcs).__new__(mcs, name, bases, attrs)
if name in mcs._classes:
raise Exception(('The type of plugin {} already exists. '
'Please, choose a different name').format(name))
mcs._classes[name] = cls
return cls
@classmethod
def for_type(cls, ptype):
return cls._classes[ptype]
class Plugin(with_metaclass(PluginMeta, models.Plugin)): class Plugin(with_metaclass(PluginMeta, models.Plugin)):
...@@ -234,7 +244,7 @@ def load_module(name, root=None): ...@@ -234,7 +244,7 @@ def load_module(name, root=None):
return tmp return tmp
def log_subprocess_output(process): def _log_subprocess_output(process):
for line in iter(process.stdout.readline, b''): for line in iter(process.stdout.readline, b''):
logger.info('%r', line) logger.info('%r', line)
for line in iter(process.stderr.readline, b''): for line in iter(process.stderr.readline, b''):
...@@ -253,7 +263,7 @@ def install_deps(*plugins): ...@@ -253,7 +263,7 @@ def install_deps(*plugins):
process = subprocess.Popen(pip_args, process = subprocess.Popen(pip_args,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
log_subprocess_output(process) _log_subprocess_output(process)
exitcode = process.wait() exitcode = process.wait()
installed = True installed = True
if exitcode != 0: if exitcode != 0:
...@@ -289,6 +299,8 @@ def load_plugin_from_info(info, root=None, validator=validate_info, install=True ...@@ -289,6 +299,8 @@ def load_plugin_from_info(info, root=None, validator=validate_info, install=True
cls = None cls = None
if '@type' not in info: if '@type' not in info:
cls = get_plugin_class(tmp) cls = get_plugin_class(tmp)
else:
cls = PluginMeta.from_type(info['@type'])
if not cls: if not cls:
raise Exception("No valid plugin for: {}".format(module)) raise Exception("No valid plugin for: {}".format(module))
return cls(info=info, *args, **kwargs) return cls(info=info, *args, **kwargs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment