5 if isinstance(val, str):
8 return '(?:' + '|'.join(map(expand_re, val)) + ')'
10 def filename_regex_extractor(f):
12 This is a decorator to apply to functions that take a name and return
13 (filename, RegexObject) tuples. It converts it into a function
14 that takes a name and returns another function (the actual extractor)
15 which takes a deployment and returns the value of the extracted variable.
17 The regular expression requires a very specific form, essentially ()()()
18 (with the second subgroup being the value we care about), so that we can
19 reuse the regex for other things.
21 Its Haskell-style type signature would be::
23 (String -> (Filename, Regex)) -> (String -> (Deployment -> String))
25 For convenience purposes, we also accept [Filename], in which case
26 we use the first entry (index 0). Passing an empty list is invalid.
30 if not isinstance(file, str):
33 contents = deployment.read(file) # cached
34 match = regex.search(contents)
35 if not match: return None
36 # assumes that the second match is the one we want.
41 def filename_regex_substitution(f):
43 This is a decorator to apply to functions that take a name and return
44 (filename, RegexObject) tuples. It converts it into a function
45 that takes a name and returns another function (that does substitution)
46 which takes a deployment and modifies its files to replace explicit
47 values with their generic WIZARD_* equivalents. The final function returns
48 the number of replacements made.
50 The regular expression requires a very specific form, essentially ()()()
51 (with the second subgroup being the value to be replaced).
53 Its Haskell-style type signature would be::
55 (String -> ([Filename], Regex)) -> (String -> (Deployment -> IO Int))
57 For convenience purposes, we also accept Filename, in which case it is treated
58 as a single item list.
62 if isinstance(files, str):
65 base = deployment.location
68 file = os.path.join(base, file)
69 contents = open(file, "r").read()
70 contents, n = regex.subn("\\1" + key + "\\3", contents)
72 open(file, "w").write(contents)