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

Cosmetic/trivial changes.

 - Comments, debug messages, cleanup imports, better variable names.

 - nicer code in the ``nb_results`` property (BasePlugin)
parent ac06ba53
......@@ -34,28 +34,31 @@ Holds the plugin loader.
"""
__author__ = 'Jonny Lamb'
__copyright__ = 'Copyright © 2008 Jonny Lamb, Copyright © 2010 Jan Dittberner, Copyright © Clément Schreiner'
__copyright__ = ', '.join([
'Copyright © 2008 Jonny Lamb',
'Copyright © 2010 Jan Dittberner',
'Copyright © 2012 Clément Schreiner',
])
__license__ = 'MIT'
import logging
import os
import shutil
import sys
import tempfile
import traceback
from collections import namedtuple
from debian import deb822
import pylons
from debexpo.model import meta
from debexpo.model.plugin_results import PluginResult
log = logging.getLogger(__name__)
PluginModule = namedtuple('PluginModule', ['name', 'stage', 'plugin', 'models'])
class Plugins(object):
"""
Plugin loader.
......@@ -93,7 +96,6 @@ class Plugins(object):
self.package_version = package_version
self.modules = self.import_plugins(self.type)
# the plugin instances
......@@ -197,25 +199,23 @@ class Plugins(object):
log.debug('Something went wrong while loading the plugin {}:'
'{}'.format(module.name, traceback.format_exc()))
def run_plugins(self):
"""
Run all imported plugins.
"""
if len(self.plugins) == 0:
log.debug("No plugin loaded. Returning result: %s",
self.result_objects)
return self.result_objects
if len(self) == 0:
log.debug('No plugin loaded.')
# Run each plugin.
for name, plugin in self.plugins.iteritems():
for name, plugin in self.iteritems():
log.debug('Running plugin: %s' % name)
try:
plugin.run()
except Exception:
log.debug("Something wrong happened while running the plugin '%s': %s"
% (name,
traceback.format_exc()))
% (name, traceback.format_exc()))
else:
plugin.save()
......@@ -239,7 +239,10 @@ class Plugins(object):
Returns a plugin instance for the given plugin name.
plugins['native'] -> NativePlugin instance
"""
return self.plugins.get(key, None)
return self.plugins[key]
def get(self, key, default):
return self.plugins.get(key, default)
def iteritems(self):
"""Iter the items in the ``plugins`` dictionary attribute."""
......@@ -250,6 +253,6 @@ class Plugins(object):
return len(self.plugins)
def __iter__(self):
""" Iter over plugin instances """
for plugin in self.plugins.values():
yield plugin
""" Iter over plugin instances' names """
for name in self.plugins:
yield name
......@@ -44,7 +44,6 @@ from debexpo.model.package_versions import PackageVersion
from debexpo.lib.utils import uncamel
class PluginResult(meta.Base):
__tablename__ = 'plugin_results'
......@@ -103,7 +102,7 @@ class PluginResult(meta.Base):
del self._data[key]
#
# Direct access to some attributes (for templates)
# Direct access to some attributes
#
@property
......
......@@ -36,24 +36,27 @@ __author__ = 'Jonny Lamb'
__copyright__ = 'Copyright © 2008 Jonny Lamb'
__license__ = 'MIT'
from debexpo.lib import constants
import os.path
import logging
from inspect import getmembers, ismethod
# for template rendering
from pylons import config
# template rendering
from mako.lookup import TemplateLookup
from mako.exceptions import TopLevelLookupException
from pylons import config
import os.path
import debexpo.lib
from debexpo.lib import constants, helpers
from debexpo.model.meta import session
from debexpo.model.plugin_results import PluginResult
log = logging.getLogger(__name__)
# FIXME: there is probably a better way to define this
PLUGINS_TEMPLATE_DIRS = [os.path.join(path, "plugins")
for path in config["pylons.paths"]["templates"]]
log = logging.getLogger(__name__)
#
# Decorators simplifying the writing of plugins
......@@ -68,9 +71,17 @@ def importercmd(func):
return func
def test_result(cls):
"""
Makes a plugin result model the result of a QA test.
"""
cls.test_result = True
return cls
#
# Bases classes for plugins
#
class BasePlugin(object):
"""
Base class for importer plugins.
......@@ -149,8 +160,7 @@ class BasePlugin(object):
"""
Number of results that have been retrieved from the DB.
"""
return reduce(lambda s,l: s + len(l), self.results.values(), 0)
return sum(len(l) for l in self.results.itervalues())
def _find_template(self, name, render_format):
# Files to try out for plugin data rendering
......@@ -176,7 +186,7 @@ class BasePlugin(object):
break
else:
# No template file found, something weird happened
return "(!! no template found for %s plugin data)" % self.__name__
return "(!! no template found for %s plugin data)" % name
return template
......@@ -206,16 +216,16 @@ class BasePlugin(object):
def render(self, render_format, **render_args):
"""Render the plugin data to the given format"""
template = self._find_template(self.plugin, render_format)
template = self._find_template(self.name, render_format)
return template.render_unicode(results = self.results,
h = debexpo.lib.helpers,
h = helpers,
**render_args)
class QAPlugin(BasePlugin):
"""
Class for implementing QA plugins.
QA plugins have a the concept of a test that can be passed or
QA plugins have the concept of a test that can be passed or
failed.
Also, their templates look alike and could be abstracted
......@@ -237,9 +247,13 @@ class QAPlugin(BasePlugin):
"""
Returns the severity of the test result.
"""
# FIXME: ugly
if self.test_result is None:
log.debug('The plugin data has not been loaded, '
'or this plugin does not define a test')
return None
# FIXME: ugly
if self.test_result.severity == 0:
return 1
return self.test_result.severity
......@@ -250,6 +264,7 @@ class QAPlugin(BasePlugin):
the test.
"""
return self.new_result(self.test_model, **data)
def render(self, render_format, **render_args):
"""
Render the QA test's template.
......
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