Package actions :: Module ModeController
[hide private]
[frames] | no frames]

Source Code for Module actions.ModeController

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import sys 
 17   
 18  try: 
 19      PY3 = sys.version_info.major >= 3 
 20      raw_input = input 
 21  except AttributeError: 
 22      PY3 = False 
 23   
 24   
25 -class ModeMissingException(Exception):
26 pass
27 28 29 #Handles operations on a group of Modes.
30 -class ModeController:
31 - def __init__(self, force=None):
32 self.mode_list = {} #Indexed on the name of the mode 33 self.force = False
34 35 #Enable the mode.
36 - def on(self, mode_name):
37 if mode_name in self.mode_list: 38 self.mode_list[mode_name].on() 39 else: 40 raise ModeMissingException()
41 42 #Disable the mode
43 - def off(self, mode_name):
44 if mode_name in self.mode_list: 45 self.mode_list[mode_name].off() 46 else: 47 raise ModeMissingException()
48 49 #Turn on all of the modes.
50 - def all_on(self):
51 for m in self.mode_list.keys(): 52 self.mode_list[m].on() 53 54 if self.mode_list['all'].is_on(): 55 self.mode_list['all'].off()
56 57 #Turn off all of the modes.
58 - def all_off(self):
59 for m in self.mode_list.keys(): 60 self.mode_list[m].off()
61 62 #Check to see if the mode is on.
63 - def is_on(self, mode_name):
64 if mode_name in self.mode_list: 65 return self.mode_list[mode_name].is_on() 66 else: 67 return 0
68 69 #Check to see if the mode is off.
70 - def is_off(self, mode_name):
71 if mode_name in self.mode_list: 72 return self.mode_list[mode_name].is_off() 73 else: 74 return 0
75 76 #Add another mode to the batch.
77 - def add_mode(self, mode_obj):
78 mode_name = mode_obj.get_name() 79 80 if not mode_name in self.mode_list: 81 self.mode_list[mode_name] = mode_obj
82 83 #Remove a mode from the batch.
84 - def del_mode(self, mode_obj):
85 mode_name = mode_obj.get_name() 86 if mode_name in self.mode_list: 87 del self.mode_list[mode_name]
88 89 #set the value of force
90 - def set_force(self, force):
91 self.force = force
92
93 -class ConfigFilesModeController(ModeController):
94 - def __init__(self):
96
97 - def is_on(self, mode_name):
98 if ModeController.is_on(self, 'all'): 99 return 1 100 else: 101 return ModeController.is_on(self, mode_name)
102
103 - def is_off(self, mode_name):
104 if ModeController.is_off(self, 'all'): 105 return 1 106 else: 107 return ModeController.is_off(self, mode_name)
108 109 #the possible presence of the 'all' file confuses things a little. 110 #If the user enables something while the 'all' file is present, nothing should be added to the configfiles dir.
111 - def on(self, mode_name):
112 if mode_name != 'all': 113 if self.is_off('all'): 114 ModeController.on(self, mode_name) 115 116 #Go through each of the modes and see if they're on. If they're all on, then place the 'all' file in there. 117 all_modes_on = 1 118 for m in self.mode_list.keys(): 119 if m != 'all' and self.mode_list[m].is_off(): 120 all_modes_on = 0 121 if all_modes_on: 122 self.all_on() 123 else: 124 self.all_on()
125 126 #If the 'all' file is present and the user disables a mode, then the all file must be removed, and all modes other than 127 #the specified mode need to be turned on.
128 - def off(self, mode_name):
129 if mode_name != 'all': 130 if self.is_on('all'): 131 if not self.force: 132 ask_before_continuing("All modes are currently enabled. Continue? (y or n):") 133 134 #Turn off all modes 135 self.all_off() 136 137 #Manually flip on all of the modes except 'all'. 138 for m in self.mode_list.keys(): 139 if m != 'all': 140 self.mode_list[m].on() 141 142 #Turn off the specified mode. Calls off() at the Mode level, not at the Controller level. 143 self.mode_list[mode_name].off() 144 else: 145 self.all_off()
146 147 148 #This is a little different when the 'all' file is used. 149 #There shouldn't be any other files in the directory when 'all' is used.
150 - def all_on(self):
151 #Get rid of all of the files. 152 self.all_off() 153 #Turn on the 'all' mode. 154 self.mode_list['all'].on()
155
156 -def ask_before_continuing(question=None):
157 if question is None: 158 the_question = "Continue? (y or n):" 159 else: 160 the_question = question 161 162 answer = '-1' 163 164 while answer != 'y' and answer != 'n': 165 answer = raw_input(the_question) 166 answer = answer.lower()[0] 167 168 if answer == 'n': 169 sys.exit(0)
170