Container object for the variables defined in a config file. `VariableSet` can be used as a plain dictionary, with the variable names as keys. Parameters ---------- d : dict Dict of items in the "variables" section of the configuration file.
| 146 | return "\n".join(m) |
| 147 | |
| 148 | class VariableSet: |
| 149 | """ |
| 150 | Container object for the variables defined in a config file. |
| 151 | |
| 152 | `VariableSet` can be used as a plain dictionary, with the variable names |
| 153 | as keys. |
| 154 | |
| 155 | Parameters |
| 156 | ---------- |
| 157 | d : dict |
| 158 | Dict of items in the "variables" section of the configuration file. |
| 159 | |
| 160 | """ |
| 161 | def __init__(self, d): |
| 162 | self._raw_data = dict([(k, v) for k, v in d.items()]) |
| 163 | |
| 164 | self._re = {} |
| 165 | self._re_sub = {} |
| 166 | |
| 167 | self._init_parse() |
| 168 | |
| 169 | def _init_parse(self): |
| 170 | for k, v in self._raw_data.items(): |
| 171 | self._init_parse_var(k, v) |
| 172 | |
| 173 | def _init_parse_var(self, name, value): |
| 174 | self._re[name] = re.compile(r'\$\{%s\}' % name) |
| 175 | self._re_sub[name] = value |
| 176 | |
| 177 | def interpolate(self, value): |
| 178 | # Brute force: we keep interpolating until there is no '${var}' anymore |
| 179 | # or until interpolated string is equal to input string |
| 180 | def _interpolate(value): |
| 181 | for k in self._re.keys(): |
| 182 | value = self._re[k].sub(self._re_sub[k], value) |
| 183 | return value |
| 184 | while _VAR.search(value): |
| 185 | nvalue = _interpolate(value) |
| 186 | if nvalue == value: |
| 187 | break |
| 188 | value = nvalue |
| 189 | |
| 190 | return value |
| 191 | |
| 192 | def variables(self): |
| 193 | """ |
| 194 | Return the list of variable names. |
| 195 | |
| 196 | Parameters |
| 197 | ---------- |
| 198 | None |
| 199 | |
| 200 | Returns |
| 201 | ------- |
| 202 | names : list of str |
| 203 | The names of all variables in the `VariableSet` instance. |
| 204 | |
| 205 | """ |
no outgoing calls
no test coverage detected