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 ##############################################################################
12 from ConfigParser import SafeConfigParser, NoOptionError
15 class ParseError(Exception):
17 Custom exception class for config file
20 def __init__(self, message):
24 return 'error parsing config file : %s' % self.msg
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()
34 self._default_config_location = "config.ini"
36 self.mongo_dbname = None
38 self.api_debug_on = None
41 def _get_parameter(self, section, param):
43 return self._parser.get(section, param)
45 raise ParseError("[%s.%s] parameter not found" % (section, param))
47 def _get_int_parameter(self, section, param):
49 return int(self._get_parameter(section, param))
51 raise ParseError("[%s.%s] not an int" % (section, param))
53 def _get_bool_parameter(self, section, param):
54 result = self._get_parameter(section, param)
55 if str(result).lower() == 'true':
57 if str(result).lower() == 'false':
61 "[%s.%s : %s] not a boolean" % (section, param, result))
64 def parse(config_location=None):
67 if config_location is None:
68 config_location = obj._default_config_location
70 obj._parser = SafeConfigParser()
71 obj._parser.read(config_location)
73 raise ParseError("%s not found" % config_location)
75 # Linking attributes to keys from file with their sections
76 obj.mongo_url = obj._get_parameter("mongo", "url")
77 obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
79 obj.api_port = obj._get_int_parameter("api", "port")
80 obj.api_debug_on = obj._get_bool_parameter("api", "debug")
85 return "mongo_url = %s \n" \
86 "mongo_dbname = %s \n" \
88 "api_debug_on = %s \n" % (self.mongo_url,