use configure file rather than arguments to organize the configuration
[releng.git] / utils / test / scripts / 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 = "./config.ini"
26         self.elastic_url = 'http://localhost:9200'
27         self.elastic_creds = None
28         self.destination = 'elasticsearch'
29         self.kibana_url = None
30         self.is_js = True
31         self.js_path = None
32
33     def _get_str_parameter(self, section, param):
34         try:
35             return self._parser.get(section, param)
36         except NoOptionError:
37             raise ParseError("[%s.%s] parameter not found" % (section, param))
38
39     def _get_int_parameter(self, section, param):
40         try:
41             return int(self._get_str_parameter(section, param))
42         except ValueError:
43             raise ParseError("[%s.%s] not an int" % (section, param))
44
45     def _get_bool_parameter(self, section, param):
46         result = self._get_str_parameter(section, param)
47         if str(result).lower() == 'true':
48             return True
49         if str(result).lower() == 'false':
50             return False
51
52         raise ParseError(
53             "[%s.%s : %s] not a boolean" % (section, param, result))
54
55     @staticmethod
56     def parse(config_location=None):
57         obj = APIConfig()
58
59         if config_location is None:
60             config_location = obj._default_config_location
61
62         obj._parser = SafeConfigParser()
63         obj._parser.read(config_location)
64         if not obj._parser:
65             raise ParseError("%s not found" % config_location)
66
67         # Linking attributes to keys from file with their sections
68         obj.elastic_url = obj._get_str_parameter("elastic", "url")
69         obj.elastic_creds = obj._get_str_parameter("elastic", "creds")
70         obj.destination = obj._get_str_parameter("output", "destination")
71         obj.kibana_url = obj._get_str_parameter("kibana", "url")
72         obj.is_js = obj._get_bool_parameter("kibana", "js")
73         obj.js_path = obj._get_str_parameter("kibana", "js_path")
74
75         return obj
76
77     def __str__(self):
78         return "elastic_url = %s \n" \
79                "elastic_creds = %s \n" \
80                "destination = %s \n" \
81                "kibana_url = %s \n" \
82                "is_js = %s \n" \
83                "js_path = %s \n" % (self.elastic_url,
84                                         self.elastic_creds,
85                                         self.destination,
86                                         self.kibana_url,
87                                         self.is_js,
88                                         self.js_path)