import ast


def _to_dict(contents):
  """Parse |contents| as a dict, returning None on failure or if it's not a
  dict."""
  try:
    result = ast.literal_eval(contents)
    if isinstance(result, dict):
      return result

  except (ValueError, TypeError):
    pass

  return None