- import the module based on the path string
- get all the variables of the module
- restrict it to be only enums (those which are classes, subclasses of the enum classes, but not a builtin enum subclass)
- get just the docstrings
from enum import EnumType, EnumMeta, Enum, IntEnum, Flag, IntFlag, ReprEnum
import importlib.util
import inspect
def get_documentation(path_to_file, module_name):
spec = importlib.util.spec_from_file_location(module_name, path_to_file)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
variables = {
item: getattr(mod, item) for item in dir(mod) if not item.startswith("__")
}
enums = {
name: value
for name, value in variables.items()
if inspect.isclass(value)
and issubclass(
value, (EnumType, EnumMeta, Enum, IntEnum, Flag, IntFlag, ReprEnum)
)
and value not in (EnumType, EnumMeta, Enum, IntEnum, Flag, IntFlag, ReprEnum)
}
docstrings = {name: value.__doc__ for name, value in enums.items()}
return docstrings
For this example, the output is
{'Enumeration1': '\n Some documentation.\n '}
If you want to get rid of the extra whitespace, it's as simple as adding .strip()
after .__doc__