Merge "refactor static_path to accomodate web portal"
[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         self.static_path = os.path.join(
21             os.path.dirname(os.path.normpath(__file__)),
22             os.pardir,
23             'static')
24
25     def _parse(self):
26         if not os.path.exists(self.file):
27             raise Exception("%s not found" % self.file)
28
29         config = ConfigParser.RawConfigParser()
30         config.read(self.file)
31         self._parse_section(config)
32
33     def _parse_section(self, config):
34         [self._parse_item(config, section) for section in (config.sections())]
35
36     def _parse_item(self, config, section):
37         [setattr(self, '{}_{}'.format(section, k), self._parse_value(v))
38          for k, v in config.items(section)]
39
40     @staticmethod
41     def _parse_value(value):
42         try:
43             value = int(value)
44         except:
45             if str(value).lower() == 'true':
46                 value = True
47             elif str(value).lower() == 'false':
48                 value = False
49         return value
50
51     @staticmethod
52     def _default_config():
53         is_venv = os.getenv('VIRTUAL_ENV')
54         return os.path.join('/' if not is_venv else is_venv,
55                             'etc/opnfv_testapi/config.ini')