comparison configuration/config.py @ 6:dce954a3831f

more stubbing
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 24 Mar 2012 23:14:22 -0700
parents 7910b0ef0bab
children 6e3cf8f05464
comparison
equal deleted inserted replaced
5:7910b0ef0bab 6:dce954a3831f
18 try: 18 try:
19 import yaml 19 import yaml
20 except ImportError: 20 except ImportError:
21 yaml = None 21 yaml = None
22 22
23 __all__ = ['Configuration', 'configuration_providers']
24
23 configuration_providers = [] 25 configuration_providers = []
24 if json: 26 if json:
25 class JSON(object): 27 class JSON(object):
26 extensions = ['json'] 28 extensions = ['json']
27 configuration_providers.append(JSON) 29 configuration_providers.append(JSON)
28 30
29 if yaml: 31 if yaml:
30 class YAML(object): 32 class YAML(object):
31 extensions = ['yml'] 33 extensions = ['yml']
32 def read(self, filename): 34 def read(self, filename):
33 pass 35 f = file(filename)
36 config = yaml.load(f)
37 f.close()
38 return config
39
34 configuration_providers.append(YAML) 40 configuration_providers.append(YAML)
35 41
36 __all__ = ['Configuration']
37 42
38 class Configuration(object): 43 class Configuration(object):
39 options = {} 44 options = {}
40 45
41 def __init__(self): 46 def __init__(self, configuration_providers=configuration_providers):
42 self.config = {} 47 self.config = {}
48 self.configuration_providers = configuration_providers
43 49
44 def check(self): 50 def check(self, config):
45 """check validity of configuration""" 51 """check validity of configuration"""
52
53 # TODO: ensure options in configuration are in self.options
54 unknown_options = []
55
56 #
46 57
47 def __call__(self, *args): 58 def __call__(self, *args):
48 """add items to configuration and check it""" 59 """add items to configuration and check it"""
49 60
50 def add(self, config): 61 def add(self, config):
51 """update configuration: not undoable""" 62 """update configuration: not undoable"""
63
64 self.check(config)
65
52 self.config.update(config) 66 self.config.update(config)
53 # TODO: option to extend; augment lists/dicts 67 # TODO: option to extend; augment lists/dicts
54 68
55 def main(args=sys.argv[:]): 69 def main(args=sys.argv[:]):
56 70