summaryrefslogtreecommitdiffstats
path: root/pyanaconda/ui/__init__.py
blob: c60698d646fc3f74ccfca4400091eaa521c5f836 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Base classes for all user interfaces.
#
# Copyright (C) 2011-2012  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.  You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Chris Lumens <clumens@redhat.com>
#

__all__ = ["UserInterface"]

import os
from .common import collect, PathDict

class UserInterface(object):
    """This is the base class for all kinds of install UIs.  It primarily
       defines what kinds of dialogs and entry widgets every interface must
       provide that the rest of anaconda may rely upon.
    """
    def __init__(self, storage, payload, instclass):
        """Create a new UserInterface instance.

           The arguments this base class accepts defines the API that interfaces
           have to work with.  A UserInterface does not get free reign over
           everything in the anaconda class, as that would be a big mess.
           Instead, a UserInterface may count on the following:

           storage      -- An instance of storage.Storage.  This is useful for
                           determining what storage devices are present and how
                           they are configured.
           payload      -- An instance of a packaging.Payload subclass.  This
                           is useful for displaying and selecting packages to
                           install, and in carrying out the actual installation.
           instclass    -- An instance of a BaseInstallClass subclass.  This
                           is useful for determining distribution-specific
                           installation information like default package
                           selections and default partitioning.
        """
        if self.__class__ is UserInterface:
            raise TypeError("UserInterface is an abstract class.")

        self.storage = storage
        self.payload = payload
        self.instclass = instclass

        # Register this interface with the top-level ErrorHandler.
        from pyanaconda.errors import errorHandler
        errorHandler.ui = self


    basepath = os.path.dirname(__file__)
    basemask = "pyanaconda.ui"
    paths = PathDict({})

    @property
    def tty_num(self):
        """Returns the number of tty the UserInterface is running on."""
        raise NotImplementedError

    @classmethod
    def update_paths(cls, pathdict):
        """Receives pathdict and appends it's contents to the current
           class defined search path dictionary."""
        for k,v in pathdict.iteritems():
            cls.paths.setdefault(k, [])
            cls.paths[k].extend(v)

    def setup(self, data):
        """Construct all the objects required to implement this interface.
           This method must be provided by all subclasses.
        """
        raise NotImplementedError

    def run(self):
        """Run the interface.  This should do little more than just pass
           through to something else's run method, but is provided here in
           case more is needed.  This method must be provided by all subclasses.
        """
        raise NotImplementedError

    @property
    def meh_interface(self):
        """
        Returns an interface for exception handling (defined by python-meh's
        AbstractIntf class).

        """
        raise NotImplementedError

    ###
    ### MESSAGE HANDLING METHODS
    ###
    def showError(self, message):
        """Display an error dialog with the given message. There is no return
           value. This method must be implemented by all UserInterface
           subclasses.

           In the code, this method should be used sparingly and only for
           critical errors that anaconda cannot figure out how to recover from.
        """
        raise NotImplementedError

    def showYesNoQuestion(self, message):
        """Display a dialog with the given message that presents the user a yes
           or no choice.  This method returns True if the yes choice is selected,
           and False if the no choice is selected.  From here, anaconda can
           figure out what to do next.  This method must be implemented by all
           UserInterface subclasses.

           In the code, this method should be used sparingly and only for those
           times where anaconda cannot make a reasonable decision.  We don't
           want to overwhelm the user with choices.
        """
        raise NotImplementedError

    def _collectActionClasses(self, module_pattern_w_path, standalone_class):
        """Collect all the Hub and Spoke classes which should be enqueued for
           processing.

           :param module_pattern_w_path: the full name patterns (pyanaconda.ui.gui.spokes.%s)
                                         and directory paths to modules we are about to import
           :type module_pattern_w_path: list of (string, string)

           :param standalone_class: the parent type of Spokes we want to pick up
           :type standalone_class: common.StandaloneSpoke based types

           :return: list of Spoke classes with standalone_class as a parent
           :rtype: list of Spoke classes

        """
        standalones = []

        for module_pattern, path in module_pattern_w_path:
            standalones.extend(collect(module_pattern, path, lambda obj: issubclass(obj, standalone_class) and \
                                       getattr(obj, "preForHub", False) or getattr(obj, "postForHub", False)))

        return standalones

    def _orderActionClasses(self, spokes, hubs):
        """Order all the Hub and Spoke classes which should be enqueued for
           processing according to their pre/post dependencies.

           :param spokes: the classes we are to about order according
                          to the hub dependencies
           :type spokes: list of Spoke instances

           :param hubs: the list of Hub classes we check to be in pre/postForHub
                        attribute of Spokes to pick up
           :type hubs: common.Hub based types
        """

        actionClasses = []
        for hub in hubs:
            actionClasses.extend(sorted(filter(lambda obj: getattr(obj, "preForHub", None) == hub, spokes),
                                        key=lambda obj: obj.priority))
            actionClasses.append(hub)
            actionClasses.extend(sorted(filter(lambda obj: getattr(obj, "postForHub", None) == hub, spokes),
                                        key=lambda obj: obj.priority))

        return actionClasses