Merge "Avoid build conflicts in build server"
[releng.git] / utils / test / testapi / opnfv_testapi / common / config.py
index 84a1273..362fca6 100644 (file)
@@ -8,6 +8,7 @@
 # feng.xiaowei@zte.com.cn remove prepare_put_request            5-30-2016
 ##############################################################################
 import ConfigParser
+import os
 
 
 class ParseError(Exception):
@@ -22,14 +23,14 @@ class ParseError(Exception):
         return 'error parsing config file : %s' % self.msg
 
 
-class APIConfig:
+class APIConfig(object):
     """
     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 __init__(self):
-        self._default_config_location = "/etc/opnfv_testapi/config.ini"
+        self._set_default_config()
         self.mongo_url = None
         self.mongo_dbname = None
         self.api_port = None
@@ -38,17 +39,22 @@ class APIConfig:
         self._parser = None
         self.swagger_base_url = None
 
+    def _set_default_config(self):
+        venv = os.getenv('VIRTUAL_ENV')
+        self._default_config = os.path.join('/' if not venv else venv,
+                                            'etc/opnfv_testapi/config.ini')
+
     def _get_parameter(self, section, param):
         try:
             return self._parser.get(section, param)
         except ConfigParser.NoOptionError:
-            raise ParseError("[%s.%s] parameter not found" % (section, param))
+            raise ParseError("No parameter: [%s.%s]" % (section, param))
 
     def _get_int_parameter(self, section, param):
         try:
             return int(self._get_parameter(section, param))
         except ValueError:
-            raise ParseError("[%s.%s] not an int" % (section, param))
+            raise ParseError("Not int: [%s.%s]" % (section, param))
 
     def _get_bool_parameter(self, section, param):
         result = self._get_parameter(section, param)
@@ -58,19 +64,20 @@ class APIConfig:
             return False
 
         raise ParseError(
-            "[%s.%s : %s] not a boolean" % (section, param, result))
+            "Not boolean: [%s.%s : %s]" % (section, param, result))
 
     @staticmethod
     def parse(config_location=None):
         obj = APIConfig()
 
         if config_location is None:
-            config_location = obj._default_config_location
+            config_location = obj._default_config
+
+        if not os.path.exists(config_location):
+            raise ParseError("%s not found" % config_location)
 
         obj._parser = ConfigParser.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")
@@ -84,15 +91,3 @@ class APIConfig:
         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.api_authenticate_on,
-                                             self.swagger_base_url)