polymerist.genutils.importutils.pkgiter

Tools for iterating over and extracting information from Python package hierarchies

Attributes

LOGGER

module_tree

Classes

ModuleToNodeCorrespondence

Concrete implementation of Python modules and packages as nodes in a tree

Functions

module_tree_direct(→ anytree.node.Node)

Produce a tree from the Python package hierarchy starting with a given module

iter_submodules(→ Generator[types.ModuleType, None, None])

Generates all modules which can be imported from the given toplevel module

iter_submodule_info(...)

Generates information about all modules which can be imported from the given toplevel module

register_submodules(→ None)

Registers all submodules of a given module into it's own namespace (i.e. autoimports submodules)

module_hierarchy() → str)

Generates a printable string which summarizes a Python packages hierarchy. Reminiscent of GNU tree output

Module Contents

polymerist.genutils.importutils.pkgiter.LOGGER
class polymerist.genutils.importutils.pkgiter.ModuleToNodeCorrespondence[source]

Bases: polymerist.genutils.trees.treebase.NodeCorrespondence

Concrete implementation of Python modules and packages as nodes in a tree

name(module: types.ModuleType) str[source]

Define how to obtain a string name

has_children(module: types.ModuleType) bool[source]

Define how to check if an object can produce children in the first place before attempting to do so

children(module: types.ModuleType) Iterable[types.ModuleType][source]

Define how to obtain node children from an instance Should return NoneType if the instance is “leaf-like”

polymerist.genutils.importutils.pkgiter.module_tree
polymerist.genutils.importutils.pkgiter.module_tree_direct(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) anytree.node.Node[source]

Produce a tree from the Python package hierarchy starting with a given module

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Returns:

modtree – The root node of the module tree, corresponding to the module object passed to “module”

Return type:

Node

polymerist.genutils.importutils.pkgiter.iter_submodules(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) Generator[types.ModuleType, None, None][source]

Generates all modules which can be imported from the given toplevel module

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Returns:

submodules – A generator which yields modules in traversal pre-order as they appear wihin the package hierarchy

Return type:

Generator[ModuleType]

polymerist.genutils.importutils.pkgiter.iter_submodule_info(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) Generator[tuple[types.ModuleType, str, bool], None, None][source]

Generates information about all modules which can be imported from the given toplevel module Namely, yields the module object, module name, and whether or not the module is a package

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Returns:

submodule_info – A generator which yields modules info in traversal pre-order as they appear wihin the package hierarchy yields 3-tuples containing ModuleType objects, module names, and whether the current module is also a subpackage

Return type:

Generator[ModuleType, str, bool]

polymerist.genutils.importutils.pkgiter.register_submodules(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None) None[source]

Registers all submodules of a given module into it’s own namespace (i.e. autoimports submodules)

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

Return type:

None

polymerist.genutils.importutils.pkgiter.module_hierarchy(module: types.ModuleType, recursive: bool = True, blacklist: Container[str] | None = None, style: str | anytree.render.AbstractStyle = ContStyle()) str[source]

Generates a printable string which summarizes a Python packages hierarchy. Reminiscent of GNU tree output

Parameters:
  • module (ModuleType) – The “root” module to begin importing from Represented in the Node object returned by this function

  • recursive (bool, default=True) – Whether or not to recursively import modules from subpackages and add them to the tree

  • blacklist (Container[str] (optional)) – List of module names to exclude from tree building If provided, will exclude any modules whose names occur in this list

  • style (str or AbstractStyle) – An element drawing style for the final tree structure printout

Returns:

module_summary – Printable string which displays the package structure

Return type:

str