File: typeattr.py

package info (click to toggle)
setools 4.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,600 kB
  • sloc: python: 24,485; makefile: 14
file content (50 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# SPDX-License-Identifier: LGPL-2.1-only

from PyQt6 import QtGui, QtWidgets
import setools

from . import util

__all__ = ('typeattr_detail', 'typeattr_detail_action', 'typeattr_tooltip')


def typeattr_detail(attr: setools.TypeAttribute, parent: QtWidgets.QWidget | None = None) -> None:

    """Display a dialog with type attribute details."""

    types = list[setools.Type](sorted(attr.expand()))

    util.display_object_details(
        f"{attr} Details",
        f"""
        <h1>Type Attribute Name</h1>
        <p>{attr}<p>

        <h2>Types ({len(types)})</h2>
        <ul>
        {"".join(f"<li>{t}</li>" for t in types)}
        </ul>
        """,
        parent)


def typeattr_detail_action(attr: setools.TypeAttribute,
                           parent: QtWidgets.QWidget | None = None) -> QtGui.QAction:

    """Return a QAction that, when triggered, opens an detail popup for the attr."""

    a = QtGui.QAction(f"Properties of {attr}")
    a.triggered.connect(lambda _: typeattr_detail(attr, parent))
    return a


def typeattr_tooltip(attr: setools.TypeAttribute) -> str:
    """Return tooltip text for this type attribute."""
    n_types = len(attr)
    if n_types == 0:
        return f"{attr.name} is an empty attribute."
    elif n_types > 5:
        return f"{attr.name} is an attribute consisting of {n_types} types."
    else:
        return f"{attr.name} is an attribute consisting of: " \
                f"{', '.join(t.name for t in attr.expand())}"