import inspect


def depth_filter(path, parent, children):
  """Depth filter.

  This is intended to filter out "non-public" objects. The general idea is that
  in the root directory we define an __init__.py that imports all the modules
  that should be exposed publicly. For each of those modules, there is also an
  associated __init__.py that imports everything that should be exposed publicly
  at that level. We don't have anything nested deeper than that in the public
  API (e.g. tfma.rootimport.subimport.object). If this changes then this
  filter will need to be updated.

  As such, we can filter on depth: we show objects at depth 2
  (e.g. tfma.evaluators.MetricsAndPlotsEvaluator), but not modules at depth 2
  (e.g. tfma.evaluators.counter_util).

  We also do not descend into modules beyond depth 2 - so we descend into
  tfma, and tfma.evaluators (and so on), but no further.

  Args:
    path: Path to parent
    parent: Parent
    children: List of children

  Returns:
    Filtered list of children.
  """
  del parent

  if len(path) == 1:
    return children

  # At depth 2 and beyond don't descend into child modules.
  filtered_children = []
  for pair in children:
    _, child = pair
    if inspect.ismodule(child):
      continue
    filtered_children.append(pair)
  return filtered_children