183: Added public.DefaultFrom class; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose
2008-08-22 20:07:17 +00:00
parent b0ec8fe551
commit cad924168e
2 changed files with 56 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ and UI all use.
import re
import inspect
import plugable
from plugable import lock
import errors
@@ -39,6 +40,26 @@ def is_rule(obj):
return callable(obj) and getattr(obj, RULE_FLAG, False) is True
class DefaultFrom(plugable.ReadOnly):
def __init__(self, callback, *keys):
assert callable(callback), 'not a callable: %r' % callback
self.callback = callback
self.keys = keys
lock(self)
def __call__(self, **kw):
vals = tuple(kw.get(k, None) for k in self.keys)
if None in vals:
return None
try:
ret = self.callback(*vals)
except Exception:
return None
if isinstance(ret, basestring):
return ret
return None
class option(plugable.Plugin):
"""
The option class represents a kw argument from a command.