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