Find all named replacements in the header Returns a list of dictionaries, one for each loop iteration, where each key is a name to be substituted and the corresponding value is the replacement string. Also return a list of exclusions. The exclusions are dictionaries of key va
(loophead)
| 159 | exclude_vars_re = re.compile(r"(\w*)=(\w*)") |
| 160 | exclude_re = re.compile(":exclude:") |
| 161 | def parse_loop_header(loophead): |
| 162 | """Find all named replacements in the header |
| 163 | |
| 164 | Returns a list of dictionaries, one for each loop iteration, |
| 165 | where each key is a name to be substituted and the corresponding |
| 166 | value is the replacement string. |
| 167 | |
| 168 | Also return a list of exclusions. The exclusions are dictionaries |
| 169 | of key value pairs. There can be more than one exclusion. |
| 170 | [{'var1':'value1', 'var2', 'value2'[,...]}, ...] |
| 171 | |
| 172 | """ |
| 173 | # Strip out '\n' and leading '*', if any, in continuation lines. |
| 174 | # This should not effect code previous to this change as |
| 175 | # continuation lines were not allowed. |
| 176 | loophead = stripast.sub("", loophead) |
| 177 | # parse out the names and lists of values |
| 178 | names = [] |
| 179 | reps = named_re.findall(loophead) |
| 180 | nsub = None |
| 181 | for rep in reps: |
| 182 | name = rep[0] |
| 183 | vals = parse_values(rep[1]) |
| 184 | size = len(vals) |
| 185 | if nsub is None: |
| 186 | nsub = size |
| 187 | elif nsub != size: |
| 188 | msg = "Mismatch in number of values, %d != %d\n%s = %s" |
| 189 | raise ValueError(msg % (nsub, size, name, vals)) |
| 190 | names.append((name, vals)) |
| 191 | |
| 192 | # Find any exclude variables |
| 193 | excludes = [] |
| 194 | |
| 195 | for obj in exclude_re.finditer(loophead): |
| 196 | span = obj.span() |
| 197 | # find next newline |
| 198 | endline = loophead.find('\n', span[1]) |
| 199 | substr = loophead[span[1]:endline] |
| 200 | ex_names = exclude_vars_re.findall(substr) |
| 201 | excludes.append(dict(ex_names)) |
| 202 | |
| 203 | # generate list of dictionaries, one for each template iteration |
| 204 | dlist = [] |
| 205 | if nsub is None: |
| 206 | raise ValueError("No substitution variables found") |
| 207 | for i in range(nsub): |
| 208 | tmp = {name: vals[i] for name, vals in names} |
| 209 | dlist.append(tmp) |
| 210 | return dlist |
| 211 | |
| 212 | |
| 213 | replace_re = re.compile(r"@(\w+)@") |
no test coverage detected
searching dependent graphs…