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