Passed
Pull Request — rhel9-branch (#160)
by Matěj
01:14
created

OSCAPTuiSpoke.apply()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
#
2
# Copyright (C) 2013  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
# Red Hat Author(s): Vratislav Podzimek <[email protected]>
19
#
20
#
21
# NOTE: Anaconda is using Simpleline library for Text User Interface.
22
#       To learn how to use Simpleline look on the documentation:
23
#
24
#       http://python-simpleline.readthedocs.io/en/latest/
25
#
26
27
28
"""Module with the class for the Hello world TUI spoke."""
29
30
import re
31
32
from pyanaconda.ui.tui.spokes import NormalTUISpoke
33
from pyanaconda.ui.common import FirstbootSpokeMixIn
34
from pyanaconda.ui.categories.system import SystemCategory
35
36
# Simpleline's dialog configured for use in Anaconda
37
from pyanaconda.ui.tui.tuiobject import Dialog, PasswordDialog
38
39
from simpleline.render.prompt import Prompt
40
from simpleline.render.screen import InputState
41
from simpleline.render.containers import ListColumnContainer
42
from simpleline.render.widgets import CheckboxWidget, EntryWidget
43
44
from org_fedora_oscap.constants import OSCAP
45
from org_fedora_oscap.structures import PolicyData
46
47
# export only the HelloWorldSpoke and HelloWorldEditSpoke classes
48
__all__ = ["OSCAPTuiSpoke"]
49
50
# import gettext
51
# _ = lambda x: gettext.ldgettext("hello-world-anaconda-plugin", x)
52
53
# will never be translated
54
_ = lambda x: x
55
N_ = lambda x: x
56
57
58
class OSCAPTuiSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
59
    """
60
    Class for the Hello world TUI spoke that is a subclass of NormalTUISpoke. It
61
    is a simple example of the basic unit for Anaconda's text user interface.
62
    Since it is also inherited form the FirstbootSpokeMixIn, it will also appear
63
    in the Initial Setup (successor of the Firstboot tool).
64
65
    :see: pyanaconda.ui.tui.TUISpoke
66
    :see: pyanaconda.ui.common.FirstbootSpokeMixIn
67
    :see: simpleline.render.widgets.Widget
68
69
    """
70
71
    ### class attributes defined by API ###
72
73
    # category this spoke belongs to
74
    category = SystemCategory
75
76
    def __init__(self, data, storage, payload):
77
        """
78
        :see: simpleline.render.screen.UIScreen
79
        :param data: data object passed to every spoke to load/store data
80
                     from/to it
81
        :type data: pykickstart.base.BaseHandler
82
        :param storage: object storing storage-related information
83
                        (disks, partitioning, bootloader, etc.)
84
        :type storage: blivet.Blivet
85
        :param payload: object storing packaging-related information
86
        :type payload: pyanaconda.packaging.Payload
87
88
        """
89
90
        NormalTUISpoke.__init__(self, data, storage, payload)
91
        self.title = N_("_Security Policy")
92
        # the proxy to OSCAP DBus module
93
        self._oscap_module = OSCAP.get_proxy()
94
        self._policy_data = PolicyData.from_structure(
95
            self._oscap_module.PolicyData
96
        )
97
98
    def initialize(self):
99
        """
100
        The initialize method that is called after the instance is created.
101
        The difference between __init__ and this method is that this may take
102
        a long time and thus could be called in a separated thread.
103
104
        :see: pyanaconda.ui.common.UIObject.initialize
105
106
        """
107
108
        NormalTUISpoke.initialize(self)
109
110
    def refresh(self, args=None):
111
        """
112
        The refresh method that is called every time the spoke is displayed.
113
        It should update the UI elements according to the contents of
114
        self.data.
115
116
        :see: pyanaconda.ui.common.UIObject.refresh
117
        :see: simpleline.render.screen.UIScreen.refresh
118
        :param args: optional argument that may be used when the screen is
119
                     scheduled
120
        :type args: anything
121
122
        """
123
124
        # call parent method to setup basic container with screen title set
125
        super().refresh(args)
126
127
        fresh_data = PolicyData.from_structure(
128
            self._oscap_module.PolicyData
129
        )
130
131
        self._policy_data.update_from(fresh_data)
132
133
    def apply(self):
134
        """
135
        The apply method that is called when the spoke is left. It should
136
        update the contents of self.data with values set in the spoke.
137
138
        """
139
140
        self.data.addons.org_fedora_hello_world.text = self._entered_text
141
142
    def execute(self):
143
        """
144
        The execute method that is called when the spoke is left. It is
145
        supposed to do all changes to the runtime environment according to
146
        the values set in the spoke.
147
148
        """
149
150
        # nothing to do here
151
        pass
152
153
    @property
154
    def completed(self):
155
        """
156
        The completed property that tells whether all mandatory items on the
157
        spoke are set, or not. The spoke will be marked on the hub as completed
158
        or uncompleted according to the returned value.
159
160
        :rtype: bool
161
162
        """
163
164
        return True
165
166
    @property
167
    def status(self):
168
        """
169
        The status property that is a brief string describing the state of the
170
        spoke. It should describe whether all values are set and if possible
171
        also the values themselves. The returned value will appear on the hub
172
        below the spoke's title.
173
174
        :rtype: str
175
176
        """
177
178
        profile_id = self._policy_data.profile_id
179
180
        if profile_id:
181
            return _(f"Selected profile: {profile_id}")
182
        else:
183
            return _("Not hardening anything")
184
185
    def input(self, args, key):
186
        """
187
        The input method that is called by the main loop on user's input.
188
189
        :param args: optional argument that may be used when the screen is
190
                     scheduled
191
        :type args: anything
192
        :param key: user's input
193
        :type key: unicode
194
        :return: if the input should not be handled here, return it, otherwise
195
                 return InputState.PROCESSED or InputState.DISCARDED if the input was
196
                 processed successfully or not respectively
197
        :rtype: enum InputState
198
199
        """
200
201
        return key
202
203
    def prompt(self, args=None):
204
        """
205
        The prompt method that is called by the main loop to get the prompt
206
        for this screen.
207
208
        :see: simpleline.render.prompt.Prompt
209
210
        :param args: optional argument that can be passed to App.switch_screen*
211
                     methods
212
        :type args: anything
213
        :return: text that should be used in the prompt for the input
214
        :rtype: instance of simpleline.render.prompt.Prompt or None
215
        """
216
217
        return Prompt(_("Don't do anything"))
218