From 32e86a3c0e02fc1810eaca19d9bf4aa0bb3aaab2 Mon Sep 17 00:00:00 2001 From: b3yond Date: Fri, 23 Mar 2018 11:05:24 +0100 Subject: [PATCH] ticketfrei can be imported now, takes care of loading config & logging --- db.py | 5 ++--- frontend/website.py | 5 ++--- mailbot.py | 6 ++---- retootbot.py | 6 ++---- retweetbot.py | 6 ++---- sendmail.py | 6 ++---- ticketfrei.py | 27 +++++++++++++++++++++------ trigger.py | 6 ++---- 8 files changed, 35 insertions(+), 32 deletions(-) diff --git a/db.py b/db.py index 9ace8fd..eb8a50d 100644 --- a/db.py +++ b/db.py @@ -5,7 +5,7 @@ import jwt from os import path, urandom from pylibscrypt import scrypt_mcf, scrypt_mcf_check import sqlite3 -import pytoml as toml +import ticketfrei from mastodon import Mastodon @@ -20,8 +20,7 @@ class DB(object): self.create() print("Initialized new sqlite database.") self.secret = urandom(32) - with open('config.toml') as configfile: - self.config = toml.load(configfile) + self.config = ticketfrei.get_config() def create(self): # init db diff --git a/frontend/website.py b/frontend/website.py index b997955..8ee1d0a 100644 --- a/frontend/website.py +++ b/frontend/website.py @@ -5,7 +5,7 @@ import base64 import bottle import sqlite3 import sendmail -import pytoml as toml +import ticketfrei import jwt import pylibscrypt import smtplib @@ -345,8 +345,7 @@ class StripPathMiddleware(object): if __name__ == "__main__": global config - with open('../config.toml') as configfile: - config = toml.load(configfile) + config = ticketfrei.get_config() global db global secret diff --git a/mailbot.py b/mailbot.py index d74e3b8..64e2644 100644 --- a/mailbot.py +++ b/mailbot.py @@ -7,7 +7,6 @@ import trigger import datetime import email import logging -import pytoml as toml import imaplib import report @@ -173,9 +172,8 @@ class Mailbot(object): if __name__ == "__main__": - # read config in TOML format (https://github.com/toml-lang/toml#toml) - with open('config.toml') as configfile: - config = toml.load(configfile) + import ticketfrei + config = ticketfrei.get_config() # set log file logger = logging.getLogger() diff --git a/retootbot.py b/retootbot.py index b545cef..85a3bae 100755 --- a/retootbot.py +++ b/retootbot.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import pytoml as toml import mastodon import os import pickle @@ -129,9 +128,8 @@ class RetootBot(object): if __name__ == '__main__': - # read config in TOML format (https://github.com/toml-lang/toml#toml) - with open('config.toml') as configfile: - config = toml.load(configfile) + import ticketfrei + config = ticketfrei.get_config() fh = logging.FileHandler(config['logging']['logpath']) fh.setLevel(logging.DEBUG) diff --git a/retweetbot.py b/retweetbot.py index e196ca6..77c63b2 100755 --- a/retweetbot.py +++ b/retweetbot.py @@ -3,7 +3,6 @@ import tweepy import re import requests -import pytoml as toml import trigger from time import sleep import report @@ -203,9 +202,8 @@ class RetweetBot(object): if __name__ == "__main__": - # get the config dict of dicts - with open('config.toml') as configfile: - config = toml.load(configfile) + import ticketfrei + config = ticketfrei.get_config() # set log file fh = logging.FileHandler(config['logging']['logpath']) diff --git a/sendmail.py b/sendmail.py index 2b06afe..f758a08 100755 --- a/sendmail.py +++ b/sendmail.py @@ -2,7 +2,6 @@ import smtplib import ssl -import pytoml as toml from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart @@ -67,9 +66,8 @@ class Mailer(object): # For testing: if __name__ == '__main__': - # read config in TOML format (https://github.com/toml-lang/toml#toml) - with open('config.toml') as configfile: - config = toml.load(configfile) + import ticketfrei + config = ticketfrei.get_config() m = Mailer(config) print(m.send("This is a test mail.", m.fromaddr, "Test")) diff --git a/ticketfrei.py b/ticketfrei.py index f9f33ba..b2b5c15 100755 --- a/ticketfrei.py +++ b/ticketfrei.py @@ -10,21 +10,32 @@ from retweetbot import RetweetBot from mailbot import Mailbot from trigger import Trigger -if __name__ == '__main__': +def set_logfile(config): + logfile = config['logging']['logpath'] + logger = logging.getLogger() + fh = logging.FileHandler(logfile) + fh.setLevel(logging.DEBUG) + logger.addHandler(fh) + return logger + +def get_config(): # read config in TOML format (https://github.com/toml-lang/toml#toml) with open('config.toml') as configfile: config = toml.load(configfile) + return config + +def run(): + # get config + config = get_config() # set log file - logger = logging.getLogger() - fh = logging.FileHandler(config['logging']['logpath']) - fh.setLevel(logging.DEBUG) - logger.addHandler(fh) + logger = set_logfile(config) + # set trigger trigger = Trigger(config) + # initialize bots bots = [] - if config["muser"]["enabled"] != "false": bots.append(RetootBot(config)) if config["tuser"]["enabled"] != "false": @@ -59,3 +70,7 @@ if __name__ == '__main__': attachment=config['logging']['logpath']) except: logger.error('Mail sending failed', exc_info=True) + + +if __name__ == '__main__': + run() \ No newline at end of file diff --git a/trigger.py b/trigger.py index 864256c..366999d 100644 --- a/trigger.py +++ b/trigger.py @@ -1,9 +1,7 @@ #!/usr/bin/env python -import pytoml as toml import os import re - class Trigger(object): """ This class provides a filter to test a string against. @@ -59,8 +57,8 @@ class Trigger(object): if __name__ == "__main__": - with open("config.toml", "r") as configfile: - config = toml.load(configfile) + import ticketfrei + config = ticketfrei.get_config() print("testing the trigger") trigger = Trigger(config)