forked from CameronDeweerd/FFXIV-Market-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_handler.py
308 lines (286 loc) · 12.8 KB
/
config_handler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
Module for handling all config functions for FFXIV-Market-Calculator
"""
import ast
import configparser
import os
import logging
import pathlib
class ConfigHandler:
"""
Class for handling the script configuration.
Attributes:
-------
parser : ConfigParser object
ConfigParser object for use with config manipulation
configfile : str
The config file path/name
global_db : SqlManager object
Global database to store shared values
log_handler : Logging object
Used before logging config is loaded
ffxiv_logger : Logger object
Used for logging functions
config : dict
Dictionary to store the loaded configurations
Methods:
-------
config_create():
Creates a new config file with all default options
parse_main_config():
Retrieves main values from config file
parse_logging_config():
Retrieves logging values from config file
parse_discord_config():
Retrieves discord values from config file
main_validation():
Validates the main config values for correct values/types
logging_validation():
Validates the logging config values for correct values/types
discord_validation():
Validates thee discord config values for correct values/types
get_config_parser():
Unsure, this could potentially be blown away as I cannot find a usage in project
"""
parser = configparser.ConfigParser()
def __init__(self, configfile, global_db):
"""
Constructs all the necessary attributes for the ConfigParser object
and creates a new config file if none exists.
Parameters:
configfile : str
The config file path/name
global_db : SqlManager
Global database to store shared values
"""
self.parser = configparser.ConfigParser()
self.configfile = configfile
self.global_db = global_db
self.log_handler = logging
self.ffxiv_logger = self.log_handler.getLogger(__name__)
self.config = {}
if not os.path.exists(pathlib.Path(__file__).parent / self.configfile):
self.ffxiv_logger.info("Config File does not exist, creating one with default values")
self.config_create()
def config_create(self):
"""
Write a new default config file if none exists
"""
self.parser.add_section('MAIN')
self.parser['MAIN']['MarketboardType'] = 'World'
self.parser['MAIN']['Datacentre'] = 'Crystal'
self.parser['MAIN']['World'] = 'Zalera'
self.parser['MAIN']['ResultQuantity'] = '50'
self.parser['MAIN']['UpdateQuantity'] = '0'
self.parser["MAIN"]['DisplayWithoutCraftCost'] = 'False'
self.parser["MAIN"]['GatheringProfitTable'] = 'False'
self.parser["MAIN"]['EndlessLoop'] = 'False'
self.parser.add_section('LOGGING')
self.parser['LOGGING']['LogEnable'] = 'True'
self.parser['LOGGING']['LogLevel'] = 'INFO'
self.parser['LOGGING']['LogMode'] = 'WRITE'
self.parser['LOGGING']['LogFile'] = 'ffxiv_market_calculator.log'
self.parser.add_section('DISCORD')
self.parser['DISCORD']['DiscordEnable'] = 'False'
self.parser['DISCORD']['DefaultMessageIds'] = '[123456789123456789,123456789123456789]'
self.parser['DISCORD']['NoCraftMessageIds'] = '[123456789123456789,123456789123456789]'
self.parser['DISCORD']['GatherableMessageIds'] = '[123456789123456789,123456789123456789]'
with open(self.configfile, 'w', encoding='utf-8') as configfile:
self.parser.write(configfile)
def parse_main_config(self):
"""
Retrieves main values from config file
"""
self.parser.read(self.configfile)
try:
self.config = {
"marketboard_type": self.parser["MAIN"].get(
'MarketboardType', 'World'
).capitalize(),
"datacentre": self.parser["MAIN"].get('Datacentre', 'Crystal').capitalize(),
"world": self.parser["MAIN"].get('World', 'Zalera').capitalize(),
"result_quantity": self.parser["MAIN"].getint('ResultQuantity', 50),
"update_quantity": self.parser["MAIN"].getint(
'UpdateQuantity', 0
),
"extra_tables": {
"display_without_craft_cost": self.parser["MAIN"].getboolean(
'DisplayWithoutCraftCost', False),
"gathering_profit_table": self.parser["MAIN"].getboolean(
'GatheringProfitTable', False)
},
"endless_loop": self.parser["MAIN"].getboolean('EndlessLoop', False)
}
except Exception as err:
self.ffxiv_logger.error("MAIN Config was invalid, setting back to defaults: %i", {err})
self.parser["MAIN"]['MarketboardType'] = 'World'.capitalize()
self.parser["MAIN"]['Datacentre'] = 'Crystal'.capitalize()
self.parser["MAIN"]['World'] = 'Zalera'.capitalize()
self.parser["MAIN"]['ResultQuantity'] = '50'
self.parser["MAIN"]['UpdateQuantity'] = '0'
self.parser["MAIN"]['DisplayWithoutCraftCost'] = 'False'
self.parser["MAIN"]['GatheringProfitTable'] = 'False'
self.parser["MAIN"]['EndlessLoop'] = 'False'
with open(self.configfile, 'w', encoding='utf-8') as configfile:
self.parser.write(configfile)
self.config = {
"marketboard_type": 'World',
"datacentre": 'Crystal',
"world": 'Zalera',
"result_quantity": 50,
"update_quantity": 0,
"extra_tables": {
"display_without_craft_cost": False,
"gathering_profit_table": False
}
}
self.main_validation()
self.ffxiv_logger.info("Main Config Loaded")
return self.config
def parse_logging_config(self):
"""
Retrieves logging values from config file
"""
self.ffxiv_logger.info("Loading Logging Config")
self.parser.read(self.configfile)
try:
self.config = {
"log_enable": self.parser["LOGGING"].getboolean('LogEnable', False),
"log_level": self.parser['LOGGING'].get('LogLevel', 'INFO'),
"log_mode": self.parser['LOGGING'].get('LogMode', 'Write'),
"log_file": self.parser['LOGGING'].get('LogFile', 'ffxiv_market_calculator.log'),
}
except Exception as err:
self.ffxiv_logger.error(
"LOGGING Config was invalid, setting back to defaults: %i", {err}
)
self.parser['LOGGING']['LogEnable'] = 'True'
self.parser['LOGGING']['LogLevel'] = 'INFO'
self.parser['LOGGING']['LogMode'] = 'WRITE'
self.parser['LOGGING']['LogFile'] = 'ffxiv_market_calculator.log'
with open(self.configfile, 'w', encoding='utf-8') as configfile:
self.parser.write(configfile)
self.config = {
"log_enable": False,
"log_level": 'INFO',
"log_mode": 'Write',
"log_file": 'ffxiv_market_calculator.log'
}
self.logging_validation()
return self.config
def parse_discord_config(self):
"""
Retrieves discord values from config file
"""
self.ffxiv_logger.info("Loading Discord Config")
self.parser.read(self.configfile)
try:
self.config = {
"discord_enable": self.parser["DISCORD"].getboolean('DiscordEnable', False),
"default_message_ids": ast.literal_eval(
self.parser['DISCORD'].get('DefaultMessageIds')),
"no_craft_message_ids": ast.literal_eval(
self.parser['DISCORD'].get('NoCraftMessageIds')),
"gatherable_message_ids": ast.literal_eval(
self.parser['DISCORD'].get('GatherableMessageIds'))
}
except Exception as err:
self.ffxiv_logger.error(
"DISCORD Config was invalid, setting back to defaults: %i", {err}
)
self.parser["DISCORD"]['DiscordEnable'] = 'False'
self.parser['DISCORD']['DefaultMessageIds'] = \
'[123456789123456789,123456789123456789]'
self.parser['DISCORD']['NoCraftMessageIds'] = \
'[123456789123456789,123456789123456789]'
self.parser['DISCORD']['GatherableMessageIds'] = \
'[123456789123456789,123456789123456789]'
with open(self.configfile, 'w', encoding='utf-8') as configfile:
self.parser.write(configfile)
self.config = {
"discord_enable": False,
"default_message_ids": [],
"no_craft_message_ids": [],
"gatherable_message_ids": []
}
self.discord_validation()
self.ffxiv_logger.info("Loaded Discord Config")
return self.config
def main_validation(self):
"""
Validates the main config values for correct values/types
"""
self.ffxiv_logger.info("Performing Main Config Validation")
datacentre_data = self.global_db.return_query('SELECT name FROM datacentre')
world_data = self.global_db.return_query('SELECT name FROM world')
valid_datacentres = valid_worlds = [] = []
for datacentre in datacentre_data:
valid_datacentres.append(datacentre[0])
for world in world_data:
valid_worlds.append(world[0])
type_check = all([
isinstance(self.config["result_quantity"], int),
isinstance(self.config["update_quantity"], int),
isinstance(self.config["extra_tables"]["display_without_craft_cost"], bool),
isinstance(self.config["extra_tables"]["gathering_profit_table"], bool)
])
value_check = all([
self.config["marketboard_type"] in ["World", "Datacentre", "Datacenter"],
self.config["datacentre"] in valid_datacentres,
self.config["world"] in valid_worlds,
self.config["result_quantity"] > 0
])
if not type_check and value_check:
self.ffxiv_logger.error(
"Main Config Validation FAILED on both Type and Value validations"
)
raise TypeError
if not type_check:
self.ffxiv_logger.error("Main Config Validation FAILED on Type validations")
raise TypeError
if not value_check:
self.ffxiv_logger.error("Main Config Validation FAILED on Value validations")
raise ValueError
self.ffxiv_logger.info("Main Config Validation Complete")
def logging_validation(self):
"""
Validates the logging config values for correct values/types
"""
self.ffxiv_logger.info("Performing Logging Config Validation")
type_check = all([
isinstance(self.config["log_level"], str),
isinstance(self.config["log_mode"], str),
isinstance(self.config["log_file"], str)
])
value_check = all([
self.config["log_level"] in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"],
self.config["log_mode"] in ["WRITE", "APPEND"]
])
if not isinstance(self.config["log_enable"], bool):
raise ValueError
if self.config["log_enable"] and not type_check and not value_check:
self.ffxiv_logger.error(
"Logging Config Validation FAILED on both Type and Value validations"
)
raise TypeError
if self.config["log_enable"] and not type_check:
self.ffxiv_logger.error("Logging Config Validation FAILED on Type validations")
raise TypeError
if self.config["log_enable"] and not value_check:
self.ffxiv_logger.error("Logging Config Validation FAILED on Value validations")
raise ValueError
self.ffxiv_logger.info("Logging Config Validation Complete")
def discord_validation(self):
"""
Validates the discord config values for correct values/types
"""
self.ffxiv_logger.info("Performing Discord Config Validation")
if not isinstance(self.config["discord_enable"], bool):
self.ffxiv_logger.error("Discord Config Validation FAILED on Type validation")
raise TypeError
self.ffxiv_logger.info("Discord Config Validation Complete")
def get_config_parser(self):
"""
Unsure, this could potentially be blown away as I cannot find a usage in project
"""
return self.parser.read(self.configfile), self.configfile