Resolve each variables in a ``variables`` list of ShellVariable. Return a tuple of (list with updated variables, list of error messages). Do not report errors for variable with a name listed in the ``needed_variables`` set if provided.
(cls, variables, needed_variables=None)
| 224 | |
| 225 | @classmethod |
| 226 | def resolve(cls, variables, needed_variables=None): |
| 227 | """ |
| 228 | Resolve each variables in a ``variables`` list of ShellVariable. Return |
| 229 | a tuple of (list with updated variables, list of error messages). Do not |
| 230 | report errors for variable with a name listed in the |
| 231 | ``needed_variables`` set if provided. |
| 232 | """ |
| 233 | |
| 234 | def reportable(v): |
| 235 | if needed_variables: |
| 236 | return v.name in needed_variables |
| 237 | return True |
| 238 | |
| 239 | # mapping of variables that we use for resolution |
| 240 | # the mappings and values are updated as resolution progresses |
| 241 | environment = {} |
| 242 | errors = [] |
| 243 | for var in variables: |
| 244 | |
| 245 | if not environment: |
| 246 | if reportable(var) and not var.is_resolved(): |
| 247 | errors.append(f'Unresolvable first variable: {var}') |
| 248 | |
| 249 | if not var.is_array: |
| 250 | # we do not know how to expand an array |
| 251 | environment[var.name] = var.value |
| 252 | continue |
| 253 | |
| 254 | if var.is_resolved(): |
| 255 | if not var.is_array: |
| 256 | # we do not know how to expand an array |
| 257 | environment[var.name] = var.value |
| 258 | continue |
| 259 | |
| 260 | try: |
| 261 | if var.is_array: |
| 262 | expanded = [] |
| 263 | for item in var.value: |
| 264 | exp = pe.expand(item, env=environment) |
| 265 | if reportable(var) and ' ' in item and ' ' not in expanded: |
| 266 | errors.append(f'Expansion munged spaces in value: {item}') |
| 267 | expanded.append(exp) |
| 268 | else: |
| 269 | expanded = pe.expand(var.value, env=environment) |
| 270 | if reportable(var) and ' ' in var.value and ' ' not in expanded: |
| 271 | errors.append(f'Expansion munged spaces in value: {var.value}') |
| 272 | |
| 273 | if TRACE: |
| 274 | logger_debug( |
| 275 | f'Resolved variable: {var} to: {expanded} ' |
| 276 | f'with envt: {environment} ' |
| 277 | ) |
| 278 | |
| 279 | var.value = expanded |
| 280 | |
| 281 | if not var.is_array: |
| 282 | # we do not know how to expand an array |
| 283 | environment[var.name] = expanded |
no test coverage detected