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

'property factories' for easier type coercion in plugin results.

parent 4cf0e90e
......@@ -29,7 +29,7 @@
# OTHER DEALINGS IN THE SOFTWARE.
"""
Holds some helpful classes for plugins to use or extend.
Holds some helpful classes and functions for plugins to extend or use.
"""
__author__ = 'Jonny Lamb'
......@@ -40,7 +40,9 @@ __all__ = ['importercmd',
'test_result',
'BasePlugin',
'QAPlugin',
'PluginResult']
'PluginResult',
'bool_field',
'int_field']
import os.path
import logging
......@@ -82,7 +84,45 @@ def test_result(cls):
cls.test_result = True
return cls
#
# Property factories for PluginResult subclasses
#
# FIXME: move this somewhere else
# FIXME: some of that could be abstracted
def bool_field(name):
""" Property exposing a string ('true' or 'false') as a bool. """
def fget(instance):
return True if instance[name] == 'true' else False
def fset(instance, value):
if not isinstance(value, bool):
raise ValueError('Expected bool')
instance[name] = 'true' if value else 'false'
def fdel(instance):
del instance[name]
return property(fget, fset, fdel,
"Read-write property exposing a string ('true' or 'false')"
" as a bool.")
def int_field(name):
""" Property exposing a string as an int """
def fget(instance):
return int(instance[name])
def fset(instance, value):
if not isinstance(value, int):
raise ValueError('Expected int')
instance[name] = unicode(value)
def fdel(instance):
del instance[name]
return property(fget, fset, fdel,
'Read-write property exposing a string as an int')
#
# Bases classes for plugins
#
......
......@@ -55,9 +55,7 @@ class NativeTest(PluginResult):
Result of the 'native' plugin for a package.
"""
@property
def is_native(self):
return self['native'] == 'true'
is_native = bool_field('native')
def __str__(self):
return 'Package is %s native' % ('' if self.is_native else 'not')
......@@ -83,13 +81,14 @@ class NativePlugin(QAPlugin):
# Most uploads will not be native, and especially on mentors, a native
# package is almost probably in error.
log.warning('Package is native')
result['native'] = 'true'
result['severity'] = constants.PLUGIN_SEVERITY_WARNING
else:
log.debug('Package is not native')
result['native'] = 'false'
result['severity'] = constants.PLUGIN_SEVERITY_INFO
result.native = is_native
plugin = NativePlugin
models = [
NativeTest,
......
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