4 from ConfigParser import SafeConfigParser, NoOptionError
7 class ParseError(Exception):
9 Custom exception class for config file
12 def __init__(self, message):
16 return 'error parsing config file : %s' % self.msg
21 The purpose of this class is to load values correctly from the config file.
22 Each key is declared as an attribute in __init__() and linked in parse()
26 self._default_config_location = "/etc/dashboard/config.ini"
27 self.es_url = 'http://localhost:9200'
29 self.kibana_url = None
33 def _get_str_parameter(self, section, param):
35 return self._parser.get(section, param)
37 raise ParseError("[%s.%s] parameter not found" % (section, param))
39 def _get_int_parameter(self, section, param):
41 return int(self._get_str_parameter(section, param))
43 raise ParseError("[%s.%s] not an int" % (section, param))
45 def _get_bool_parameter(self, section, param):
46 result = self._get_str_parameter(section, param)
47 if str(result).lower() == 'true':
49 if str(result).lower() == 'false':
53 "[%s.%s : %s] not a boolean" % (section, param, result))
56 def parse(config_location=None):
59 if config_location is None:
60 config_location = obj._default_config_location
62 obj._parser = SafeConfigParser()
63 obj._parser.read(config_location)
65 raise ParseError("%s not found" % config_location)
67 # Linking attributes to keys from file with their sections
68 obj.es_url = obj._get_str_parameter("elastic", "url")
69 obj.es_creds = obj._get_str_parameter("elastic", "creds")
70 obj.kibana_url = obj._get_str_parameter("kibana", "url")
71 obj.js_path = obj._get_str_parameter("kibana", "js_path")
72 index = obj._get_str_parameter("elastic", "index")
73 obj.index_url = urlparse.urljoin(obj.es_url, index)
78 return "elastic_url = %s \n" \
79 "elastic_creds = %s \n" \
80 "kibana_url = %s \n" \
82 "js_path = %s \n" % (self.es_url,