3 from ConfigParser import SafeConfigParser, NoOptionError
6 class ParseError(Exception):
8 Custom exception class for config file
11 def __init__(self, message):
15 return 'error parsing config file : %s' % self.msg
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()
25 self._default_config_location = "../etc/config.ini"
26 self.es_url = 'http://localhost:9200'
28 self.kibana_url = None
31 def _get_str_parameter(self, section, param):
33 return self._parser.get(section, param)
35 raise ParseError("[%s.%s] parameter not found" % (section, param))
37 def _get_int_parameter(self, section, param):
39 return int(self._get_str_parameter(section, param))
41 raise ParseError("[%s.%s] not an int" % (section, param))
43 def _get_bool_parameter(self, section, param):
44 result = self._get_str_parameter(section, param)
45 if str(result).lower() == 'true':
47 if str(result).lower() == 'false':
51 "[%s.%s : %s] not a boolean" % (section, param, result))
54 def parse(config_location=None):
57 if config_location is None:
58 config_location = obj._default_config_location
60 obj._parser = SafeConfigParser()
61 obj._parser.read(config_location)
63 raise ParseError("%s not found" % config_location)
65 # Linking attributes to keys from file with their sections
66 obj.es_url = obj._get_str_parameter("elastic", "url")
67 obj.es_creds = obj._get_str_parameter("elastic", "creds")
68 obj.kibana_url = obj._get_str_parameter("kibana", "url")
69 obj.js_path = obj._get_str_parameter("kibana", "js_path")
74 return "elastic_url = %s \n" \
75 "elastic_creds = %s \n" \
76 "kibana_url = %s \n" \
77 "js_path = %s \n" % (self.es_url,