Bug 1384396 - Add a @depends_all utility function; r=nalexander

Often you only want to evaluate a function if all its dependencies
are true. Expressing this in a "when" can be difficult. So let's
add a convenience decorator for it.

The existing code for @depends_if() was refactored to take an
evaluation function as its first argument. This prevents some
duplicate code and turns @depends_if() and @depends_all() into
one-liners.

MozReview-Commit-ID: Jbugvf0lioM

--HG--
extra : rebase_source : 177741b80ac4fbfb547d6b36a6f5777fe514d91a
extra : source : 0c2bc12f4ebe44428385745266d2fd158e0c3382
This commit is contained in:
Gregory Szorc 2017-07-25 22:00:38 -07:00
Родитель 8cfbdba9c5
Коммит f6426ce5ad
1 изменённых файлов: 20 добавлений и 4 удалений

Просмотреть файл

@ -404,10 +404,11 @@ always = dependable(True)
never = dependable(False) never = dependable(False)
# Like @depends, but the decorated function is only called if one of the # Create a decorator that will only execute the body of a function
# arguments it would be called with has a positive value (bool(value) is True) # if the passed function returns True when passed all positional
# arguments.
@template @template
def depends_if(*args, **kwargs): def depends_tmpl(eval_args_fn, *args, **kwargs):
if kwargs: if kwargs:
assert len(kwargs) == 1 assert len(kwargs) == 1
when = kwargs['when'] when = kwargs['when']
@ -416,11 +417,26 @@ def depends_if(*args, **kwargs):
def decorator(func): def decorator(func):
@depends(*args, when=when) @depends(*args, when=when)
def wrapper(*args): def wrapper(*args):
if any(arg for arg in args): if eval_args_fn(args):
return func(*args) return func(*args)
return wrapper return wrapper
return decorator return decorator
# Like @depends, but the decorated function is only called if one of the
# arguments it would be called with has a positive value (bool(value) is True)
@template
def depends_if(*args, **kwargs):
return depends_tmpl(any, *args, **kwargs)
# Like @depends, but the decorated function is only called if all of the
# arguments it would be called with have a positive value.
@template
def depends_all(*args, **kwargs):
return depends_tmpl(all, *args, **kwargs)
# Hacks related to old-configure # Hacks related to old-configure
# ============================== # ==============================