import collections
import six


def _unflatten_dict(flat_dict, prefixes, delimiter="/"):
  """Unflattens a dictionary into a dict of dicts by one level.

  Args:
    flat_dict: The dictionary to unflatten.
    prefixes: The string keys to use for the unflattened dictionary. Keys in the
      flat_dict which do not begin with a prefix are unmodified.
    delimiter: The value used to delmit the keys in the flat_dict.

  Returns:
    The unflattened dictionary.
  """

  unflat_dict = collections.defaultdict(dict)
  for key, value in six.iteritems(flat_dict):
    parts = key.split(delimiter)
    if len(parts) > 1:
      prefix = parts[0]
      if prefix in prefixes:
        suffix = key[len(prefix + delimiter):]
        unflat_dict[prefix][suffix] = value
      else:
        unflat_dict[key] = value
    else:
      unflat_dict[key] = value
  return unflat_dict