code: def discover_modules(entry=pyvista, recurse=True):
      """Discover the submodules present under an entry point.
  
      If ``recurse=True``, search goes all the way into descendants of the
      entry point. Only modules are gathered, because within a module
      ``doctest``'s discovery can work recursively.
  
      Should work for ``pyvista`` as entry, but no promises for its more
      general applicability.
  
      Parameters
      ----------
      entry : module, optional
          The entry point of the submodule search. Defaults to the main
          ``pyvista`` module.
  
      recurse : bool, optional
          Whether to recurse into submodules.
  
      Returns
      -------
      modules : dict of modules
          A (module name -> module) mapping of submodules under ``entry``.
  
      """
      entry_name = entry.__name__
      found_modules = {}
      next_entries = {entry}
      while next_entries:
          next_modules = {}
          for entry in next_entries:
              for attr_short_name in dir(entry):
                  attr = getattr(entry, attr_short_name)
                  if not isinstance(attr, ModuleType):
                      continue
  
                  module_name = attr.__name__
  
                  if module_name.startswith(entry_name):
                      next_modules[module_name] = attr
  
          if not recurse:
              return next_modules
  
          # find as-of-yet-undiscovered submodules
          next_entries = {
              module
              for module_name, module in next_modules.items()
              if module_name not in found_modules
          }
          found_modules.update(next_modules)
  
      return found_modules
  
file_path: pyvista/tests/check_doctest_names.py
imports: from argparse import ArgumentParser
  from doctest import DocTestFinder
  import re
  import sys
  from textwrap import indent
  from types import ModuleType
  import pyvista
label_desc: Use image node_id 1 for calling a global function with example usage: discover_modules(entry, recurse) and returns: found_modules, next_modules