Mercurial > hg > configuration
annotate configuration/configuration.py @ 108:a2184db43fe2
fix dict command line processing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 03 May 2012 15:46:13 -0700 |
parents | 83d66a9bdef0 |
children | 09642528be02 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
66 | 4 unified configuration with serialization/deserialization |
0 | 5 """ |
6 | |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
7 import copy |
37
a1f8dec4d4f9
we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
8 import os |
0 | 9 import sys |
10 import optparse | |
11 | |
67 | 12 # imports for configuration providers |
4 | 13 try: |
14 import json | |
15 except ImportError: | |
16 try: | |
17 import simplejson as json | |
18 except ImportError: | |
19 json = None | |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
20 try: |
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
21 import yaml |
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
22 except ImportError: |
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
23 yaml = None |
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
24 |
67 | 25 __all__ = ['Configuration', |
26 'configuration_providers', | |
27 'types', | |
28 'UnknownOptionException', | |
29 'MissingValueException', | |
30 'ConfigurationProviderException', | |
31 'TypeCastException', | |
32 'ConfigurationOption'] | |
36
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
33 |
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
34 ### exceptions |
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
35 |
67 | 36 class UnknownOptionException(Exception): |
37 """exception raised when a non-configuration value is present in the configuration""" | |
38 | |
36
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
39 class MissingValueException(Exception): |
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
40 """exception raised when a required value is missing""" |
6 | 41 |
41
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
42 class ConfigurationProviderException(Exception): |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
43 """exception raised when a configuration provider is missing, etc""" |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
44 |
46 | 45 class TypeCastException(Exception): |
46 """exception raised when a configuration item cannot be coerced to a type""" | |
47 | |
67 | 48 |
26
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
49 ### configuration providers for serialization/deserialization |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
50 |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
51 configuration_providers = [] |
26
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
52 |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
53 class ConfigurationProvider(object): |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
54 """ |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
55 abstract base class for configuration providers for |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
56 serialization/deserialization |
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
57 """ |
27 | 58 def read(self, filename): |
59 raise NotImplementedError("Abstract base class") | |
60 | |
61 def write(self, config, filename): | |
62 if isinstance(filename, basestring): | |
63 f = file(filename, 'w') | |
64 newfile = True | |
65 else: | |
66 f = filename | |
67 newfile = False | |
68 try: | |
69 self._write(f, config) | |
70 finally: | |
71 # XXX try: finally: works in python >= 2.5 | |
72 if newfile: | |
73 f.close() | |
74 def _write(self, fp, config): | |
75 raise NotImplementedError("Abstract base class") | |
26
4fd88b1b08d5
ABC-ing configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
76 |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
77 if json: |
38
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
78 class JSON(ConfigurationProvider): |
27 | 79 indent = 2 |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
80 extensions = ['json'] |
7 | 81 def read(self, filename): |
9 | 82 return json.loads(file(filename).read()) |
27 | 83 def _write(self, fp, config): |
38
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
84 fp.write(json.dumps(config, indent=self.indent, sort_keys=True)) |
27 | 85 # TODO: could use templates to get order down, etc |
30
b27a7cb2dd5b
stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
86 configuration_providers.append(JSON()) |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
87 |
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
88 if yaml: |
38
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
89 class YAML(ConfigurationProvider): |
30
b27a7cb2dd5b
stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
90 extensions = ['yml', 'yaml'] |
87
928654373755
change default flow style for yaml, http://pyyaml.org/wiki/PyYAMLDocumentation
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
91 dump_args = {'default_flow_style': False} |
5
7910b0ef0bab
stub configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 def read(self, filename): |
6 | 93 f = file(filename) |
94 config = yaml.load(f) | |
95 f.close() | |
96 return config | |
27 | 97 def _write(self, fp, config): |
87
928654373755
change default flow style for yaml, http://pyyaml.org/wiki/PyYAMLDocumentation
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
98 fp.write(yaml.dump(config, **self.dump_args)) |
27 | 99 # TODO: could use templates to get order down, etc |
6 | 100 |
30
b27a7cb2dd5b
stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
101 configuration_providers.append(YAML()) |
b27a7cb2dd5b
stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
102 |
50
113e8c2f7cab
start to hook up deserializers
Jeff Hammel <jhammel@mozilla.com>
parents:
49
diff
changeset
|
103 # TODO: add a configuration provider for taking command-line arguments |
113e8c2f7cab
start to hook up deserializers
Jeff Hammel <jhammel@mozilla.com>
parents:
49
diff
changeset
|
104 # from a file |
113e8c2f7cab
start to hook up deserializers
Jeff Hammel <jhammel@mozilla.com>
parents:
49
diff
changeset
|
105 |
30
b27a7cb2dd5b
stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
106 __all__.extend([i.__class__.__name__ for i in configuration_providers]) |
4 | 107 |
44
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
108 ### optparse interface |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
109 |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
110 class ConfigurationOption(optparse.Option): |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
111 """option that keeps track if it is seen""" |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
112 # TODO: this should be configurable or something |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
113 def take_action(self, action, dest, opt, value, values, parser): |
108
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
114 |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
115 # switch on types |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
116 formatter = getattr(parser, 'cli_formatter') |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
117 if formatter: |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
118 formatter = formatter(dest) |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
119 if formatter: |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
120 value = formatter(value) |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
121 |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
122 # call the optparse front-end |
44
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
123 optparse.Option.take_action(self, action, dest, opt, value, values, parser) |
82 | 124 |
125 # add the parsed option to the set of things parsed | |
44
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
126 if not hasattr(parser, 'parsed'): |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
127 parser.parsed = set() |
b3db38b14567
stub adding an option of keeping track of what is parsed
Jeff Hammel <jhammel@mozilla.com>
parents:
43
diff
changeset
|
128 parser.parsed.add(dest) |
82 | 129 |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
130 ### plugins for option types |
80
35416ad14098
notes to self about how to make this better; still needs a little thought
Jeff Hammel <jhammel@mozilla.com>
parents:
78
diff
changeset
|
131 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
132 class BaseCLI(object): |
80
35416ad14098
notes to self about how to make this better; still needs a little thought
Jeff Hammel <jhammel@mozilla.com>
parents:
78
diff
changeset
|
133 """base_cli for all option types""" |
35416ad14098
notes to self about how to make this better; still needs a little thought
Jeff Hammel <jhammel@mozilla.com>
parents:
78
diff
changeset
|
134 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
135 def __call__(self, name, value): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
136 """return args, kwargs needed to instantiate an optparse option""" |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
137 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
138 args = value.get('flags', ['--%s' % name]) |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
139 if not args: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
140 # No CLI interface |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
141 return (), {} |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
142 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
143 kw = {'dest': name} |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
144 help = value.get('help', name) |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
145 if 'default' in value: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
146 kw['default'] = value['default'] |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
147 help += ' [DEFAULT: %s]' % value['default'] |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
148 kw['help'] = help |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
149 kw['action'] = 'store' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
150 return args, kw |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
151 |
82 | 152 def take_action(self, value): |
153 return value | |
154 | |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
155 class BoolCLI(BaseCLI): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
156 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
157 def __call__(self, name, value): |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
158 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
159 # preserve the default values |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
160 help = value.get('help') |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
161 flags = value.get('flags') |
23 | 162 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
163 args, kw = BaseCLI.__call__(self, name, value) |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
164 kw['help'] = help # reset |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
165 if value.get('default'): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
166 kw['action'] = 'store_false' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
167 if not flags: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
168 args = ['--no-%s' % name] |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
169 if not help: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
170 kw['help'] = 'disable %s' % name |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
171 else: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
172 kw['action'] = 'store_true' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
173 if not help: |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
174 kw['help'] = 'enable %s' % name |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
175 return args, kw |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
176 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
177 class ListCLI(BaseCLI): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
178 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
179 def __call__(self, name, value): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
180 args, kw = BaseCLI.__call__(self, name, value) |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
181 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
182 # TODO: could use 'extend' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
183 # - http://hg.mozilla.org/build/mozharness/file/5f44ba08f4be/mozharness/base/config.py#l41 |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
184 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
185 # TODO: what about nested types? |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
186 kw['action'] = 'append' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
187 return args, kw |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
188 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
189 class IntCLI(BaseCLI): |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
190 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
191 def __call__(self, name, value): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
192 args, kw = BaseCLI.__call__(self, name, value) |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
193 kw['type'] = 'int' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
194 return args, kw |
23 | 195 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
196 class FloatCLI(BaseCLI): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
197 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
198 def __call__(self, name, value): |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
199 args, kw = BaseCLI.__call__(self, name, value) |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
200 kw['type'] = 'float' |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
201 return args, kw |
23 | 202 |
82 | 203 class DictCLI(ListCLI): |
204 | |
205 delimeter = '=' | |
206 | |
108
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
207 def __call__(self, name, value): |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
208 |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
209 # optparse can't handle dict types OOTB |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
210 default = value.get('default') |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
211 if isinstance(default, dict): |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
212 value = copy.deepcopy(value) |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
213 value['default'] = default.items() |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
214 |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
215 return ListCLI.__call__(self, name, value) |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
216 |
82 | 217 def take_action(self, value): |
108
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
218 if self.delimeter not in value: |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
219 raise AssertionError("Each value must be delimited by '%s': %s" % (self.delimeter, value)) |
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
220 return value.split(self.delimeter, 1) |
82 | 221 |
60 | 222 # TODO: 'dict'-type cli interface |
223 | |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
224 types = {bool: BoolCLI(), |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
225 int: IntCLI(), |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
226 float: FloatCLI(), |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
227 list: ListCLI(), |
82 | 228 dict: DictCLI(), |
88
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
229 str: BaseCLI(), |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
230 None: BaseCLI()} # default |
82 | 231 |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
232 __all__ += [i.__class__.__name__ for i in types.values()] |
4 | 233 |
59
b8f066b8ab62
begin migration path towards inheriting from ConfigParser; ideally, Configuration should live in its own class and the CLI class should inherit from that
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
234 class Configuration(optparse.OptionParser): |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
235 """declarative configuration object""" |
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
236 |
101
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
237 options = {} # configuration basis definition |
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
238 load_option = 'load' # where to put the load option |
102
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
239 extend = set() # if dicts/lists should be extended |
1 | 240 |
75
c119eb07a378
rename/shuffle options to make more sense
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
241 def __init__(self, configuration_providers=configuration_providers, types=types, load=None, dump='--dump', **parser_args): |
61
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
242 |
84
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
243 # sanity check |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
244 if isinstance(self.options, dict): |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
245 self.option_dict = self.options |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
246 elif isinstance(self.options, list): |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
247 # XXX could also be tuple, etc |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
248 self.option_dict = dict(self.options) |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
249 else: |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
250 raise NotImplementedError |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
251 |
61
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
252 # setup configuration |
2 | 253 self.config = {} |
6 | 254 self.configuration_providers = configuration_providers |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
255 self.types = types |
61
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
256 |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
257 # setup optionparser |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
258 if 'description' not in parser_args: |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
259 parser_args['description'] = getattr(self, '__doc__', '') |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
260 if 'formatter' not in parser_args: |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
261 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
262 """description formatter for console script entry point""" |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
263 def format_description(self, description): |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
264 if description: |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
265 return description.strip() + '\n' |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
266 else: |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
267 return '' |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
268 parser_args['formatter'] = PlainDescriptionFormatter() |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
269 parser_args.setdefault('option_class', ConfigurationOption) |
59
b8f066b8ab62
begin migration path towards inheriting from ConfigParser; ideally, Configuration should live in its own class and the CLI class should inherit from that
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
270 optparse.OptionParser.__init__(self, **parser_args) |
61
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
271 self.parsed = set() |
ef2d1c6211b9
more stubbing to make Configuration an OptionParser
Jeff Hammel <jhammel@mozilla.com>
parents:
60
diff
changeset
|
272 self.optparse_options(self) |
63 | 273 # add option(s) for configuration_providers |
75
c119eb07a378
rename/shuffle options to make more sense
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
274 if load: |
c119eb07a378
rename/shuffle options to make more sense
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
275 self.add_option(load, |
101
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
276 dest=self.load_option, action='append', |
75
c119eb07a378
rename/shuffle options to make more sense
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
277 help="load configuration from a file") |
63 | 278 |
279 # add an option for dumping | |
280 formats = self.formats() | |
281 if formats and dump: | |
90 | 282 if isinstance(dump, basestring): |
283 dump = [dump] | |
284 dump = list(dump) | |
107 | 285 self.add_option(*dump, **dict(dest='dump', |
286 help="Output configuration file; Formats: %s" % formats)) | |
63 | 287 |
2 | 288 |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
289 ### methods for iteration |
67 | 290 ### TODO: make the class a real iterator |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
291 |
11
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
292 def items(self): |
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
293 # TODO: allow options to be a list of 2-tuples |
84
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
294 if isinstance(self.options, dict): |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
295 return self.options.items() |
34c13cde0255
move towards dict or items system
Jeff Hammel <jhammel@mozilla.com>
parents:
82
diff
changeset
|
296 return self.options |
11
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
297 |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
298 ### methods for validating configuration |
2 | 299 |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
300 def check(self, config): |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
301 """ |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
302 check validity of configuration to be added |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
303 """ |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
304 |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
305 # ensure options in configuration are in self.options |
86 | 306 unknown_options = [i for i in config if i not in self.option_dict] |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
307 if unknown_options: |
67 | 308 raise UnknownOptionException("Unknown options: %s" % ', '.join(unknown_options)) |
6 | 309 |
67 | 310 # ensure options are of the right type (if specified) |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
311 for key, value in config.items(): |
86 | 312 _type = self.option_dict[key].get('type') |
313 if _type is None and 'default' in self.option_dict[key]: | |
314 _type = type(self.option_dict[key]['default']) | |
67 | 315 if _type is not None and not isinstance(value, _type): |
46 | 316 try: |
317 config[key] = _type(value) | |
318 except BaseException, e: | |
319 raise TypeCastException("Could not coerce %s, %s, to type %s: %s" % (key, value, _type.__name__, e)) | |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
320 |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
321 return config |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
322 |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
323 def validate(self): |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
324 """validate resultant configuration""" |
67 | 325 |
78 | 326 for key, value in self.items(): |
35
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
327 if key not in self.config: |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
328 required = value.get('required') |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
329 if required: |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
330 if isinstance(required, basestring): |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
331 required_message = required |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
332 else: |
321fe58a9eae
flush out validate function
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
333 required_message = "Parameter %s is required but not present" % key |
67 | 334 # TODO: this should probably raise all missing values vs |
37
a1f8dec4d4f9
we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
335 # one by one |
36
d218df4d0b4c
add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents:
35
diff
changeset
|
336 raise MissingValueException(required_message) |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
337 # TODO: configuration should be locked after this is called |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
338 |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
339 ### methods for adding configuration |
6 | 340 |
102
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
341 def default_config(self): |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
342 """configuration defaults""" |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
343 defaults = {} |
103 | 344 for key, value in self.items(): |
102
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
345 if 'default' in value: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
346 defaults[key] = value['default'] |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
347 return copy.deepcopy(defaults) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
348 |
2 | 349 def __call__(self, *args): |
350 """add items to configuration and check it""" | |
67 | 351 |
102
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
352 # start with defaults |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
353 self.config = self.default_config() |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
354 |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
355 # add the configuration |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
356 for config in args: |
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
357 self.add(config) |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
358 |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
359 # validate total configuration |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
360 self.validate() |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
361 # TODO: configuration should be locked after this is called |
2 | 362 |
24
39f2611db9be
rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
363 def add(self, config, check=True): |
2 | 364 """update configuration: not undoable""" |
6 | 365 |
102
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
366 # check config to be added |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
367 self.check(config) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
368 |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
369 # add the configuration |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
370 for key, value in config.items(): |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
371 value = copy.deepcopy(value) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
372 if key in self.extend and key in self.config: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
373 type1 = type(self.config[key]) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
374 type2 = type(value) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
375 assert type1 == type2 # XXX hack |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
376 if type1 == dict: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
377 self.config[key].update(value) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
378 elif type1 == list: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
379 self.config[key].extend(value) |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
380 else: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
381 raise NotImplementedError |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
382 else: |
c530f6265deb
allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
383 self.config[key] = value |
2 | 384 |
18
d8871956536e
remove cruft and start to organize
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
385 ### methods for optparse |
d8871956536e
remove cruft and start to organize
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
386 ### XXX could go in a subclass |
d8871956536e
remove cruft and start to organize
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
387 |
82 | 388 def cli_formatter(self, option): |
99
17001bceec8f
only use formatters for options we actually know about from self.options; the rest should be handled from the client
Jeff Hammel <jhammel@mozilla.com>
parents:
98
diff
changeset
|
389 if option in self.option_dict: |
108
a2184db43fe2
fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
390 handler = self.types[self.option_type(option)] |
99
17001bceec8f
only use formatters for options we actually know about from self.options; the rest should be handled from the client
Jeff Hammel <jhammel@mozilla.com>
parents:
98
diff
changeset
|
391 return getattr(handler, 'take_action', lambda x: x) |
82 | 392 |
88
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
393 def option_type(self, name): |
98
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
394 """get the type of an option named `name`""" |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
395 |
88
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
396 value = self.option_dict[name] |
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
397 if 'type' in value: |
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
398 return value['type'] |
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
399 if 'default' in value: |
98
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
400 default = value['default'] |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
401 if default is None: |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
402 return None |
88
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
403 return type(value['default']) |
36cf1d9bf40b
slightly improve type system
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
404 |
17
a78ab14ae376
separate thing to add options to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
405 def optparse_options(self, parser): |
a78ab14ae376
separate thing to add options to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
406 """add optparse options to a OptionParser instance""" |
11
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
407 for key, value in self.items(): |
98
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
408 try: |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
409 handler = self.types[self.option_type(key)] |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
410 except: |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
411 # XXX print this for now |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
412 # ultimately, if an option can't be coerced to a type |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
413 # we should just not add a CLI handler for it |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
414 print key, value |
91e213025769
handle None type (though its probably a bogus case) and note on future failure upon coercion
Jeff Hammel <jhammel@mozilla.com>
parents:
96
diff
changeset
|
415 raise |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
416 args, kw = handler(key, value) |
11
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
417 if not args: |
21
0fe74db6a56c
a hacky way to do CLI types handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
418 # No CLI interface |
11
e00afe2c83bf
stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
419 continue |
12 | 420 parser.add_option(*args, **kw) |
17
a78ab14ae376
separate thing to add options to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
421 |
63 | 422 def parse_args(self, *args, **kw): |
81
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
423 |
f3b2563b2a91
move to a class-based architecture
Jeff Hammel <jhammel@mozilla.com>
parents:
80
diff
changeset
|
424 self.parsed = set() |
63 | 425 options, args = optparse.OptionParser.parse_args(self, *args, **kw) |
426 | |
427 # get CLI configuration options | |
428 cli_config = dict([(key, value) for key, value in options.__dict__.items() | |
86 | 429 if key in self.option_dict and key in self.parsed]) |
63 | 430 |
431 # deserialize configuration | |
101
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
432 configuration_files = getattr(options, self.load_option, args) |
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
433 if not configuration_files: |
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
434 configuration_files = [] |
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
435 if isinstance(configuration_files, basestring): |
f4590492cb4c
handle load option a bit better
Jeff Hammel <jhammel@mozilla.com>
parents:
100
diff
changeset
|
436 configuration_files = [configuration_files] |
63 | 437 missing = [i for i in configuration_files |
438 if not os.path.exists(i)] | |
439 if missing: | |
64 | 440 self.error("Missing files: %s" % ', '.join(missing)) |
63 | 441 config = [] |
442 for f in configuration_files: | |
443 try: | |
104
9dd0ebcdbc62
be more forgiving of blank configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
103
diff
changeset
|
444 loaded_config = self.deserialize(f) |
9dd0ebcdbc62
be more forgiving of blank configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
103
diff
changeset
|
445 if loaded_config: |
9dd0ebcdbc62
be more forgiving of blank configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
103
diff
changeset
|
446 config.append(loaded_config) |
63 | 447 except BaseException, e: |
448 parser.error(str(e)) | |
449 config.append(cli_config) | |
450 | |
451 missingvalues = None | |
452 try: | |
453 # generate configuration | |
454 self(*config) | |
455 except MissingValueException, missingvalues: | |
89
0804a706d6bf
handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents:
88
diff
changeset
|
456 # errors are handled below |
63 | 457 pass |
458 | |
94
291fbbeb6e1e
actually dump configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
459 # dump configuration |
106
a929a7331069
remove extraneous assignment
Jeff Hammel <jhammel@mozilla.com>
parents:
104
diff
changeset
|
460 self.dump(options, missingvalues) |
63 | 461 |
64 | 462 # update options from config |
463 options.__dict__.update(self.config) | |
464 | |
63 | 465 # return parsed arguments |
466 return options, args | |
467 | |
95
290c40224e5d
improved abstraction; sadly doesnt seem to work
Jeff Hammel <jhammel@mozilla.com>
parents:
94
diff
changeset
|
468 def dump(self, options, missingvalues): |
90 | 469 """dump configuration, if specified""" |
91
1e06fd17b8ad
make dump method configurable
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
470 |
96
bfe4234ee6f4
better ordering of processing for dump()
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
471 if missingvalues: |
bfe4234ee6f4
better ordering of processing for dump()
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
472 self.error(str(missingvalues)) |
bfe4234ee6f4
better ordering of processing for dump()
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
473 |
90 | 474 dump = getattr(options, 'dump') |
475 if dump: | |
476 # TODO: have a way of specifying format other than filename | |
477 self.serialize(dump) | |
478 | |
28
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
479 ### serialization/deserialization |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
480 |
43 | 481 def formats(self): |
482 """formats for deserialization""" | |
483 retval = [] | |
484 for provider in self.configuration_providers: | |
485 if provider.extensions and hasattr(provider, 'write'): | |
486 retval.append(provider.extensions[0]) | |
487 return retval | |
488 | |
28
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
489 def configuration_provider(self, format): |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
490 """configuration provider guess for a given filename""" |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
491 for provider in self.configuration_providers: |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
492 if format in provider.extensions: |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
493 return provider |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
494 |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
495 def filename2format(self, filename): |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
496 extension = os.path.splitext(filename)[-1] |
38
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
497 return extension.lstrip('.') or None |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
498 |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
499 def serialize(self, filename, format=None, full=False): |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
500 """ |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
501 serialize configuration to a file |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
502 - filename: path of file to serialize to |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
503 - format: format of configuration provider |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
504 - full: whether to serialize non-set optional strings [TODO] |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
505 """ |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
506 # TODO: allow file object vs file name |
28
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
507 |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
508 if not format: |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
509 format = self.filename2format(filename) |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
510 if not format: |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
511 raise Exception('Please specify a format') |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
512 # TODO: more specific exception type |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
513 |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
514 provider = self.configuration_provider(format) |
38
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
515 if not provider: |
8909ae1cc4ea
fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
516 raise Exception("Provider not found for format: %s" % format) |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
517 |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
518 config = copy.deepcopy(self.config) |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
519 |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
520 provider.write(config, filename) |
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
521 |
28
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
522 def deserialize(self, filename, format=None): |
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
523 """load configuration from a file""" |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
524 # TODO: allow file object vs file name |
28
c516ab813079
begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
525 |
50
113e8c2f7cab
start to hook up deserializers
Jeff Hammel <jhammel@mozilla.com>
parents:
49
diff
changeset
|
526 assert os.path.exists(filename) |
113e8c2f7cab
start to hook up deserializers
Jeff Hammel <jhammel@mozilla.com>
parents:
49
diff
changeset
|
527 |
41
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
528 # get the format |
29
fadcc6ab51d4
more (de/)serialization stubbing
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
529 if not format: |
41
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
530 format = self.filename2format(filename) |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
531 |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
532 # get the providers in some sensible order |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
533 providers = self.configuration_providers[:] |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
534 if format: |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
535 providers.sort(key=lambda x: int(format in x.extensions), reverse=True) |
34
79aca36abd9a
fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
536 |
41
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
537 # deserialize the data |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
538 for provider in providers: |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
539 try: |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
540 return provider.read(filename) |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
541 except: |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
542 continue |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
543 else: |
1bd4ddf2e81d
deserialization should work now
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
544 raise ConfigurationProviderException("Could not load %s" % filename) |