File: boolean.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 (98 lines) | stat: -rw-r--r-- 3,406 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# SPDX-License-Identifier: LGPL-2.1-only

from PyQt6 import QtWidgets
import setools

from .. import models
from .combobox import ComboBoxWidget
from .list import ListWidget
from .name import NameWidget

# Regex for exact matches to types/attrs
VALIDATE_EXACT = r"[A-Za-z0-9._-]*"

__all__ = ("BooleanList", "BooleanName", "BooleanState")


class BooleanList(ListWidget):

    """A widget providing a QListView widget for selecting zero or more Booleans."""

    def __init__(self, title: str, query, attrname: str, enable_equal: bool = True,
                 parent: QtWidgets.QWidget | None = None) -> None:

        model = models.BooleanTable(data=sorted(query.policy.bools()))

        super().__init__(title, query, attrname, model, enable_equal=enable_equal, parent=parent)

        self.criteria_any.setToolTip("Any selected Boolean will match.")
        self.criteria_any.setWhatsThis("<b>Any selected Boolean will match.</b>")

        if enable_equal:
            self.criteria_equal.setToolTip("The selected Booleans must exactly match.")
            self.criteria_equal.setWhatsThis("<b>The selected Booleans must exactly match.</b>")


class BooleanName(NameWidget):

    """
    Widget providing a QLineEdit for the user to enter a Boolean name, with
    the criteria saved to the attributes of the specified query.
    """

    def __init__(self, title: str, query, attrname: str,
                 parent: QtWidgets.QWidget | None = None,
                 enable_regex: bool = True, required: bool = False):

        completion: list[str] = sorted(b.name for b in query.policy.bools())

        super().__init__(title, query, attrname, completion, VALIDATE_EXACT,
                         enable_regex=enable_regex, required=required, parent=parent)


class BooleanState(ComboBoxWidget):

    """Criteria selection widget presenting possible Boolean states."""

    def __init__(self, title: str, query: setools.PolicyQuery, attrname: str, /, *,
                 enable_any: bool = True, parent: QtWidgets.QWidget | None = None) -> None:

        super().__init__(title, query, attrname, enable_any=enable_any, parent=parent)

        self.criteria.addItem("False", False)
        self.criteria.addItem("True", True)


if __name__ == '__main__':
    import sys
    import logging
    import warnings
    import setools

    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
    warnings.simplefilter("default")

    p = setools.SELinuxPolicy()
    q1 = setools.TERuleQuery(p)
    q2 = setools.BoolQuery(p)

    app = QtWidgets.QApplication(sys.argv)
    mw = QtWidgets.QMainWindow()
    window = QtWidgets.QWidget(mw)
    layout = QtWidgets.QHBoxLayout(window)
    widget1 = BooleanList("Test Booleans list", q1, "boolean", parent=window)
    widget2 = BooleanName("Test Booleans linedit", q2, "name", parent=window)
    widget3 = BooleanState("Test Booleans State", q2, "default", enable_any=True, parent=window)
    layout.addWidget(widget1)
    layout.addWidget(widget2)
    layout.addWidget(widget3)
    window.setToolTip("test tooltip")
    window.setWhatsThis("test what's this")
    mw.setCentralWidget(window)
    mw.resize(window.size())
    whatsthis = QtWidgets.QWhatsThis.createAction(mw)
    mw.menuBar().addAction(whatsthis)  # type: ignore[union-attr]
    mw.show()
    rc = app.exec()
    sys.exit(rc)