X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=utils%2Ftest%2Ftestapi%2Fopnfv_testapi%2Fcommon%2Fconfig.py;h=f73c0abf285a1d0db214718c54533f19b74a1ed2;hb=b7644e8f1f43b762bab51d8bc7a68298558a6bdf;hp=ecab88ae316b745a3588b2a7bdf7292efffba421;hpb=50e9b87f746bf4be89617c3ead6e0117b1e52be8;p=releng.git diff --git a/utils/test/testapi/opnfv_testapi/common/config.py b/utils/test/testapi/opnfv_testapi/common/config.py index ecab88ae3..f73c0abf2 100644 --- a/utils/test/testapi/opnfv_testapi/common/config.py +++ b/utils/test/testapi/opnfv_testapi/common/config.py @@ -7,89 +7,54 @@ # http://www.apache.org/licenses/LICENSE-2.0 # feng.xiaowei@zte.com.cn remove prepare_put_request 5-30-2016 ############################################################################## +import ConfigParser +import os -from ConfigParser import SafeConfigParser, NoOptionError +class Config(object): + CONFIG = None + def __init__(self): + self.file = self.CONFIG if self.CONFIG else self._default_config() + self._parse() + self._parse_per_page() + self.static_path = os.path.join( + os.path.dirname(os.path.normpath(__file__)), + os.pardir, + 'static') -class ParseError(Exception): - """ - Custom exception class for config file - """ - - def __init__(self, message): - self.msg = message + def _parse(self): + if not os.path.exists(self.file): + raise Exception("%s not found" % self.file) - def __str__(self): - return 'error parsing config file : %s' % self.msg + config = ConfigParser.RawConfigParser() + config.read(self.file) + self._parse_section(config) + def _parse_section(self, config): + [self._parse_item(config, section) for section in (config.sections())] -class APIConfig: - """ - The purpose of this class is to load values correctly from the config file. - Each key is declared as an attribute in __init__() and linked in parse() - """ + def _parse_item(self, config, section): + [setattr(self, '{}_{}'.format(section, k), self._parse_value(v)) + for k, v in config.items(section)] - def __init__(self): - self._default_config_location = "/etc/opnfv_testapi/config.ini" - self.mongo_url = None - self.mongo_dbname = None - self.api_port = None - self.api_debug_on = None - self._parser = None - self.swagger_base_url = None + def _parse_per_page(self): + if not hasattr(self, 'api_results_per_page'): + self.api_results_per_page = 20 - def _get_parameter(self, section, param): - try: - return self._parser.get(section, param) - except NoOptionError: - raise ParseError("[%s.%s] parameter not found" % (section, param)) - - def _get_int_parameter(self, section, param): + @staticmethod + def _parse_value(value): try: - return int(self._get_parameter(section, param)) - except ValueError: - raise ParseError("[%s.%s] not an int" % (section, param)) - - def _get_bool_parameter(self, section, param): - result = self._get_parameter(section, param) - if str(result).lower() == 'true': - return True - if str(result).lower() == 'false': - return False - - raise ParseError( - "[%s.%s : %s] not a boolean" % (section, param, result)) + value = int(value) + except: + if str(value).lower() == 'true': + value = True + elif str(value).lower() == 'false': + value = False + return value @staticmethod - def parse(config_location=None): - obj = APIConfig() - - if config_location is None: - config_location = obj._default_config_location - - obj._parser = SafeConfigParser() - obj._parser.read(config_location) - if not obj._parser: - raise ParseError("%s not found" % config_location) - - # Linking attributes to keys from file with their sections - obj.mongo_url = obj._get_parameter("mongo", "url") - obj.mongo_dbname = obj._get_parameter("mongo", "dbname") - - obj.api_port = obj._get_int_parameter("api", "port") - obj.api_debug_on = obj._get_bool_parameter("api", "debug") - obj.swagger_base_url = obj._get_parameter("swagger", "base_url") - - return obj - - def __str__(self): - return "mongo_url = %s \n" \ - "mongo_dbname = %s \n" \ - "api_port = %s \n" \ - "api_debug_on = %s \n" \ - "swagger_base_url = %s \n" % (self.mongo_url, - self.mongo_dbname, - self.api_port, - self.api_debug_on, - self.swagger_base_url) + def _default_config(): + is_venv = os.getenv('VIRTUAL_ENV') + return os.path.join('/' if not is_venv else is_venv, + 'etc/opnfv_testapi/config.ini')