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:
parent
a3870f4f88
commit
e540fa09b2
4 changed files with 41 additions and 46 deletions
|
@ -15,4 +15,8 @@ token = 123456789:YOUR_TOKEN_GOES_HERE_______________
|
||||||
; A lower value reduces memory usage but can be inconvenient for the users
|
; A lower value reduces memory usage but can be inconvenient for the users
|
||||||
conversation_timeout = 7200
|
conversation_timeout = 7200
|
||||||
; Time to wait before sending another getUpdates request
|
; 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.
|
||||||
|
|
|
@ -2,41 +2,38 @@ import sys
|
||||||
import os
|
import os
|
||||||
import configparser
|
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.
|
||||||
# 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:
|
||||||
with open("config/template_config.ini") as template_file:
|
# Check if the config file exists
|
||||||
# Check if the config file exists
|
if not os.path.isfile("config/config.ini"):
|
||||||
if not os.path.isfile("config/config.ini"):
|
# Copy the template file to the config file
|
||||||
# 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
|
|
||||||
with open("config/config.ini", "w") as config_file:
|
with open("config/config.ini", "w") as config_file:
|
||||||
config.write(config_file)
|
config_file.write(template_file.read())
|
||||||
# Notify the user and quit
|
# Find the template version number
|
||||||
print("The config file in config/config.ini has been updated.\n"
|
config = configparser.ConfigParser()
|
||||||
"Edit it with the new required data, set the is_template flag to true and restart this script.")
|
config.read_file(template_file)
|
||||||
sys.exit(1)
|
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)
|
7
core.py
7
core.py
|
@ -8,11 +8,8 @@ import configloader
|
||||||
def main():
|
def main():
|
||||||
"""The core code of the program. Should be run only in the main process!"""
|
"""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
|
# Create a bot instance
|
||||||
bot = telegram.Bot(config["Telegram"]["token"])
|
bot = telegram.Bot(configloader.config["Telegram"]["token"])
|
||||||
|
|
||||||
# Test the specified token
|
# Test the specified token
|
||||||
try:
|
try:
|
||||||
|
@ -36,7 +33,7 @@ def main():
|
||||||
while True:
|
while True:
|
||||||
# Get a new batch of 100 updates and mark the last 100 parsed as read
|
# Get a new batch of 100 updates and mark the last 100 parsed as read
|
||||||
try:
|
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...
|
# If the method times out...
|
||||||
except telegram.error.TimedOut:
|
except telegram.error.TimedOut:
|
||||||
# Increase the TimedOut counter
|
# Increase the TimedOut counter
|
||||||
|
|
|
@ -5,9 +5,6 @@ import configloader
|
||||||
import sys
|
import sys
|
||||||
import queue as queuem
|
import queue as queuem
|
||||||
|
|
||||||
# Load the configuration
|
|
||||||
config = configloader.load_config()
|
|
||||||
|
|
||||||
class StopSignal:
|
class StopSignal:
|
||||||
"""A data class that should be sent to the worker when the conversation has to be stopped abnormally."""
|
"""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."""
|
If a stop signal is sent, try to gracefully stop the thread."""
|
||||||
# Pop data from the queue
|
# Pop data from the queue
|
||||||
try:
|
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:
|
except queuem.Empty:
|
||||||
# If the conversation times out, gracefully stop the thread
|
# If the conversation times out, gracefully stop the thread
|
||||||
self._graceful_stop()
|
self._graceful_stop()
|
||||||
|
|
Loading…
Reference in a new issue