code: def expand_variables(template_str, value_map, transformer=None):
    
    if template_str is None:
      return None
    else:
      if transformer is None:
        transformer = lambda v: v
      try:
        # Don't bother iterating items for Python 2+3 compatibility.
        transformed_value_map = {k: transformer(value_map[k]) for k in value_map}
        return Template(template_str).substitute(transformed_value_map)
      except Exception as e:
        raise ValueError("could not expand variable names in command '%s': %s" % (template_str, e))
docs: Expand a template string like "blah blah $FOO blah" using given value mapping.