Merge "Fix how tier_handler is imported"
[functest.git] / functest / utils / config.py
1 #!/usr/bin/env python
2
3 import yaml
4
5 import six
6
7 from functest.utils import env
8
9
10 class Config(object):
11     def __init__(self):
12         try:
13             with open(env.ENV.CONFIG_FUNCTEST_YAML) as f:
14                 self.functest_yaml = yaml.safe_load(f)
15                 self._parse(None, self.functest_yaml)
16         except Exception as error:
17             raise Exception('Parse config failed: {}'.format(str(error)))
18         self._set_others()
19
20     def _parse(self, attr_now, left_parametes):
21         for param_n, param_v in six.iteritems(left_parametes):
22             attr_further = self._get_attr_further(attr_now, param_n)
23             if attr_further:
24                 self.__setattr__(attr_further, param_v)
25             if isinstance(param_v, dict):
26                 self._parse(attr_further, param_v)
27
28     def _get_attr_further(self, attr_now, next):
29         return attr_now if next == 'general' else (
30             '{}_{}'.format(attr_now, next) if attr_now else next)
31
32     def _set_others(self):
33         pass
34
35 CONF = Config()