diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2015-02-17 00:21:15 -0800 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2015-02-17 00:21:15 -0800 |
commit | 67db8ddca8ac7ab9adeb827a7dadd34a385b2b6b (patch) | |
tree | 08221a70f67226eeb5e5bead1d6043fb5e24f113 /lib | |
parent | 06d6b0b205095f849ace216aa51393b907cf821b (diff) | |
download | spack-67db8ddca8ac7ab9adeb827a7dadd34a385b2b6b.tar.gz spack-67db8ddca8ac7ab9adeb827a7dadd34a385b2b6b.tar.bz2 spack-67db8ddca8ac7ab9adeb827a7dadd34a385b2b6b.tar.xz spack-67db8ddca8ac7ab9adeb827a7dadd34a385b2b6b.zip |
Factor ignore logic into a predicate builder.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/llnl/util/lang.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index db15da0506..332367f537 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -291,6 +291,37 @@ def check_kwargs(kwargs, fun): % (next(kwargs.iterkeys()), fun.__name__)) +def match_predicate(*args): + """Utility function for making string matching predicates. + + Each arg can be a: + - regex + - list or tuple of regexes + - predicate that takes a string. + + This returns a predicate that is true if: + - any arg regex matches + - any regex in a list or tuple of regexes matches. + - any predicate in args matches. + """ + def match(string): + for arg in args: + if isinstance(arg, basestring): + if re.search(arg, string): + return True + elif isinstance(arg, list) or isinstance(arg, tuple): + if any(re.search(i, string) for i in arg): + return True + elif callable(arg): + if arg(string): + return True + else: + raise ValueError("args to match_predicate must be regex, " + "list of regexes, or callable.") + return False + return match + + class RequiredAttributeError(ValueError): def __init__(self, message): super(RequiredAttributeError, self).__init__(message) |