Skip to content

ImportlibTreeModel

Base classes

Name Children Inherits
ColumnItemModel
prettyqt.itemmodels.columnitemmodel
TreeModel with columns defined by ColumnItem classes.

â‹” Inheritance diagram

graph TD
  2452743520["itemmodels.ImportlibTreeModel"]
  3096575510["itemmodels.ColumnItemModel"]
  1030521844["itemmodels.ColumnItemModelMixin"]
  2751680898["builtins.object"]
  2846913429["itemmodels.TreeModel"]
  874904086["core.AbstractItemModel"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  3096575510 --> 2452743520
  1030521844 --> 3096575510
  2751680898 --> 1030521844
  2846913429 --> 3096575510
  874904086 --> 2846913429
  3882849576 --> 874904086
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  1898060022 --> 874904086
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ColumnItemModel

Model showing the dependency tree of a distribution.

Source code in prettyqt/itemmodels/importlibdistributionmodel.py
class ImportlibTreeModel(itemmodels.ColumnItemModel):
    """Model showing the dependency tree of a distribution."""

    @core.Enum
    class Roles(enum.IntEnum):
        DistributionRole = constants.USER_ROLE + 43255

    SUPPORTS = metadata.Distribution | str | list[metadata.Distribution]
    TreeItem = DistTreeItem
    IS_RECURSIVE = True
    COLUMNS: ClassVar = [
        NameColumn,
        VersionColumn,
        ConstraintsColumn,
        MarkerColumn,
        SummaryColumn,
        HomepageColumn,
        AuthorColumn,
        LicenseColumn,
    ]

    def __init__(
        self,
        obj: metadata.Distribution | str | list[metadata.Distribution],
        show_root: bool = False,
        parent: core.QObject | None = None,
    ):
        """Show the requirements tree of a distribution.

        Arguments:
            obj: Distribution (or name of it). If a list of distributions is given,
                 they will be shown as top level items.
            show_root: whether to show the distribution itself as the root item.
            parent: parent QObject
        """
        if isinstance(obj, str):
            obj = metadata.distribution(obj)
        if isinstance(obj, list):
            show_root = False
        super().__init__(
            obj=obj, columns=self.COLUMNS, parent=parent, show_root=show_root
        )

    @classmethod
    def setup_example(cls):
        from prettyqt import widgets

        model = cls("prettyqt")
        table = widgets.TreeView(word_wrap=False)
        table.set_model(model)
        table.set_delegate("render_link", column=5)
        table.expand_all(depth=4)
        return table

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, metadata.Distribution)

    @classmethod
    def from_system(cls, parent: core.QObject | None = None) -> Self:
        return cls(list_system_modules(), parent=parent)

    @classmethod
    def from_package(cls, package_name: str, parent: core.QObject | None = None) -> Self:
        return cls(list_package_requirements(package_name), parent=parent)

    def _has_children(self, item: DistTreeItem) -> bool:
        return bool(item.obj) if isinstance(item.obj, list) else bool(item.requires)

    def _fetch_object_children(self, item: DistTreeItem) -> list[DistTreeItem]:
        if isinstance(item.obj, list):
            return [DistTreeItem(obj=dist) for dist in item.obj]
        if not item.requires:
            return []
        dists = (load_dist_info(req.name) for req in item.requires.values())
        return [DistTreeItem(obj=dist) for dist in dists if dist is not None]

__init__(obj: metadata.Distribution | str | list[metadata.Distribution], show_root: bool = False, parent: core.QObject | None = None)

Show the requirements tree of a distribution.

Parameters:

Name Type Description Default
obj Distribution | str | list[Distribution]

Distribution (or name of it). If a list of distributions is given, they will be shown as top level items.

required
show_root bool

whether to show the distribution itself as the root item.

False
parent QObject | None

parent QObject

None
Source code in prettyqt/itemmodels/importlibdistributionmodel.py
def __init__(
    self,
    obj: metadata.Distribution | str | list[metadata.Distribution],
    show_root: bool = False,
    parent: core.QObject | None = None,
):
    """Show the requirements tree of a distribution.

    Arguments:
        obj: Distribution (or name of it). If a list of distributions is given,
             they will be shown as top level items.
        show_root: whether to show the distribution itself as the root item.
        parent: parent QObject
    """
    if isinstance(obj, str):
        obj = metadata.distribution(obj)
    if isinstance(obj, list):
        show_root = False
    super().__init__(
        obj=obj, columns=self.COLUMNS, parent=parent, show_root=show_root
    )

:fa-brands-github: Show source on GitHub