Merge "prototypes: bifrost: Remove OPNFV specific group_vars file"
[releng.git] / utils / test / testapi / opnfv_testapi / common / config.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # feng.xiaowei@zte.com.cn remove prepare_put_request            5-30-2016
9 ##############################################################################
10 import ConfigParser
11 import os
12
13
14 class Config(object):
15     CONFIG = None
16
17     def __init__(self):
18         self.file = self.CONFIG if self.CONFIG else self._default_config()
19         self._parse()
20
21     def _parse(self):
22         if not os.path.exists(self.file):
23             raise Exception("%s not found" % self.file)
24
25         config = ConfigParser.RawConfigParser()
26         config.read(self.file)
27         self._parse_section(config)
28
29     def _parse_section(self, config):
30         [self._parse_item(config, section) for section in (config.sections())]
31
32     def _parse_item(self, config, section):
33         [setattr(self, '{}_{}'.format(section, k), self._parse_value(v))
34          for k, v in config.items(section)]
35
36     @staticmethod
37     def _parse_value(value):
38         try:
39             value = int(value)
40         except:
41             if str(value).lower() == 'true':
42                 value = True
43             elif str(value).lower() == 'false':
44                 value = False
45         return value
46
47     @staticmethod
48     def _default_config():
49         is_venv = os.getenv('VIRTUAL_ENV')
50         return os.path.join('/' if not is_venv else is_venv,
51                             'etc/opnfv_testapi/config.ini')