Commit ac06ba53 authored by Clément Schreiner's avatar Clément Schreiner
Browse files

Interactions between the database and a plugin now done in the plugin itself.

BasePlugin:

 - new methods: ``load`` and ``save`` for loading the data and
   committing the sqlalchemy session

 - ``_load_result``, allowing easy overridding in specific plugins' classes (e.g. QAPlugin)

 - ``db_objects`` attribute replaced by ``results``

Plugins class now simpler.
parent 6062ebf7
......@@ -93,7 +93,6 @@ class Plugins(object):
self.package_version = package_version
self.found_results = False
self.modules = self.import_plugins(self.type)
......@@ -218,33 +217,16 @@ class Plugins(object):
% (name,
traceback.format_exc()))
else:
for obj in plugin.db_objects:
meta.session.add(obj)
meta.session.commit()
log.debug("Added plugin result objects to the database.")
return self.result_objects
plugin.save()
def load_results(self):
"""
Load all plugin results from the database into each plugin's
'result' dictionary attribute.
e.g.: plugins['native'].results -> {'native_test': [<NativeTest 1>,]}
Calls ``load`` method on all loaded plugin instances.
"""
query = meta.session.query(PluginResult)
query = query.filter_by(package_version_id = self.package_version.id)
results = query.all()
if len(results) > 0:
self.found_results = True
for result in query.all():
plugin = self.plugins[result.plugin]
entity_list = plugin.results.setdefault(result.entity, list())
for name, plugin in self.iteritems():
plugin.load()
entity_list.append(result)
#
# magic methods from accessing the ``plugins`` attribute
......
......@@ -48,6 +48,7 @@ from pylons import config
import os.path
import debexpo.lib
from debexpo.model.meta import session
from debexpo.model.plugin_results import PluginResult
PLUGINS_TEMPLATE_DIRS = [os.path.join(path, "plugins")
for path in config["pylons.paths"]["templates"]]
......@@ -80,12 +81,6 @@ class BasePlugin(object):
Constructor for a plugin.
"""
# new results to be added to the session
self._db_objects = []
# for storing results retrieved from the database
# FIXME: use only one attribute for result objects
self.results = {}
self.name = name
# PackageVersion object for the package we're looking at
......@@ -94,9 +89,19 @@ class BasePlugin(object):
self.models = []
if models is not None:
self._load_models(models)
# for storing results retrieved from the database
self.results = {}
# other argumnets
for key in kw:
setattr(self, key, kw[key])
# sqlalchemy session
self.session = session
def run(self):
"""
Import data from the package.
......@@ -106,12 +111,26 @@ class BasePlugin(object):
if getattr(method, 'importercmd', False):
method()
def _add_db_objects(self, *db_objects):
def load(self):
"""
Adds objects to the list of objects that will be added to the
sqlalchemy session.
Load all data previously imported into the database by this
plugin.
"""
self._db_objects.extend(db_objects)
for model in self.models:
q = self.session.query(model)
q = q.filter_by(package_version = self.package_version)
for result in q.all():
self._load_result(result)
return self.results
def _load_result(self, result):
self.results.setdefault(result.entity, list()).append(result)
def save(self):
""" Save DB changes """
self.session.commit()
log.debug('Added results to the database')
def new_result(self, result_cls, **data):
"""
......@@ -119,17 +138,11 @@ class BasePlugin(object):
instance of the result.
"""
result = result_cls(package_version = self.package_version)
self._add_db_objects(result)
for k, v in data.iteritems():
result[k] = v
return result
@property
def db_objects(self):
"""
List of objects to be added to the database.
"""
return self._db_objects
self.session.add(result)
return result
@property
def nb_results(self):
......@@ -216,18 +229,8 @@ class QAPlugin(BasePlugin):
# the PluginResult-derived class representing the result of the
# test
@property
def test_result(self):
"""
Returns the result of the QA test.
"""
if self.results.has_key(self.test_entity):
return self.results[self.test_entity][0]
else:
return None
test_model = None
test_result = None # instance of that class, set by ``load``
@property
def test_severity(self):
......@@ -264,3 +267,14 @@ class QAPlugin(BasePlugin):
getattr(model, 'test_result', False)):
self.test_model = model
super(QAPlugin, self)._load_model(model)
def _load_result(self, result):
"""
Overridding BasePlugin's ``_load_result`` to set the
``test_result`` attribute if found.
"""
log.debug('Loading %s' % result)
if isinstance(result, self.test_model):
self.test_result = result
super(QAPlugin, self)._load_result(result)
Supports Markdown
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