a62e80593e78aa9da76bbb5f1aad4c6b8e60971d
[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.mongo_dbname = None
51         self.api_port = None
52         self.api_debug_on = None
53         self._parser = None
54
55     def _get_parameter(self, section, param):
56         try:
57             return self._parser.get(section, param)
58         except NoOptionError:
59             raise ParseError("[%s.%s] parameter not found" % (section, param))
60
61     def _get_int_parameter(self, section, param):
62         try:
63             return int(self._get_parameter(section, param))
64         except ValueError:
65             raise ParseError("[%s.%s] not an int" % (section, param))
66
67     def _get_bool_parameter(self, section, param):
68         result = self._get_parameter(section, param)
69         if str(result).lower() == 'true':
70             return True
71         if str(result).lower() == 'false':
72             return False
73
74         raise ParseError(
75             "[%s.%s : %s] not a boolean" % (section, param, result))
76
77     @staticmethod
78     def parse(config_location=None):
79         obj = APIConfig()
80
81         if config_location is None:
82             config_location = obj._default_config_location
83
84         obj._parser = SafeConfigParser()
85         obj._parser.read(config_location)
86         if not obj._parser:
87             raise ParseError("%s not found" % config_location)
88
89         # Linking attributes to keys from file with their sections
90         obj.mongo_url = obj._get_parameter("mongo", "url")
91         obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
92
93         obj.api_port = obj._get_int_parameter("api", "port")
94         obj.api_debug_on = obj._get_bool_parameter("api", "debug")
95
96         return obj
97
98     def __str__(self):
99         return "mongo_url = %s \n" \
100                "mongo_dbname = %s \n" \
101                "api_port = %s \n" \
102                "api_debug_on = %s \n" % (self.mongo_url,
103                                          self.mongo_dbname,
104                                          self.api_port,
105                                          self.api_debug_on)