File: objclass.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 (73 lines) | stat: -rw-r--r-- 2,440 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# SPDX-License-Identifier: LGPL-2.1-only
from itertools import chain

from PyQt6 import QtGui, QtWidgets
import setools

from . import util

__all__ = ('objclass_detail', 'objclass_detail_action', 'objclass_tooltip')


def objclass_detail(class_: setools.ObjClass, parent: QtWidgets.QWidget | None = None) -> None:
    """Display a dialog with object class details."""
    try:
        common = class_.common
        common_details = \
            f"""
            <h2>Permissions inherited  from {common} ({len(common.perms)})</h2>
            <ul>
            {"".join(f"<li>{p}</li>" for p in sorted(common.perms))}
            </ul>
            """

    except setools.exception.NoCommon:
        common_details = ""

    util.display_object_details(
        f"{class_} Details",
        f"""
        <h1>Object Class Name</h1>
        <p>{class_}<p>

        {common_details}

        <h2>Permissions ({len(class_.perms)})</h2>
        <ul>
        {"".join(f"<li>{p}</li>" for p in sorted(class_.perms))}
        </ul>
        """,
        parent)


def objclass_detail_action(class_: setools.ObjClass,
                           parent: QtWidgets.QWidget | None = None) -> QtGui.QAction:
    """Return a QAction that, when triggered, opens an object class detail popup."""
    a = QtGui.QAction(f"Properties of {class_}")
    a.triggered.connect(lambda x: objclass_detail(class_, parent))
    return a


def objclass_tooltip(class_: setools.ObjClass) -> str:
    """Return tooltip text for this object class."""
    try:
        nperms = len(class_.perms) + len(class_.common.perms)
        if nperms == 0:
            return f"{class_} is an object class with no permissions defined."
        elif nperms > 5:
            return \
                f"{class_} inherits {class_.common} and consists of " \
                f"{nperms} permissions."
        else:
            return f"{class_.name} is an object class with permissions: " \
                   f"{', '.join(chain(class_.common.perms, class_.perms))}"

    except setools.exception.NoCommon:
        nperms = len(class_.perms)
        if nperms == 0:
            return f"{class_} is an object class with no permissions defined."
        elif nperms > 5:
            return f"{class_} is an object class with {nperms} permissions defined."
        else:
            return f"{class_} is an object class with permissions: " \
                    f"{', '.join(class_.perms)}"