4cee634945e27afb4f78100b2d67c136a51d52ef
[functest.git] / functest / utils / config.py
1 import os
2
3 import yaml
4
5
6 class Config(object):
7     def __init__(self):
8         if 'CONFIG_FUNCTEST_YAML' not in os.environ:
9             raise Exception('CONFIG_FUNCTEST_YAML not configed')
10         self.config_functest = os.environ['CONFIG_FUNCTEST_YAML']
11         try:
12             with open(self.config_functest) as f:
13                 self.functest_yaml = yaml.safe_load(f)
14                 self.parse(None, self.functest_yaml)
15         except:
16             raise Exception('Parse {} failed'.format(self.config_functest))
17
18     def parse(self, attr_now, left_parametes):
19         for param_n, param_v in left_parametes.iteritems():
20             attr_further = self.get_attr_further(attr_now, param_n)
21             if not isinstance(param_v, dict):
22                 self.__setattr__(attr_further, param_v)
23             else:
24                 self.parse(attr_further, param_v)
25
26     def get_attr_further(self, attr_now, next):
27         return attr_now if next == 'general' else (
28             '{}_{}'.format(attr_now, next) if attr_now else next)
29
30 CONF = Config()