no more output elastic docs to stdout/console
[releng.git] / utils / test / dashboard / dashboard / conf / config.py
1 #! /usr/bin/env python
2
3 from ConfigParser import SafeConfigParser, NoOptionError
4
5
6 class ParseError(Exception):
7     """
8     Custom exception class for config file
9     """
10
11     def __init__(self, message):
12         self.msg = message
13
14     def __str__(self):
15         return 'error parsing config file : %s' % self.msg
16
17
18 class APIConfig:
19     """
20     The purpose of this class is to load values correctly from the config file.
21     Each key is declared as an attribute in __init__() and linked in parse()
22     """
23
24     def __init__(self):
25         self._default_config_location = "../etc/config.ini"
26         self.elastic_url = 'http://localhost:9200'
27         self.elastic_creds = None
28         self.kibana_url = None
29         self.is_js = True
30         self.js_path = None
31
32     def _get_str_parameter(self, section, param):
33         try:
34             return self._parser.get(section, param)
35         except NoOptionError:
36             raise ParseError("[%s.%s] parameter not found" % (section, param))
37
38     def _get_int_parameter(self, section, param):
39         try:
40             return int(self._get_str_parameter(section, param))
41         except ValueError:
42             raise ParseError("[%s.%s] not an int" % (section, param))
43
44     def _get_bool_parameter(self, section, param):
45         result = self._get_str_parameter(section, param)
46         if str(result).lower() == 'true':
47             return True
48         if str(result).lower() == 'false':
49             return False
50
51         raise ParseError(
52             "[%s.%s : %s] not a boolean" % (section, param, result))
53
54     @staticmethod
55     def parse(config_location=None):
56         obj = APIConfig()
57
58         if config_location is None:
59             config_location = obj._default_config_location
60
61         obj._parser = SafeConfigParser()
62         obj._parser.read(config_location)
63         if not obj._parser:
64             raise ParseError("%s not found" % config_location)
65
66         # Linking attributes to keys from file with their sections
67         obj.elastic_url = obj._get_str_parameter("elastic", "url")
68         obj.elastic_creds = obj._get_str_parameter("elastic", "creds")
69         obj.kibana_url = obj._get_str_parameter("kibana", "url")
70         obj.is_js = obj._get_bool_parameter("kibana", "js")
71         obj.js_path = obj._get_str_parameter("kibana", "js_path")
72
73         return obj
74
75     def __str__(self):
76         return "elastic_url = %s \n" \
77                "elastic_creds = %s \n" \
78                "kibana_url = %s \n" \
79                "is_js = %s \n" \
80                "js_path = %s \n" % (self.elastic_url,
81                                     self.elastic_creds,
82                                     self.kibana_url,
83                                     self.is_js,
84                                     self.js_path)