/
ConfigManager.py
274 lines (211 loc) · 10.5 KB
/
ConfigManager.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""Configuration handler to go from default -> config files -> environmental variables -> command line options.
Adds an option to dump the current loaded config as a config file, and
the ability to expand environmental variables in help msgs. Options
that come from a config file MUST be in a config group.
Extends/depends on os, ConfigParser, and optparse.
Advanced functionality of ConfigParser and optparse HAS NOT BEEN TESTED.
Usage example:
from ConfigManager import ConfigManager, ConfigGroup
appname = 'cfgmgr' # could use basename, I suppose
cfgmgr = ConfigManager()
cfgmgr.set_config_files(['/etc/%s/%s.conf' % (appname, appname),
os.path.expanduser('~/.%s.cfg' % appname)]
daemonGroup = ConfigGroup(cfgmgr, "Daemon", "Settings for running as a daemon.")
daemonGroup.add_option('-p', '--pidfile', envvar="CFGMGR_PIDFILE",
help="The location to use for the pid file.",
default="/var/run/%s.pid" % (appname,))
cfgmgr.add_option_group(daemonGroup)
print cfgmgr.parse_args()
"""
__version__ = '0.9.0'
__all__ = ['ConfigManager', 'ConfigGroup', 'ConfigOption']
__copyright__ = """
Copyright (c) 2010 Jason A. Whitlark. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from optparse import OptionParser, OptionGroup, Option, IndentedHelpFormatter
from ConfigParser import SafeConfigParser
import os
class ExpandedIndentedHelpFormatter(IndentedHelpFormatter):
"Add expansion of Environmental variables in help string."
def __init__(self, *args, **kwargs):
IndentedHelpFormatter.__init__(self, *args, **kwargs)
self.envvar_tag = "%envvar"
def expand_default(self, option):
if hasattr(option, 'envvar') and hasattr(option, 'help'):
option.help = option.help.replace(str(self.envvar_tag), "$" + str(option.envvar))
return IndentedHelpFormatter.expand_default(self, option)
class ConfigOption(Option):
ATTRS = Option.ATTRS
ATTRS.append('envvar')
ATTRS.append('group_name')
ATTRS.append('parent_parser')
ACTIONS = Option.ACTIONS + ('generate_config',)
def __init__(self, *opts, **attrs):
Option.__init__(self, *opts, **attrs)
self._look_for_config_value()
self._look_for_env_var()
def _look_for_env_var(self):
"Replace default with the environmental variable, if it exists."
if hasattr(self, 'envvar'):
val = os.getenv(self.envvar)
if val:
self.default = val
delattr(self, 'envvar')
def _look_for_config_value(self):
if hasattr(self, 'group_name') and hasattr(self, 'parent_parser'):
if self.parent_parser:
config_val = self.parent_parser._get_config_value(self.group_name,
self.dest)
if config_val:
self.default = config_val
def _lookup_default_from_config_file(self, parser, grp, opt):
return parser.find_config_value_from_file(grp.title, opt.dest)
def take_action(self, action, dest, opt, value, values, parser):
"Extended to handle generation a config file"
if action == "generate_config":
parser.generate_and_print_config_file()
parser.exit()
Option.take_action(self, action, dest, opt, value, values, parser)
class ConfigGroup(OptionGroup):
# Add information the option needs to look up its config.
def add_option(self, *args, **kwargs):
kwargs['group_name'] = self.title
kwargs['parent_parser'] = self.parser
option = OptionGroup.add_option(self, *args, **kwargs)
return option
class ConfigManager(OptionParser):
def __init__(self,
usage=None,
option_list=None,
option_class=ConfigOption,
version=None,
conflict_handler="error",
description=None,
formatter=None,
add_help_option=True,
prog=None,
epilog=None):
if formatter is None:
formatter = ExpandedIndentedHelpFormatter()
OptionParser.__init__(self,
usage,
option_list,
option_class,
version,
conflict_handler,
description,
formatter,
add_help_option,
prog,
epilog)
def _add_generate_config_file_option(self):
self.add_option("--generate-config-file",
action='generate_config',
help=("Generate a config file on standard out."))
def generate_and_print_config_file(self):
"""Generate a config file.
"""
if hasattr(self, '_curr_config_files'):
print "# Current config files searched:"
for f in self._curr_config_files:
print "# %s" % f
print
for grp in self.option_groups:
print "[%s]" % grp.title
desc = grp.get_description()
if desc:
print "## %s\n" % desc
for opt in grp.option_list:
self._print_opt_help_msg(opt)
self._print_opt_config_pair(opt)
print # Empty line, to separate groups.
def _print_opt_help_msg(self, opt):
help_msg = None
if hasattr(opt, 'help'):
help_msg = "## %s" % opt.help
if hasattr(opt, 'default'):
help_msg = help_msg.replace('%default', str(opt.default))
if hasattr(opt, 'envvar'):
help_msg = help_msg.replace('%envvar', str(opt.envvar))
if help_msg:
print help_msg
def _print_opt_config_pair(self, opt):
print "%s = %s" % (opt.dest, opt.default)
def _populate_option_list(self, option_list, add_help=True):
OptionParser._populate_option_list(self, option_list, add_help)
self._add_generate_config_file_option()
def read_config_files(self, file_or_files):
self._curr_config_files = file_or_files
self.config_file_vars = SafeConfigParser()
self.config_file_vars.read(file_or_files)
def _get_config_value(self, section, option):
if self.config_file_vars.has_section(section) and \
self.config_file_vars.has_option(section, option):
return self.config_file_vars.get(section, option)
else:
return None
if __name__ == '__main__':
appname = 'agent-example'
cfgmgr = ConfigManager()
cfgmgr.read_config_files(['/etc/%s/agent.conf' % (appname,),
os.path.expanduser('~/.%s.cfg' % appname)])
daemonGroup = ConfigGroup(cfgmgr, "Daemon", "Settings for running as a daemon.")
daemonGroup.add_option('-p', '--pidfile', envvar="CFGMGR_PIDFILE",
help="The location to use for the pid file. [environment: %envvar, default: %default]",
default="/var/run/%s.pid" % (appname,))
daemonGroup.add_option( '--stdin',
help="Stdin file descriptor for the daemon. [default: %default]",
default="/dev/null")
daemonGroup.add_option( '--stdout',
help="Stdout file descriptor for the daemon. [default: %default]",
default="/dev/null")
daemonGroup.add_option( '--stderr',
help="Stderr file descriptor for the daemon. [default: %default]",
default="/dev/null")
logGroup = ConfigGroup(cfgmgr, "Log", "Log settings.")
logGroup.add_option('-l', '--logfile', envvar="CFGMGR_LOGFILE",
help="The location to use for the log file. [environment: %envvar, default: %default]",
default="/var/log/%s.log" % (appname,))
logGroup.add_option( '--loglevel',
help="Log level for the daemon. [default: %default]",
default="warn")
stompGroup = ConfigGroup(cfgmgr, "Stomp", "Stomp connection settings.")
stompGroup.add_option( '--server',
help="Address to use to connect to the Stomp server.[default: %default]",
default="localhost")
stompGroup.add_option( '--port',
help="Port to use to connect to the Stomp server.[default: %default]",
default="61613")
stompGroup.add_option( '--user',
help="Username to use to connect to the Stomp server.Log level for the daemon. [default: %default]",
default="")
stompGroup.add_option( '--password',
help="Password to use to connect to the Stomp server. [default: %default]",
default="")
cfgmgr.add_option_group(daemonGroup)
cfgmgr.add_option_group(logGroup)
cfgmgr.add_option_group(stompGroup)
print cfgmgr.parse_args()
print "Done."