Huge cleanup and refactoring :>

This commit is contained in:
mrhanky
2017-08-22 17:43:48 +02:00
parent 7f5571fc09
commit f33a17038a
21 changed files with 127 additions and 185 deletions

View File

@@ -1,4 +1,56 @@
# -*- coding: utf-8 -*-
import os
import json
import logging
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
import irc3
from dotenv import load_dotenv
MODULE = __name__
# TODO: ddg
class Bot(irc3.IrcBot):
DEV = 'BOT_DEV' in os.environ
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = REPO_DIR if DEV else os.path.dirname(REPO_DIR)
@classmethod
def from_json(cls, cfg_file: str = 'config.json', env_file: str = '.env'):
cfg_file = os.path.join(cls.CONFIG_DIR, cfg_file)
env_file = os.path.join(cls.CONFIG_DIR, env_file)
load_dotenv('.env')
with open(cfg_file, 'r') as fp:
conf = json.load(fp)
return cls.from_config(conf)
@irc3.plugin
class BasePlugin:
requires = ['irc3.plugins.command']
def __init__(self, bot: Bot):
self.bot = bot
self.log = logging.getLogger('irc3.{}'.format(self.__class__.__name__.lower()))
class Plugin(BasePlugin):
@classmethod
def reload(cls, old: BasePlugin):
return cls(old.bot)
from .storage import Storage # noqa
class DatabasePlugin(Plugin):
requires = Plugin.requires + ['bot.storage']
def __init__(self, bot: Bot):
super().__init__(bot)
self.db = bot.get_plugin(Storage)
self.con = self.db.con
self.cur = self.db.cur