-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_genie.py
120 lines (101 loc) · 4.59 KB
/
config_genie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
class ConfigGenie:
def __init__(self):
# Global Configuration
self._global_config = {
"shell_verbose": "True", # just some examples on how this could be used
"log_level": "INFO"
}
# Group configuration
self._group_config = {
"servers": {},
"users": {}
}
# In case you want to restore to the defaults,
# the default values are being assigned to variables
_default_global_config = self._global_config
_default_group_config = self._group_config
self.logger = logging.getLogger("ConfigGenie")
self.logger.info("ConfigGenie component initialized.")
def set_global_config(self, attribute, new_value):
"""
Replaces current value with the given new value for the specified attribute.
Example: set_config("dhcp", False)
:param attribute: [String] The attribute to be modified
:param new_value: [String or Boolean] The new value to be set to the specified attribute
"""
# Verifies if attribute is allowed in global config
if not attribute in self._global_config:
self.logger.error(f"Attribute: '{attribute}' doesn't exist in '_configuration' dictionary.")
# Stores the old attribute value
# and updates the attribute
old_value = self._global_config[attribute]
self._global_config.update({attribute: new_value})
self.logger.info(
f"Attribute '{attribute}' has been modified. Old value: '{old_value}', New value: '{new_value}'")
def set_group_config(self, group, attribute, new_value):
"""
Replaces current value with the given new value for the specified
attribute in a group.
:param group: [Group or List-of Group] Group
:param attribute: [String] The attribute to be modified.
:param new_value: [String or Boolean] The new value to be set.
"""
if not group in self._group_config:
self.logger.warning(f"Group: '{group}' doesn't exist in '_configuration' dictionary.")
if not attribute in self._group_config:
self.logger.warning(f"Attribute: '{attribute}' doesn't exist in '_configuration' dictionary.")
# Stores the old attribute value
# and updates the attribute
old_value = self._global_config[attribute]
self._global_config.update({attribute: new_value})
self.logger.info(
f"Attribute '{attribute}' has been modified. Old value: '{old_value}', New value: '{new_value}'")
def get_global_config(self, *args):
"""
Gets current configuration from ConfigGenie / App (internal). If:
- args > 0 returns those specified attributes
- args = 0 returns whole dictionary
:param args: [String or List-of String] Specified attributes
:return: Dictionary
"""
if args:
# Returns a new dictionary only containing the specified attributes
result = {}
for attribute in args:
if attribute in self._global_config:
result[attribute] = self._global_config[attribute]
self.logger.info(f"Global config has been successfully read.")
return result
else:
# Returns the full dictionary
self.logger.info(f"Global config has been successfully read.")
return self._global_config
def get_group_config(self, group, *args):
"""
Gets current configuration for the given group (internal).
If:
- args > 0 returns those specified attributes
- args = 0 returns whole dictionary
:param group: [Group or List-of Group] Group
:param args: [String or List-of String] Specified attributes
:return: Dictionary
"""
if group in self._group_config:
if args:
# Returns a new dictionary only containing the specified attributes
result = {}
for attribute in args:
if attribute in self._group_config:
result[attribute] = self._global_config[attribute]
self.logger.info(f"Group config has been successfully read.")
return result
else:
# Returns the full dictionary
self.logger.info(f"Group config has been successfully read.")
return self._group_config
def validate_config(self):
"""
Validates the config
"""
self.logger.info(f"Config has been successfully validated.")