1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 08:12:27 +00:00

Enhance plugin metaclass

* Change names of plugins to avoid repetitions (we may have to revert this)
* Make subprocess log private
This commit is contained in:
J. Fernando Sánchez 2018-01-02 11:56:55 +01:00
parent bfc588a915
commit abd401f863
3 changed files with 18 additions and 6 deletions

View File

@ -1,7 +1,7 @@
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):
if __name__ == '__main__': if __name__ == '__main__':
d = DummyPlugin() d = DummyNoInfo()
d.test() d.test()

View File

@ -1,5 +1,5 @@
from senpy.plugins import SentimentPlugin from senpy.plugins import SentimentPlugin
class DummyPlugin(SentimentPlugin): class NoOp(SentimentPlugin):
import noop import noop

View File

@ -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):
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):
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):
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
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)