Add external config support to result_collection_api
[releng.git] / utils / test / result_collection_api / common / config.py
index 9f7272b..a0d0757 100644 (file)
@@ -7,15 +7,8 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-"""
-from ConfigParser import SafeConfigParser
 
-parser = SafeConfigParser()
-parser.read('config.ini')
-
-
-mongo_url = parser.get('default', 'mongo_url')
-"""
+from ConfigParser import SafeConfigParser, NoOptionError
 
 
 def prepare_put_request(edit_request, key, new_value, old_value):
@@ -31,3 +24,76 @@ def prepare_put_request(edit_request, key, new_value, old_value):
                 edit_request[key] = new_value
 
     return edit_request
+
+
+class ParseError(Exception):
+    """
+    Custom exception class for config file
+    """
+
+    def __init__(self, message):
+        self.msg = message
+
+    def __str__(self):
+        return 'error parsing config file : %s' % self.msg
+
+
+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 __init__(self):
+        self._default_config_location = "config.ini"
+        self.mongo_url = None
+        self.api_port = None
+        self.api_debug_on = None
+        self._parser = None
+
+    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):
+        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))
+
+    @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.api_port = obj._get_int_parameter("api", "port")
+        obj.api_debug_on = obj._get_bool_parameter("api", "debug")
+        return obj
+
+    def __str__(self):
+        return "mongo_url = %s \n" \
+               "api_port = %s \n" \
+               "api_debug_on = %s \n" % (self.mongo_url,
+                                         self.api_port,
+                                         self.api_debug_on)