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

Small changes to the property factories + new property for strings.

 - improve docstrings
 - use the same 'deleter' for all properties
 - new property factory: string_field
 - int_field properties return 0 if the item is not found
parent 308ee163
......@@ -85,48 +85,76 @@ 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 _fdel(name):
"""
Returns a function for deleting the dictionary's item with the
given key. For use in properties below.
"""
def fdel(instance):
del instance[name]
return fdel
def bool_field(name):
""" Property exposing a string ('true' or 'false') as a bool. """
"""
Property exposing an item ('true' or 'false') from the plugin
result's dictionary as a bool.
If the dictionary does not have the requested item yet, the getter
will return False.
"""
def fget(instance):
return True if instance.get(name, 'false') == '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]
instance[name] = u'true' if value else u'false'
return property(fget, fset, fdel,
return property(fget, fset, _fdel(name),
"Read-write property exposing a string ('true' or 'false')"
" as a bool.")
def int_field(name):
""" Property exposing a string as an int """
"""
Property exposing a numeric item from the plugin result's dictionary as
an int.
If the dictionary does not have the requested item yet, the getter
will return 0.
"""
def fget(instance):
return int(instance[name])
# FIXME: better error handling
return int(instance.get(name, 0))
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,
return property(fget, fset, _fdel(name),
'Read-write property exposing a string as an int')
def string_field(name):
"""
Property for accessing a string item from the plugin result's dictionary.
"""
def fget(instance):
return instance.get(name, "")
def fset(instance, value):
instance[name] = value
return property(fget, fset, _fdel(name))
#
# Bases classes for plugins
......
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