Geoffrey-Django/geoffrey/BotConfig.py

86 lines
2.8 KiB
Python
Raw Normal View History

import codecs
2018-08-12 19:00:04 +00:00
import configparser
2018-07-21 18:28:31 +00:00
def create_config(config, path):
2018-08-11 21:07:33 +00:00
config['Discord'] = {'Token': '',
'Status': '',
'Prefix': '?',
'Bot_Mod': '',
'Error_Users': ''
2018-08-11 21:07:33 +00:00
}
2018-09-13 17:46:26 +00:00
config['SQL'] = {'Dialect+Driver': 'mysql+pymysql',
2018-08-11 21:07:33 +00:00
'Username': '',
'Password': '',
'Host': '',
'Port': '',
'Database': ''
}
config['Minecraft'] = {'Dynmap_Url': '',
'World_Name': '',
2018-08-11 21:07:33 +00:00
}
config['Logging'] = {'Log_Path': '',
'Count': '7',
'Rotation_Duration': '1'
2018-08-11 21:07:33 +00:00
}
config['Special Names'] = {}
2018-08-11 21:07:33 +00:00
with open(path, 'w') as configfile:
2018-08-11 21:07:33 +00:00
config.write(configfile)
def read_config(path):
2018-08-11 21:07:33 +00:00
config = configparser.ConfigParser()
try:
file = codecs.open(path, "r", "utf8")
config.read_file(file)
file.close()
except FileNotFoundError:
create_config(config, path)
print("Config generated.")
2018-08-11 21:07:33 +00:00
quit(0)
return config
class Config:
def __init__(self, path):
try:
self.config = read_config(path)
self.engine_args = self.read_engine_arg()
self.token = self.config['Discord']['Token']
self.status = self.config['Discord']['Status']
self.prefix = self.config['Discord']['Prefix']
self.bot_mod = self.config['Discord']['Bot_Mod'].split(',')
self.error_users = self.config['Discord']['Error_Users'].split(',')
self.world_name = self.config['Minecraft']['World_Name']
self.dynmap_url = self.config['Minecraft']['Dynmap_Url']
self.log_path = self.config['Logging']['log_path']
2018-08-11 21:07:33 +00:00
self.count = int(self.config['Logging']['Count'])
self.rotation_duration = int(self.config['Logging']['Rotation_Duration'])
self.special_name_list = dict(self.config.items('Special Names'))
2018-08-11 21:07:33 +00:00
except Exception as e:
print("Invalid config file, missing {}.".format(e))
quit(1)
def read_engine_arg(self):
driver = self.config['SQL']['Dialect+Driver']
2018-08-11 21:07:33 +00:00
username = self.config['SQL']['Username']
password = self.config['SQL']['Password']
host = self.config['SQL']['Host']
port = self.config['SQL']['Port']
database_name = self.config['SQL']['Database']
engine_args = '{}://{}:{}@{}:{}/{}?charset=utf8mb4&use_unicode=1'
return engine_args.format(driver, username, password, host, port, database_name)
def get_config(config_path):
return Config(config_path)