Browse Source

added comman line argument for settings to be used. can set this to a folder name inside options.

"python app.py pvecw" would look for settings in "bot/options/pvecw"
testing
Christian Voigt 3 months ago
parent
commit
a303b21853
2 changed files with 39 additions and 7 deletions
  1. +3
    -1
      app.py
  2. +36
    -6
      bot/__init__.py

+ 3
- 1
app.py View File

@ -2,6 +2,8 @@
""" Lasciate ogni speranza voi ch’entrate """
from bot import chrani_bot
chrani_bot.run()
if chrani_bot is not None:
chrani_bot.run()
# TODO: make action have a queue limit, working off of a queue

+ 36
- 6
bot/__init__.py View File

@ -10,6 +10,8 @@ import time
import traceback
from collections import deque
import logging
from sys import argv
from datetime import datetime
class ChraniBot:
@ -17,6 +19,7 @@ class ChraniBot:
just import chrani_bot and .run() it
"""
root_dir = str
server_identifier = str
bot_is_ready_to_roll = False
@ -31,10 +34,15 @@ class ChraniBot:
DEBUG = True
def __init__(self):
def __init__(self, server_identifier=None):
self.root_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(self.root_dir)
if server_identifier is None:
self.server_identifier = 'default'
else:
self.server_identifier = server_identifier
self.loaded_modules_dict = {} # this will be populated by the imports done next:
self.bot_is_ready_to_roll = False
@ -84,7 +92,9 @@ class ChraniBot:
try:
return func(*args, **kwargs)
except Exception as e:
self.write_log(str(e) + traceback.print_exc())
log_line = str(e) + traceback.print_exc()
self.write_log(log_line)
print(log_line)
return None
return func_wrapper
@ -92,7 +102,7 @@ class ChraniBot:
def setup_modules(self):
def get_options_dict(module_name):
try:
options_dir = "{}/{}".format(self.root_dir, "options")
options_dir = "{}/{}/{}".format(self.root_dir, "options", self.server_identifier)
with open(os.path.join(options_dir, module_name + ".json")) as open_file:
return json.load(open_file)
except FileNotFoundError:
@ -145,11 +155,15 @@ class ChraniBot:
import_module("bot.modules." + module)
def setup_logging(self):
self.log_filename = self.root_dir + os.path.sep + 'debug.log'
date_format = '%Y%m%d_%H%M%S'
today = datetime.now()
self.log_filename = self.root_dir + os.path.sep + "debug_{}.log".format(
today.strftime(date_format)
)
self.logger = logging.getLogger("debug")
logging.basicConfig(
format='%(asctime)s %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
datefmt=date_format,
filename=self.log_filename,
level=logging.DEBUG,
filemode='w'
@ -179,4 +193,20 @@ class ChraniBot:
time.sleep(1)
chrani_bot = ChraniBot()
chrani_bot = None
try:
server_identifier = str(argv[1])
except IndexError:
print("no command line argument, using default settings")
chrani_bot = ChraniBot("default")
except ValueError:
print("argument must be a string")
else:
# argument seems to be a string , let's check it for sanity
if 0 < len(server_identifier) < 64:
print("using settings from '{}'".format(
server_identifier
))
chrani_bot = ChraniBot(server_identifier)
else:
print("command line argument seems a tad long. must be shorter than 64 chars!")

Loading…
Cancel
Save