2018-08-11 23:02:50 +00:00
|
|
|
import codecs
|
2018-08-12 19:00:04 +00:00
|
|
|
import configparser
|
2018-07-21 18:28:31 +00:00
|
|
|
|
|
|
|
|
2018-08-29 15:08:21 +00:00
|
|
|
def create_config(config, path):
|
2018-08-11 21:07:33 +00:00
|
|
|
config['Discord'] = {'Token': '',
|
|
|
|
'Status': '',
|
|
|
|
'Prefix': '?',
|
2018-08-29 15:08:21 +00:00
|
|
|
'Bot_Mod': '',
|
|
|
|
'Error_Users': ''
|
2018-08-11 21:07:33 +00:00
|
|
|
}
|
|
|
|
config['SQL'] = {'Dialect+Driver': 'mysql+mysqldb',
|
|
|
|
'Username': '',
|
|
|
|
'Password': '',
|
|
|
|
'Host': '',
|
|
|
|
'Port': '',
|
|
|
|
'Database': ''
|
|
|
|
}
|
|
|
|
config['Minecraft'] = {'Dynmap_Url': '',
|
2018-08-25 17:30:53 +00:00
|
|
|
'World_Name': '',
|
|
|
|
'North_Tunnel': 'North',
|
|
|
|
"East_Tunnel": 'South',
|
|
|
|
"South_Tunnel": 'East',
|
|
|
|
"West_Tunnel": 'West'
|
2018-08-11 21:07:33 +00:00
|
|
|
}
|
2018-08-25 17:30:53 +00:00
|
|
|
config['Logging'] = {'Count': '7',
|
|
|
|
'Rotation_Duration': '1'
|
2018-08-11 21:07:33 +00:00
|
|
|
}
|
2018-08-12 00:11:35 +00:00
|
|
|
config['Special Names'] = {}
|
2018-08-11 21:07:33 +00:00
|
|
|
|
2018-08-29 21:10:35 +00:00
|
|
|
with open(path, 'w') as configfile:
|
2018-08-11 21:07:33 +00:00
|
|
|
config.write(configfile)
|
|
|
|
|
|
|
|
|
2018-08-29 21:10:35 +00:00
|
|
|
def read_config(path):
|
2018-08-11 21:07:33 +00:00
|
|
|
config = configparser.ConfigParser()
|
2018-08-30 13:58:54 +00:00
|
|
|
try:
|
|
|
|
config.read_file(codecs.open(path, "r", "utf8"))
|
|
|
|
except FileNotFoundError:
|
2018-08-29 15:08:21 +00:00
|
|
|
create_config(config, path)
|
2018-08-29 21:10:35 +00:00
|
|
|
print("Config generated.")
|
2018-08-11 21:07:33 +00:00
|
|
|
quit(0)
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
class Config:
|
|
|
|
|
2018-08-29 21:10:35 +00:00
|
|
|
def __init__(self, path):
|
2018-08-10 02:25:55 +00:00
|
|
|
try:
|
2018-08-29 21:10:35 +00:00
|
|
|
self.config = read_config(path)
|
2018-08-10 02:25:55 +00:00
|
|
|
self.engine_args = self.read_engine_arg()
|
2018-08-25 17:30:53 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.token = self.config['Discord']['Token']
|
|
|
|
self.world_name = self.config['Minecraft']['World_Name']
|
|
|
|
self.status = self.config['Discord']['Status']
|
|
|
|
self.prefix = self.config['Discord']['Prefix']
|
2018-08-14 23:56:07 +00:00
|
|
|
self.bot_mod = self.config['Discord']['Bot_Mod'].split(',')
|
2018-08-19 16:37:59 +00:00
|
|
|
self.error_users = self.config['Discord']['Error_Users'].split(',')
|
2018-08-25 17:30:53 +00:00
|
|
|
|
|
|
|
self.dynmap_url = self.config['Minecraft']['Dynmap_Url']
|
|
|
|
self.north_tunnel = self.config['Minecraft']['North_Tunnel']
|
|
|
|
self.east_tunnel = self.config['Minecraft']['East_Tunnel']
|
|
|
|
self.south_tunnel = self.config['Minecraft']['South_Tunnel']
|
|
|
|
self.west_tunnel = self.config['Minecraft']['West_Tunnel']
|
|
|
|
|
2018-08-11 21:07:33 +00:00
|
|
|
self.count = int(self.config['Logging']['Count'])
|
|
|
|
self.rotation_duration = int(self.config['Logging']['Rotation_Duration'])
|
2018-08-11 23:02:50 +00:00
|
|
|
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))
|
2018-08-10 02:25:55 +00:00
|
|
|
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']
|
2018-08-10 02:25:55 +00:00
|
|
|
|
|
|
|
engine_args = '{}://{}:{}@{}:{}/{}?charset=utf8mb4&use_unicode=1'
|
|
|
|
|
|
|
|
return engine_args.format(driver, username, password, host, port, database_name)
|
|
|
|
|
2018-08-29 18:37:06 +00:00
|
|
|
|
2018-08-29 21:10:35 +00:00
|
|
|
def get_config(config_path):
|
|
|
|
return Config(config_path)
|
|
|
|
|