Mercurial > hg > configuration
comparison configuration/configuration.py @ 131:dff886188b55
minor fixes
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 30 Mar 2014 19:27:41 -0700 |
parents | ee8fcdda1fd2 |
children | f01d351ccf2b |
comparison
equal
deleted
inserted
replaced
130:6b798d23f99f | 131:dff886188b55 |
---|---|
29 'MissingValueException', | 29 'MissingValueException', |
30 'ConfigurationProviderException', | 30 'ConfigurationProviderException', |
31 'TypeCastException', | 31 'TypeCastException', |
32 'ConfigurationOption'] | 32 'ConfigurationOption'] |
33 | 33 |
34 | |
34 ### exceptions | 35 ### exceptions |
35 | 36 |
36 class UnknownOptionException(Exception): | 37 class UnknownOptionException(Exception): |
37 """exception raised when a non-configuration value is present in the configuration""" | 38 """exception raised when a non-configuration value is present in the configuration""" |
38 | 39 |
63 f = file(filename, 'w') | 64 f = file(filename, 'w') |
64 newfile = True | 65 newfile = True |
65 else: | 66 else: |
66 f = filename | 67 f = filename |
67 newfile = False | 68 newfile = False |
68 exception = None | |
69 try: | 69 try: |
70 self._write(f, config) | 70 self._write(f, config) |
71 except Exception, exception: | 71 finally: |
72 pass | 72 if newfile: |
73 if newfile: | 73 f.close() |
74 f.close() | |
75 if exception: | |
76 raise exception | |
77 def _write(self, fp, config): | 74 def _write(self, fp, config): |
78 raise NotImplementedError("Abstract base class") | 75 raise NotImplementedError("Abstract base class") |
79 | 76 |
80 if json: | 77 if json: |
81 class JSON(ConfigurationProvider): | 78 class JSON(ConfigurationProvider): |
83 extensions = ['json'] | 80 extensions = ['json'] |
84 def read(self, filename): | 81 def read(self, filename): |
85 return json.loads(file(filename).read()) | 82 return json.loads(file(filename).read()) |
86 def _write(self, fp, config): | 83 def _write(self, fp, config): |
87 fp.write(json.dumps(config, indent=self.indent, sort_keys=True)) | 84 fp.write(json.dumps(config, indent=self.indent, sort_keys=True)) |
88 # TODO: could use templates to get order down, etc | |
89 configuration_providers.append(JSON()) | 85 configuration_providers.append(JSON()) |
90 | 86 |
91 if yaml: | 87 if yaml: |
92 class YAML(ConfigurationProvider): | 88 class YAML(ConfigurationProvider): |
93 extensions = ['yml', 'yaml'] | 89 extensions = ['yml', 'yaml'] |
130 # add the parsed option to the set of things parsed | 126 # add the parsed option to the set of things parsed |
131 if not hasattr(parser, 'parsed'): | 127 if not hasattr(parser, 'parsed'): |
132 parser.parsed = dict() | 128 parser.parsed = dict() |
133 parser.parsed[dest] = value | 129 parser.parsed[dest] = value |
134 | 130 |
131 | |
135 ### plugins for option types | 132 ### plugins for option types |
136 | 133 |
137 class BaseCLI(object): | 134 class BaseCLI(object): |
138 """base_cli for all option types""" | 135 """base_cli for all option types""" |
139 | 136 |
154 kw['action'] = 'store' | 151 kw['action'] = 'store' |
155 return args, kw | 152 return args, kw |
156 | 153 |
157 def take_action(self, value): | 154 def take_action(self, value): |
158 return value | 155 return value |
156 | |
159 | 157 |
160 class BoolCLI(BaseCLI): | 158 class BoolCLI(BaseCLI): |
161 | 159 |
162 def __call__(self, name, value): | 160 def __call__(self, name, value): |
163 | 161 |