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