mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
183: Added public.DefaultFrom class; added corresponding unit tests
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user