Bug 1269513 - Add a template to execute a depends function only when a given value is present. r=glandium draft
authorChris Manchester <cmanchester@mozilla.com>
Mon, 16 May 2016 16:43:53 -0700
changeset 367602 75423e6be2f647e5f6bf5c5a3c9c8111c7af4a3e
parent 367573 a884b96685aa13b65601feddb24e5f85ba861561
child 367603 b11ba8f5fcc001799015ab7d3fdfb9b53d8fe884
push id18288
push usercmanchester@mozilla.com
push dateMon, 16 May 2016 23:44:41 +0000
reviewersglandium
bugs1269513
milestone49.0a1
Bug 1269513 - Add a template to execute a depends function only when a given value is present. r=glandium MozReview-Commit-ID: H99fVOqlh8d
build/moz.configure/util.configure
--- a/build/moz.configure/util.configure
+++ b/build/moz.configure/util.configure
@@ -121,8 +121,24 @@ def delayed_getattr(func, key):
 def depends_if(*args):
     def decorator(func):
         @depends(*args)
         def wrapper(*args):
             if any(arg for arg in args):
                 return func(*args)
         return wrapper
     return decorator
+
+# Like @depends_if, but a distinguished value passed as a keyword argument
+# "when" is truth tested instead of every argument. This value is not passed
+# to the function if it is called.
+@template
+def depends_when(*args, **kwargs):
+    if not len(kwargs) == 1 and kwargs.get('when'):
+        die('depends_when requires a single keyword argument, "when"')
+    when = kwargs['when']
+    def decorator(fn):
+        @depends(when, *args)
+        def wrapper(val, *args):
+            if val:
+                return fn(*args)
+        return wrapper
+    return decorator