From 609129556389d983d159bb114e300828f30ae7b1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 6 Dec 2017 15:40:12 +0100 Subject: [PATCH] Add config code --- .gitignore | 2 ++ config/template_config.ini | 13 +++++++++++++ core.py | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 config/template_config.ini create mode 100644 core.py diff --git a/.gitignore b/.gitignore index df61408..2e977ac 100644 --- a/.gitignore +++ b/.gitignore @@ -100,3 +100,5 @@ ENV/ # mypy .mypy_cache/ .idea/ + +config/config.ini \ No newline at end of file diff --git a/config/template_config.ini b/config/template_config.ini new file mode 100644 index 0000000..53ec169 --- /dev/null +++ b/config/template_config.ini @@ -0,0 +1,13 @@ +# greed configuration file + +# Config file parameters +[Config] +; Config file version. DO NOT EDIT THIS! +version = 1 +; Set this to no when you are done editing the file +is_template = yes + +# Telegram bot parameters +[Telegram] +; Your bot token goes here. Get one from @BotFather! +token = 123456789:YOUR_TOKEN_GOES_HERE_______________ \ No newline at end of file diff --git a/core.py b/core.py new file mode 100644 index 0000000..43947e5 --- /dev/null +++ b/core.py @@ -0,0 +1,23 @@ +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()) + +# Create a configparser +config = configparser.ConfigParser() +# Read the config file into the configparser +with open("config/config.ini") as config_file: + config.read_file(config_file) + +# Check if the file has been edited correctly +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.") + sys.exit(1) + +print("Program goes here") \ No newline at end of file