1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
42 return "(Config %s.%s): %s" % (self.section, self.option, self.msg)
43
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],
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
61 self.add_section("Main")
62 for key, value in ClushConfig.main_defaults.iteritems():
63 self.set("Main", key, value)
64
65 if filename:
66 files = [filename]
67 else:
68 files = config_paths('clush.conf')
69
70 files.insert(1, expanduser('~/.clush.conf'))
71 self.read(files)
72
73
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
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
114 """Return a boolean value for the named option."""
115 return self._getx('boolean', section, option)
116
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
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
135 """verbosity value as an integer"""
136 try:
137 return self.getint("Main", "verbosity")
138 except ClushConfigError:
139 return 0
140
141 @property
143 """fanout value as an integer"""
144 return self.getint("Main", "fanout")
145
146 @property
148 """connect_timeout value as a float"""
149 return self.getfloat("Main", "connect_timeout")
150
151 @property
153 """command_timeout value as a float"""
154 return self.getfloat("Main", "command_timeout")
155
156 @property
158 """ssh_user value as a string (optional)"""
159 return self._get_optional("Main", "ssh_user")
160
161 @property
163 """ssh_path value as a string (optional)"""
164 return self._get_optional("Main", "ssh_path")
165
166 @property
168 """ssh_options value as a string (optional)"""
169 return self._get_optional("Main", "ssh_options")
170
171 @property
173 """scp_path value as a string (optional)"""
174 return self._get_optional("Main", "scp_path")
175
176 @property
178 """scp_options value as a string (optional)"""
179 return self._get_optional("Main", "scp_options")
180
181 @property
183 """rsh_path value as a string (optional)"""
184 return self._get_optional("Main", "rsh_path")
185
186 @property
188 """rcp_path value as a string (optional)"""
189 return self._get_optional("Main", "rcp_path")
190
191 @property
193 """rsh_options value as a string (optional)"""
194 return self._get_optional("Main", "rsh_options")
195
196 @property
204
205 @property
207 """node_count value as a boolean"""
208 return self.getboolean("Main", "node_count")
209
210 @property
212 """max number of open files (soft rlimit)"""
213 return self.getint("Main", "fd_max")
214