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 ##############################################################################
14 class ParseError(Exception):
16 Custom exception class for config file
19 def __init__(self, message):
23 return 'error parsing config file : %s' % self.msg
26 class APIConfig(object):
28 The purpose of this class is to load values correctly from the config file.
29 Each key is declared as an attribute in __init__() and linked in parse()
33 self._default_config_location = "/etc/opnfv_testapi/config.ini"
35 self.mongo_dbname = None
37 self.api_debug_on = None
38 self.api_authenticate_on = None
40 self.swagger_base_url = None
42 def _get_parameter(self, section, param):
44 return self._parser.get(section, param)
45 except ConfigParser.NoOptionError:
46 raise ParseError("No parameter: [%s.%s]" % (section, param))
48 def _get_int_parameter(self, section, param):
50 return int(self._get_parameter(section, param))
52 raise ParseError("Not int: [%s.%s]" % (section, param))
54 def _get_bool_parameter(self, section, param):
55 result = self._get_parameter(section, param)
56 if str(result).lower() == 'true':
58 if str(result).lower() == 'false':
62 "Not boolean: [%s.%s : %s]" % (section, param, result))
65 def parse(config_location=None):
68 if config_location is None:
69 config_location = obj._default_config_location
71 if not os.path.exists(config_location):
72 raise ParseError("%s not found" % config_location)
74 obj._parser = ConfigParser.SafeConfigParser()
75 obj._parser.read(config_location)
77 # Linking attributes to keys from file with their sections
78 obj.mongo_url = obj._get_parameter("mongo", "url")
79 obj.mongo_dbname = obj._get_parameter("mongo", "dbname")
81 obj.api_port = obj._get_int_parameter("api", "port")
82 obj.api_debug_on = obj._get_bool_parameter("api", "debug")
83 obj.api_authenticate_on = obj._get_bool_parameter("api",
86 obj.swagger_base_url = obj._get_parameter("swagger", "base_url")