From e540fa09b21ef9833abf815616d43dd8e8e645ca Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 17 Dec 2017 16:49:46 +0100 Subject: [PATCH] Load config by importing configloader --- config/template_config.ini | 6 +++- configloader.py | 69 ++++++++++++++++++-------------------- core.py | 7 ++-- worker.py | 5 +-- 4 files changed, 41 insertions(+), 46 deletions(-) diff --git a/config/template_config.ini b/config/template_config.ini index 7c3914f..5b045a4 100644 --- a/config/template_config.ini +++ b/config/template_config.ini @@ -15,4 +15,8 @@ token = 123456789:YOUR_TOKEN_GOES_HERE_______________ ; A lower value reduces memory usage but can be inconvenient for the users conversation_timeout = 7200 ; Time to wait before sending another getUpdates request -long_polling_timeout = 30 \ No newline at end of file +long_polling_timeout = 30 + +# Database parameters +[Database] +; The database engine you want to use. Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html for the possible settings. diff --git a/configloader.py b/configloader.py index 0893add..addd001 100644 --- a/configloader.py +++ b/configloader.py @@ -2,41 +2,38 @@ import sys import os import configparser -def load_config() -> configparser.ConfigParser: - # 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"]) - - # 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 - if config["Config"]["is_template"] == "yes": - 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 +# 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.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) + 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"]) - return config \ No newline at end of file +# 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 +if config["Config"]["is_template"] == "yes": + 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) \ No newline at end of file diff --git a/core.py b/core.py index d759055..0a5ee22 100644 --- a/core.py +++ b/core.py @@ -8,11 +8,8 @@ import configloader def main(): """The core code of the program. Should be run only in the main process!""" - # Load the config from config.ini - config = configloader.load_config() - # Create a bot instance - bot = telegram.Bot(config["Telegram"]["token"]) + bot = telegram.Bot(configloader.config["Telegram"]["token"]) # Test the specified token try: @@ -36,7 +33,7 @@ def main(): while True: # Get a new batch of 100 updates and mark the last 100 parsed as read try: - updates = bot.get_updates(offset=next_update, timeout=int(config["Telegram"]["long_polling_timeout"])) + updates = bot.get_updates(offset=next_update, timeout=int(configloader.config["Telegram"]["long_polling_timeout"])) # If the method times out... except telegram.error.TimedOut: # Increase the TimedOut counter diff --git a/worker.py b/worker.py index 8c0f588..9a98537 100644 --- a/worker.py +++ b/worker.py @@ -5,9 +5,6 @@ import configloader import sys import queue as queuem -# Load the configuration -config = configloader.load_config() - class StopSignal: """A data class that should be sent to the worker when the conversation has to be stopped abnormally.""" @@ -51,7 +48,7 @@ class ChatWorker(threading.Thread): If a stop signal is sent, try to gracefully stop the thread.""" # Pop data from the queue try: - data = self.queue.get(timeout=int(config["Telegram"]["conversation_timeout"])) + data = self.queue.get(timeout=int(configloader.config["Telegram"]["conversation_timeout"])) except queuem.Empty: # If the conversation times out, gracefully stop the thread self._graceful_stop()