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 ##############################################################################
11 from ConfigParser import SafeConfigParser, NoOptionError
14 def prepare_put_request(edit_request, key, new_value, old_value):
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
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
29 class ParseError(Exception):
31 Custom exception class for config file
34 def __init__(self, message):
38 return 'error parsing config file : %s' % self.msg
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()
48 self._default_config_location = "config.ini"
50 self.mongo_dbname = None
52 self.api_debug_on = None
55 def _get_parameter(self, section, param):
57 return self._parser.get(section, param)
59 raise ParseError("[%s.%s] parameter not found" % (section, param))
61 def _get_int_parameter(self, section, param):
63 return int(self._get_parameter(section, param))
65 raise ParseError("[%s.%s] not an int" % (section, param))
67 def _get_bool_parameter(self, section, param):
68 result = self._get_parameter(section, param)
69 if str(result).lower() == 'true':
71 if str(result).lower() == 'false':
75 "[%s.%s : %s] not a boolean" % (section, param, result))
78 def parse(config_location=None):
81 if config_location is None:
82 config_location = obj._default_config_location
84 obj._parser = SafeConfigParser()
85 obj._parser.read(config_location)
87 raise ParseError("%s not found" % config_location)
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")
93 obj.api_port = obj._get_int_parameter("api", "port")
94 obj.api_debug_on = obj._get_bool_parameter("api", "debug")
99 return "mongo_url = %s \n" \
100 "mongo_dbname = %s \n" \
102 "api_debug_on = %s \n" % (self.mongo_url,