Merge "Use cperf master branch even from apex danube"
[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 ParseError(Exception):
15     """
16     Custom exception class for config file
17     """
18
19     def __init__(self, message):
20         self.msg = message
21
22     def __str__(self):
23         return 'error parsing config file : %s' % self.msg
24
25
26 class APIConfig(object):
27     """
28     The purpose of this class is to load values correctly from the config file.
29     Each key is declared as an attribute in __init__() and linked in parse()
30     """
31
32     def __init__(self):
33         self._set_default_config()
34         self.mongo_url = None
35         self.mongo_dbname = None
36         self.api_port = None
37         self.api_debug_on = None
38         self.api_authenticate_on = None
39         self._parser = None
40         self.swagger_base_url = None
41
42     def _set_default_config(self):
43         venv = os.getenv('VIRTUAL_ENV')
44         self._default_config = os.path.join('/' if not venv else venv,
45                                             'etc/opnfv_testapi/config.ini')
46
47     def _get_parameter(self, section, param):
48         try:
49             return self._parser.get(section, param)
50         except ConfigParser.NoOptionError:
51             raise ParseError("No parameter: [%s.%s]" % (section, param))
52
53     def _get_int_parameter(self, section, param):
54         try:
55             return int(self._get_parameter(section, param))
56         except ValueError:
57             raise ParseError("Not int: [%s.%s]" % (section, param))
58
59     def _get_bool_parameter(self, section, param):
60         result = self._get_parameter(section, param)
61         if str(result).lower() == 'true':
62             return True
63         if str(result).lower() == 'false':
64             return False
65
66         raise ParseError(
67             "Not boolean: [%s.%s : %s]" % (section, param, result))
68
69     @staticmethod
70     def parse(config_location=None):
71         obj = APIConfig()
72
73         if config_location is None:
74             config_location = obj._default_config
75
76         if not os.path.exists(config_location):
77             raise ParseError("%s not found" % config_location)
78
79         obj._parser = ConfigParser.SafeConfigParser()
80         obj._parser.read(config_location)
81
82         # Linking attributes to keys from file with their sections
83         obj.mongo_url = obj._get_parameter("mongo", "url")
84         obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
85
86         obj.api_port = obj._get_int_parameter("api", "port")
87         obj.api_debug_on = obj._get_bool_parameter("api", "debug")
88         obj.api_authenticate_on = obj._get_bool_parameter("api",
89                                                           "authenticate")
90
91         obj.swagger_base_url = obj._get_parameter("swagger", "base_url")
92
93         return obj