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
|
@ -16,3 +16,7 @@ token = 123456789:YOUR_TOKEN_GOES_HERE_______________
|
|||
conversation_timeout = 7200
|
||||
; Time to wait before sending another getUpdates request
|
||||
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,7 +2,6 @@ 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
|
||||
|
@ -38,5 +37,3 @@ def load_config() -> configparser.ConfigParser:
|
|||
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)
|
||||
|
||||
return config
|
7
core.py
7
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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue