Package ClusterShell :: Package CLI :: Module Config
[hide private]
[frames] | no frames]

Source Code for Module ClusterShell.CLI.Config

  1  #!/usr/bin/env python 
  2  # 
  3  # Copyright (C) 2010-2016 CEA/DAM 
  4  # 
  5  # This file is part of ClusterShell. 
  6  # 
  7  # ClusterShell is free software; you can redistribute it and/or 
  8  # modify it under the terms of the GNU Lesser General Public 
  9  # License as published by the Free Software Foundation; either 
 10  # version 2.1 of the License, or (at your option) any later version. 
 11  # 
 12  # ClusterShell is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 15  # Lesser General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU Lesser General Public 
 18  # License along with ClusterShell; if not, write to the Free Software 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 20   
 21  """ 
 22  CLI configuration classes 
 23  """ 
 24   
 25  import ConfigParser 
 26  from os.path import expanduser 
 27   
 28  from ClusterShell.Defaults import config_paths, DEFAULTS 
 29  from ClusterShell.CLI.Display import VERB_QUIET, VERB_STD, \ 
 30      VERB_VERB, VERB_DEBUG, THREE_CHOICES 
31 32 33 -class ClushConfigError(Exception):
34 """Exception used by ClushConfig to report an error."""
35 - def __init__(self, section, option, msg):
36 Exception.__init__(self) 37 self.section = section 38 self.option = option 39 self.msg = msg
40
41 - def __str__(self):
42 return "(Config %s.%s): %s" % (self.section, self.option, self.msg)
43
44 -class ClushConfig(ConfigParser.ConfigParser, object):
45 """Config class for clush (specialized ConfigParser)""" 46 47 main_defaults = {"fanout": "%d" % DEFAULTS.fanout, 48 "connect_timeout": "%f" % DEFAULTS.connect_timeout, 49 "command_timeout": "%f" % DEFAULTS.command_timeout, 50 "history_size": "100", 51 "color": THREE_CHOICES[-1], # auto 52 "verbosity": "%d" % VERB_STD, 53 "node_count": "yes", 54 "fd_max": "8192"} 55
56 - def __init__(self, options, filename=None):
57 """Initialize ClushConfig object from corresponding 58 OptionParser options.""" 59 ConfigParser.ConfigParser.__init__(self) 60 # create Main section with default values 61 self.add_section("Main") 62 for key, value in ClushConfig.main_defaults.iteritems(): 63 self.set("Main", key, value) 64 # config files override defaults values 65 if filename: 66 files = [filename] 67 else: 68 files = config_paths('clush.conf') 69 # deprecated user config, kept in 1.x for 1.6 compat 70 files.insert(1, expanduser('~/.clush.conf')) 71 self.read(files) 72 73 # Apply command line overrides 74 if options.quiet: 75 self._set_main("verbosity", VERB_QUIET) 76 if options.verbose: 77 self._set_main("verbosity", VERB_VERB) 78 if options.debug: 79 self._set_main("verbosity", VERB_DEBUG) 80 if options.fanout: 81 self._set_main("fanout", options.fanout) 82 if options.user: 83 self._set_main("ssh_user", options.user) 84 if options.options: 85 self._set_main("ssh_options", options.options) 86 if options.connect_timeout: 87 self._set_main("connect_timeout", options.connect_timeout) 88 if options.command_timeout: 89 self._set_main("command_timeout", options.command_timeout) 90 if options.whencolor: 91 self._set_main("color", options.whencolor) 92 93 try: 94 # -O/--option KEY=VALUE 95 for cfgopt in options.option: 96 optkey, optvalue = cfgopt.split('=', 1) 97 self._set_main(optkey, optvalue) 98 except ValueError, exc: 99 raise ClushConfigError("Main", cfgopt, "invalid -O/--option value")
100
101 - def _set_main(self, option, value):
102 """Set given option/value pair in the Main section.""" 103 self.set("Main", option, str(value))
104
105 - def _getx(self, xtype, section, option):
106 """Return a value of specified type for the named option.""" 107 try: 108 return getattr(ConfigParser.ConfigParser, 'get%s' % xtype)(self, \ 109 section, option) 110 except (ConfigParser.Error, TypeError, ValueError), exc: 111 raise ClushConfigError(section, option, exc)
112
113 - def getboolean(self, section, option):
114 """Return a boolean value for the named option.""" 115 return self._getx('boolean', section, option)
116
117 - def getfloat(self, section, option):
118 """Return a float value for the named option.""" 119 return self._getx('float', section, option)
120
121 - def getint(self, section, option):
122 """Return an integer value for the named option.""" 123 return self._getx('int', section, option)
124
125 - def _get_optional(self, section, option):
126 """Utility method to get a value for the named option, but do 127 not raise an exception if the option doesn't exist.""" 128 try: 129 return self.get(section, option) 130 except ConfigParser.Error: 131 pass
132 133 @property
134 - def verbosity(self):
135 """verbosity value as an integer""" 136 try: 137 return self.getint("Main", "verbosity") 138 except ClushConfigError: 139 return 0
140 141 @property
142 - def fanout(self):
143 """fanout value as an integer""" 144 return self.getint("Main", "fanout")
145 146 @property
147 - def connect_timeout(self):
148 """connect_timeout value as a float""" 149 return self.getfloat("Main", "connect_timeout")
150 151 @property
152 - def command_timeout(self):
153 """command_timeout value as a float""" 154 return self.getfloat("Main", "command_timeout")
155 156 @property
157 - def ssh_user(self):
158 """ssh_user value as a string (optional)""" 159 return self._get_optional("Main", "ssh_user")
160 161 @property
162 - def ssh_path(self):
163 """ssh_path value as a string (optional)""" 164 return self._get_optional("Main", "ssh_path")
165 166 @property
167 - def ssh_options(self):
168 """ssh_options value as a string (optional)""" 169 return self._get_optional("Main", "ssh_options")
170 171 @property
172 - def scp_path(self):
173 """scp_path value as a string (optional)""" 174 return self._get_optional("Main", "scp_path")
175 176 @property
177 - def scp_options(self):
178 """scp_options value as a string (optional)""" 179 return self._get_optional("Main", "scp_options")
180 181 @property
182 - def rsh_path(self):
183 """rsh_path value as a string (optional)""" 184 return self._get_optional("Main", "rsh_path")
185 186 @property
187 - def rcp_path(self):
188 """rcp_path value as a string (optional)""" 189 return self._get_optional("Main", "rcp_path")
190 191 @property
192 - def rsh_options(self):
193 """rsh_options value as a string (optional)""" 194 return self._get_optional("Main", "rsh_options")
195 196 @property
197 - def color(self):
198 """color value as a string in (never, always, auto)""" 199 whencolor = self._get_optional("Main", "color") 200 if whencolor not in THREE_CHOICES: 201 raise ClushConfigError("Main", "color", "choose from %s" % \ 202 THREE_CHOICES) 203 return whencolor
204 205 @property
206 - def node_count(self):
207 """node_count value as a boolean""" 208 return self.getboolean("Main", "node_count")
209 210 @property
211 - def fd_max(self):
212 """max number of open files (soft rlimit)""" 213 return self.getint("Main", "fd_max")
214