1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-24 23:04:18 +00:00

Load config by importing configloader

This commit is contained in:
Steffo 2017-12-17 16:49:46 +01:00
parent a3870f4f88
commit e540fa09b2
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D
4 changed files with 41 additions and 46 deletions

View file

@ -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
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.

View file

@ -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
# 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)

View file

@ -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

View file

@ -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()