2017-12-13 10:20:53 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import configparser
|
2020-04-23 15:20:51 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
# Logs won't show up for this file as it is imported before logging is configured
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2017-12-13 10:20:53 +00:00
|
|
|
|
2019-09-02 14:05:37 +00:00
|
|
|
# Check if the config file exists, and create one if it doesn't
|
|
|
|
if not os.path.isfile("config/config.ini"):
|
2020-04-23 15:20:51 +00:00
|
|
|
log.debug("Creating config.ini from template_config.ini")
|
2019-09-02 14:05:37 +00:00
|
|
|
# Open the template file and create the config file
|
2020-04-07 00:22:47 +00:00
|
|
|
with open("config/template_config.ini", encoding="utf8") as template_file, \
|
|
|
|
open("config/config.ini", "w", encoding="utf8") as config_file:
|
2017-12-17 15:49:46 +00:00
|
|
|
# Copy the template file to the config file
|
2019-09-02 14:05:37 +00:00
|
|
|
config_file.write(template_file.read())
|
|
|
|
|
2020-04-07 00:22:47 +00:00
|
|
|
with open("config/template_config.ini", encoding="utf8") as template_file:
|
2017-12-17 15:49:46 +00:00
|
|
|
# Find the template version number
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read_file(template_file)
|
|
|
|
template_version = int(config["Config"]["version"])
|
2020-04-23 15:20:51 +00:00
|
|
|
log.debug(f"Template is version {template_version}")
|
2017-12-13 10:20:53 +00:00
|
|
|
|
2017-12-17 15:49:46 +00:00
|
|
|
# Overwrite the template config with the values in the config
|
2020-04-07 00:22:47 +00:00
|
|
|
with open("config/config.ini", encoding="utf8") as config_file:
|
2017-12-17 15:49:46 +00:00
|
|
|
config.read_file(config_file)
|
2020-04-23 15:20:51 +00:00
|
|
|
config_version = int(config["Config"]["version"])
|
|
|
|
log.debug(f"Config is version {template_version}")
|
2017-12-13 10:20:53 +00:00
|
|
|
|
2017-12-17 15:49:46 +00:00
|
|
|
# Check if the file has been edited
|
|
|
|
if config["Config"]["is_template"] == "yes":
|
2020-04-23 15:20:51 +00:00
|
|
|
log.debug("Config is a template, aborting...")
|
|
|
|
log.fatal("A config file has been created in config/config.ini.\n"
|
|
|
|
"Edit it with your configuration, set the is_template flag to 'no' and restart this script.")
|
2017-12-17 15:49:46 +00:00
|
|
|
sys.exit(1)
|
2017-12-13 10:20:53 +00:00
|
|
|
|
2017-12-17 15:49:46 +00:00
|
|
|
# Check if the version has changed from the template
|
2020-04-23 15:20:51 +00:00
|
|
|
if template_version > config_version:
|
|
|
|
log.debug("Config is older than Template, trying to merge...")
|
2017-12-17 15:49:46 +00:00
|
|
|
# Reset the is_template flag
|
|
|
|
config["Config"]["is_template"] = "yes"
|
|
|
|
# Update the config version
|
|
|
|
config["Config"]["version"] = str(template_version)
|
|
|
|
# Save the file
|
2020-04-07 00:22:47 +00:00
|
|
|
with open("config/config.ini", "w", encoding="utf8") as config_file:
|
2020-04-23 15:20:51 +00:00
|
|
|
log.debug("Writing merged config file...")
|
2017-12-17 15:49:46 +00:00
|
|
|
config.write(config_file)
|
|
|
|
# Notify the user and quit
|
2020-04-23 15:20:51 +00:00
|
|
|
log.debug("Config is now a template, aborting...")
|
|
|
|
log.fatal("The config file in config/config.ini has been updated.\n"
|
|
|
|
"Edit it with the new required data, set the is_template flag to true and restart this script.")
|
2018-04-09 10:24:55 +00:00
|
|
|
sys.exit(1)
|