-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
executable file
·144 lines (116 loc) · 8.02 KB
/
config.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright (C) 2024 V0LT - Conner Vieira
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License along with this program (LICENSE)
# If not, see https://www.gnu.org/licenses/ to read the license agreement.
import os # Required to interact with certain operating system functions
import json # Required to process JSON data
assassin_root_directory = str(os.path.dirname(os.path.realpath(__file__))) # This variable determines the folder path of the root Assassin directory. This should usually automatically recognize itself, but it if it doesn't, you can change it manually.
import utils
debug_message = utils.debug_message
is_json = utils.is_json
def load_config(file_override=""):
if (file_override != ""):
configuration_file_path = file_override
else:
if (os.path.exists(str(assassin_root_directory + "/config.json")) == True): # Check to see if the configuration file exists in the default location.
configuration_file_path = assassin_root_directory + "/config.json"
elif (os.path.exists(str(assassin_root_directory + "/../config.json")) == True): # Check to see if the configuration file exists in the parent directory. This may occur if this script is being used in a subfolder of Assassin.
configuration_file_path = assassin_root_directory + "/../config.json"
else: # The configuration file couldn't be located. Assassin can't continue to load.
config = {} # Set the configuration to a blank placeholder dictionary.
print("The configuration file could not be located from " + str(os.path.realpath(__file__)))
exit()
with open(configuration_file_path) as configuration_file: raw_configuration_file_contents = configuration_file.read() # Read the contents of the configuration file.
if (is_json(raw_configuration_file_contents)):
config = json.loads(raw_configuration_file_contents) # Load the configuration database from the contents of the config.json file.
else:
config = {} # Set the configuration to a blank placeholder dictionary.
print("The configuration file found at " + configuration_file_path + " does not appear to be valid JSON.")
exit()
return config # Return the loaded configuration information.
def check_value(value, template):
if (type(template) == list): # Check to see if the template for this value is a list of acceptable values.
if (value not in template): # Check to see if the configuration value is in the list of acceptable values.
return False
elif (type(template) == str):
if (template == "str"): # 'str' means this value needs to be a string.
if (type(value) != str):
return False
elif (template == "bool"): # 'bool' means this value needs to be true or false.
if (type(value) != bool):
return False
elif (template == "float"): # 'float' means this value needs to be a number.
if (type(value) != float and type(value) != int):
return False
elif (template == "+float"): # '+float' means this value needs to be a number greater than or equal to 0.
if (type(value) != float and type(value) != int):
return False
elif (value < 0.0):
return False
elif (template == "int"): # 'int' means this value needs to be a whole number.
if (type(value) != int):
return False
elif (template == "+int"): # '+int' means this value needs to be a whole number greater than or equal to 0.
if (type(value) != int):
return False
elif (value < 0.0):
return False
elif (template == "list"): # 'list' means this value needs to be a list.
if (type(value) != list):
return False
elif (template == "dir"): # 'dir' means this value needs to point to a directory that exists.
if (os.path.isdir(value) == False):
return False
elif (template == "file"): # 'file' means this value needs to point to a file that exists.
if (os.path.isfile(value) == False):
return False
else:
print("An entry in the configuration outline template is an unexpected value.")
return True
else:
print("An entry in the configuration outline template is an unexpected type.")
return True
return True
def validate_config(config):
config_outline = load_config("./assets/support/configoutline.json")
invalid_values = []
for key1, section1 in config_outline.items():
if (type(section1) == dict):
for key2, section2 in section1.items():
if (type(section2) == dict):
for key3, section3 in section2.items():
if (type(section3) == dict):
for key4, section4 in section3.items():
if (type(section4) == dict):
for key5, section5 in section4.items():
if (type(section5) == dict):
for key6, section6 in section5.items():
if (type(section6) == dict):
for key7, section7 in section6.items():
if (type(section7) == dict):
print("The configuration validation function hit a nested configuration outline section that exceeded 7 layers. The normal configuration outline file should never reach this point.")
exit()
else:
if (check_value(config[key1][key2][key3][key4][key5][key6][key7], section7) == False):
invalid_values.append(key1 + ">" + key2 + ">" + key3 + ">" + key4 + ">" + key5 + ">" + key6 + ">" + key7)
else:
if (check_value(config[key1][key2][key3][key4][key5][key6], section6) == False):
invalid_values.append(key1 + ">" + key2 + ">" + key3 + ">" + key4 + ">" + key5 + ">" + key6)
else:
if (check_value(config[key1][key2][key3][key4][key5], section5) == False):
invalid_values.append(key1 + ">" + key2 + ">" + key3 + ">" + key4 + ">" + key5)
else:
if (check_value(config[key1][key2][key3][key4], section4) == False):
invalid_values.append(key1 + ">" + key2 + ">" + key3 + ">" + key4)
else:
if (check_value(config[key1][key2][key3], section3) == False):
invalid_values.append(key1 + ">" + key2 + ">" + key3)
else:
if (check_value(config[key1][key2], section2) == False):
invalid_values.append(key1 + ">" + key2)
else:
if (check_value(config[key1], section1) == False):
invalid_values.append(key1)
return invalid_values
config = load_config() # Execute the configuration loading.