From 2d0631bb727020046adf78eaca1e9a78058e9301 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 7 Dec 2017 12:06:34 +0100 Subject: [PATCH] Add config version handling --- core.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/core.py b/core.py index 43947e5..02e42b7 100644 --- a/core.py +++ b/core.py @@ -2,22 +2,40 @@ import os import sys import configparser -# Check if a configuration file exists and create one if it doesn't -if not os.path.isfile("config/config.ini"): - # Copy the template file to the config file - with open("config/template_config.ini") as template_file, open("config/config.ini", "w") as config_file: - config_file.write(template_file.read()) +# Check if a configuration file exists, create one if it doesn't and get the template version number. +with open("config/template_config.ini") as template_file: + # Check if the config file exists + if not os.path.isfile("config/config.ini"): + # Copy the template file to the config file + with open("config/config.ini", "w") as config_file: + config_file.write(template_file.read()) + # Find the template version number + config = configparser.ConfigParser() + config.read_file(template_file) + template_version = int(config["Config"]["version"]) -# Create a configparser -config = configparser.ConfigParser() -# Read the config file into the configparser +# Overwrite the template config with the values in the config with open("config/config.ini") as config_file: config.read_file(config_file) -# Check if the file has been edited correctly +# Check if the file has been edited if config["Config"]["is_template"] == "yes": - print("A config file has been created in config/config.ini.") - print("Edit it with your configuration, set the is_template flag to false and restart this script.") + print("A config file has been created in config/config.ini.\n" + "Edit it with your configuration, set the is_template flag to false and restart this script.") + sys.exit(1) + +# Check if the version has changed from the template +if template_version > int(config["Config"]["version"]): + # Reset the is_template flag + config["Config"]["is_template"] = "yes" + # Update the config version + config["Config"]["version"] = str(template_version) + # Save the file + with open("config/config.ini", "w") as config_file: + config.write(config_file) + # Notify the user and quit + print("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.") sys.exit(1) print("Program goes here") \ No newline at end of file