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.elastic_url = 'http://localhost:9200'
27 self.elastic_creds = None
28 self.kibana_url = None
32 def _get_str_parameter(self, section, param):
34 return self._parser.get(section, param)
36 raise ParseError("[%s.%s] parameter not found" % (section, param))
38 def _get_int_parameter(self, section, param):
40 return int(self._get_str_parameter(section, param))
42 raise ParseError("[%s.%s] not an int" % (section, param))
44 def _get_bool_parameter(self, section, param):
45 result = self._get_str_parameter(section, param)
46 if str(result).lower() == 'true':
48 if str(result).lower() == 'false':
52 "[%s.%s : %s] not a boolean" % (section, param, result))
55 def parse(config_location=None):
58 if config_location is None:
59 config_location = obj._default_config_location
61 obj._parser = SafeConfigParser()
62 obj._parser.read(config_location)
64 raise ParseError("%s not found" % config_location)
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")
76 return "elastic_url = %s \n" \
77 "elastic_creds = %s \n" \
78 "kibana_url = %s \n" \
80 "js_path = %s \n" % (self.elastic_url,