annotate configoptionparser/__init__.py @ 1:3148ccbd82de default tip

give a version
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 20 May 2010 08:47:49 -0700
parents 3081763b099b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 from optparse import OptionParser
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 from ConfigParser import ConfigParser
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 ### duplicate/extend ConfigParser functions as we can't use them directly :(
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 def getlist(string, separator=','):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """returns a list from a string given a separator"""
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 string = string.strip()
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 if not string:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 return []
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 return [i.strip() for i in string.split(separator)]
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def getboolean(string):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 return string.lower() == 'true'
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 class Undefined(object):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 def __init__(self, default):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 self.default=default
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 class ConfigOptionParser(OptionParser):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 def __init__(self, defaults_section='DEFAULTS', dict_section=None,
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 variables=None, **kwargs):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 """
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 - defaults_section: section of .ini to look for configuration variables
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 - dict_section: section of .ini to return as a dictionary
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 - variables: attr on returned options to parse dictionary from command line
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 self.defaults_section = defaults_section
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 self.dict_section = dict_section
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 self.variables = variables
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if self.dict_section and not self.variables:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 self.variables = dict_section
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 OptionParser.__init__(self, **kwargs)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 OptionParser.add_option(self,
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 '-c', '--config', dest='config', action='append',
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 help='ini file to read from')
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 def add_option(self, *args, **kwargs):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 kwargs['default'] = Undefined(kwargs.get('default'))
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 OptionParser.add_option(self, *args, **kwargs)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 def parse_args(self, args=None, values=None):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 options, args = OptionParser.parse_args(self, args, values)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 # get defaults from the configuration parser
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 defaults = {}
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 config = ConfigParser()
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 if options.config:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 config.read(options.config)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 if self.defaults_section in config.sections():
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 defaults = dict(config.items(self.defaults_section, raw=True))
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 # option dict
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 option_dict = dict([(i.dest, i) for i in self.option_list
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 if i.dest not in ('config', 'help')])
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 # conversion functions for .ini data
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 conversions = { 'store_true': getboolean,
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 'store_false': getboolean,
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 'append': getlist }
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 # fill in the defaults not set from the command line
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 for key, value in options.__dict__.items():
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 # don't override command line arguments! they win!
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 if isinstance(value, Undefined):
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 if key in defaults and key in option_dict:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 # fill in options from .ini files
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 option = option_dict[key]
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 # converstion function
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 function = conversions.get(option.action, lambda x: x)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 setattr(options, key, function(defaults[key]))
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 else:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 # set from option defaults
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 setattr(options, key, value.default)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 # get variables from dict_section and command line arguments
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 # TODO: could do this first then interpolate the config file from these
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 variables = {}
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 if self.dict_section in config.sections():
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 variables.update(dict(config.items(self.dict_section, raw=True)))
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 if self.variables:
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 variables.update(dict([i.split('=',1) for i in args if '=' in i]))
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 args = [i for i in args if '=' not in i]
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 setattr(options, self.variables, variables)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 return (options, args)
3081763b099b initial commit of ConfigOptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93