]> scripts.mit.edu Git - wizard.git/blob - wizard/app/__init__.py
1e924335a5c2f2cad31f95ea66eac44ce3b4a2fa
[wizard.git] / wizard / app / __init__.py
1 import os.path
2
3 def filename_regex_extractor(f):
4     """This is a decorator to apply to functions that take a name and return
5     (filename, RegexObject) tuples.  It converts it into a function
6     that takes a name and returns another function (the actual extractor)
7     which takes a deployment and returns the value of the extracted variable.
8
9     The regular expression requires a very specific form, essentially ()()()
10     (with the second subgroup being the value we care about), so that we can
11     reuse the regex for other things.
12
13     Its Haskell-style type signature would be:
14         (String -> (Filename, Regex)) -> (String -> (Deployment -> String))
15
16     It's a little bit like a transformer."""
17     def g(var):
18         file, regex = f(var)
19         def h(deployment):
20             contents = deployment.read(file) # cached
21             match = regex.search(contents)
22             if not match: return None
23             # assumes that the second match is the one we want.
24             return match.group(2)
25         return h
26     return g