Skip to content

ModuleInfoModel

Qt Base Class: QAbstractItemModel

Signature: QAbstractItemModel(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
TreeModel
prettyqt.itemmodels.treemodel

⋔ Inheritance diagram

graph TD
  1473245442096["itemmodels.ModuleInfoModel"]
  1473299686192["itemmodels.TreeModel"]
  1473299893104["core.AbstractItemModel"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473299686192 --> 1473245442096
  1473299893104 --> 1473299686192
  1473299890176 --> 1473299893104
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289050128 --> 1473299893104
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: TreeModel

Tree Model to display a module hierarchy (using pkgutil).

Source code in prettyqt\itemmodels\moduleinfomodel.py
class ModuleInfoModel(itemmodels.TreeModel):
    """Tree Model to display a module hierarchy (using pkgutil)."""

    HEADER = ["Name", "Path", "Is Package"]
    SUPPORTS = str | os.PathLike | types.ModuleType | pkgutil.ModuleInfo

    def __init__(self, obj, **kwargs):
        match obj:
            case str() | os.PathLike():
                path = pathlib.Path(obj)
                obj = pkgutil.ModuleInfo(
                    module_finder=machinery.FileFinder(str(path.parent)),
                    name=path.name,
                    ispkg=True,
                )
            case types.ModuleType():
                path = pathlib.Path(obj)
                obj = pkgutil.ModuleInfo(
                    module_finder=machinery.FileFinder(str(path.parent)),
                    name=path.name,
                    ispkg=True,
                )
            case pkgutil.ModuleInfo():
                pass
            case _:
                raise TypeError(obj)
        super().__init__(obj, **kwargs)

    @core.Enum
    class Roles(enum.IntEnum):
        ModuleInfoRole = constants.USER_ROLE + 2

    def columnCount(self, parent=None):
        return len(self.HEADER)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role, section:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE, _:
                return self.HEADER[section]
        return None

    def data(self, index: core.ModelIndex, role=constants.DISPLAY_ROLE):
        if not index.isValid():
            return None
        info = self.data_by_index(index).obj
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                return info.name
            case constants.DISPLAY_ROLE, 1:
                return str(info.module_finder.path)
            case constants.CHECKSTATE_ROLE, 2:
                return self.to_checkstate(info.ispkg)
            case self.Roles.ModuleInfoRole:
                return info

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, pkgutil.ModuleInfo | types.ModuleType)

    def _fetch_object_children(
        self, item: ModuleInfoModel.TreeItem
    ) -> list[ModuleInfoModel.TreeItem]:
        return [
            self.TreeItem(obj=i)
            for i in pkgutil.iter_modules(
                [f"{item.obj.module_finder.path}\\{item.obj.name}"]
            )
        ]

    def _has_children(self, item: ModuleInfoModel.TreeItem) -> bool:
        path = [f"{item.obj.module_finder.path}\\{item.obj.name}"]
        return any(pkgutil.iter_modules(path))

Info

Supported data type: str | os.PathLike | module | pkgutil.ModuleInfo

⌗ Property table

Qt Property Type Doc
objectName QString