ticketfrei can be imported now, takes care of loading config & logging

This commit is contained in:
b3yond 2018-03-23 11:05:24 +01:00
parent b9613a60de
commit 32e86a3c0e
8 changed files with 35 additions and 32 deletions

5
db.py
View file

@ -5,7 +5,7 @@ import jwt
from os import path, urandom from os import path, urandom
from pylibscrypt import scrypt_mcf, scrypt_mcf_check from pylibscrypt import scrypt_mcf, scrypt_mcf_check
import sqlite3 import sqlite3
import pytoml as toml import ticketfrei
from mastodon import Mastodon from mastodon import Mastodon
@ -20,8 +20,7 @@ class DB(object):
self.create() self.create()
print("Initialized new sqlite database.") print("Initialized new sqlite database.")
self.secret = urandom(32) self.secret = urandom(32)
with open('config.toml') as configfile: self.config = ticketfrei.get_config()
self.config = toml.load(configfile)
def create(self): def create(self):
# init db # init db

View file

@ -5,7 +5,7 @@ import base64
import bottle import bottle
import sqlite3 import sqlite3
import sendmail import sendmail
import pytoml as toml import ticketfrei
import jwt import jwt
import pylibscrypt import pylibscrypt
import smtplib import smtplib
@ -345,8 +345,7 @@ class StripPathMiddleware(object):
if __name__ == "__main__": if __name__ == "__main__":
global config global config
with open('../config.toml') as configfile: config = ticketfrei.get_config()
config = toml.load(configfile)
global db global db
global secret global secret

View file

@ -7,7 +7,6 @@ import trigger
import datetime import datetime
import email import email
import logging import logging
import pytoml as toml
import imaplib import imaplib
import report import report
@ -173,9 +172,8 @@ class Mailbot(object):
if __name__ == "__main__": if __name__ == "__main__":
# read config in TOML format (https://github.com/toml-lang/toml#toml) import ticketfrei
with open('config.toml') as configfile: config = ticketfrei.get_config()
config = toml.load(configfile)
# set log file # set log file
logger = logging.getLogger() logger = logging.getLogger()

View file

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytoml as toml
import mastodon import mastodon
import os import os
import pickle import pickle
@ -129,9 +128,8 @@ class RetootBot(object):
if __name__ == '__main__': if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml) import ticketfrei
with open('config.toml') as configfile: config = ticketfrei.get_config()
config = toml.load(configfile)
fh = logging.FileHandler(config['logging']['logpath']) fh = logging.FileHandler(config['logging']['logpath'])
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)

View file

@ -3,7 +3,6 @@
import tweepy import tweepy
import re import re
import requests import requests
import pytoml as toml
import trigger import trigger
from time import sleep from time import sleep
import report import report
@ -203,9 +202,8 @@ class RetweetBot(object):
if __name__ == "__main__": if __name__ == "__main__":
# get the config dict of dicts import ticketfrei
with open('config.toml') as configfile: config = ticketfrei.get_config()
config = toml.load(configfile)
# set log file # set log file
fh = logging.FileHandler(config['logging']['logpath']) fh = logging.FileHandler(config['logging']['logpath'])

View file

@ -2,7 +2,6 @@
import smtplib import smtplib
import ssl import ssl
import pytoml as toml
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
@ -67,9 +66,8 @@ class Mailer(object):
# For testing: # For testing:
if __name__ == '__main__': if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml) import ticketfrei
with open('config.toml') as configfile: config = ticketfrei.get_config()
config = toml.load(configfile)
m = Mailer(config) m = Mailer(config)
print(m.send("This is a test mail.", m.fromaddr, "Test")) print(m.send("This is a test mail.", m.fromaddr, "Test"))

View file

@ -10,21 +10,32 @@ from retweetbot import RetweetBot
from mailbot import Mailbot from mailbot import Mailbot
from trigger import Trigger 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) # read config in TOML format (https://github.com/toml-lang/toml#toml)
with open('config.toml') as configfile: with open('config.toml') as configfile:
config = toml.load(configfile) config = toml.load(configfile)
return config
def run():
# get config
config = get_config()
# set log file # set log file
logger = logging.getLogger() logger = set_logfile(config)
fh = logging.FileHandler(config['logging']['logpath'])
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# set trigger
trigger = Trigger(config) trigger = Trigger(config)
# initialize bots
bots = [] bots = []
if config["muser"]["enabled"] != "false": if config["muser"]["enabled"] != "false":
bots.append(RetootBot(config)) bots.append(RetootBot(config))
if config["tuser"]["enabled"] != "false": if config["tuser"]["enabled"] != "false":
@ -59,3 +70,7 @@ if __name__ == '__main__':
attachment=config['logging']['logpath']) attachment=config['logging']['logpath'])
except: except:
logger.error('Mail sending failed', exc_info=True) logger.error('Mail sending failed', exc_info=True)
if __name__ == '__main__':
run()

View file

@ -1,9 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import pytoml as toml
import os import os
import re import re
class Trigger(object): class Trigger(object):
""" """
This class provides a filter to test a string against. This class provides a filter to test a string against.
@ -59,8 +57,8 @@ class Trigger(object):
if __name__ == "__main__": if __name__ == "__main__":
with open("config.toml", "r") as configfile: import ticketfrei
config = toml.load(configfile) config = ticketfrei.get_config()
print("testing the trigger") print("testing the trigger")
trigger = Trigger(config) trigger = Trigger(config)