From d74f29fefbf41ef7e8525a3b4da92a28db2fbe51 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 26 Dec 2019 13:05:45 +0100 Subject: [PATCH] Setup modules load and unload system --- TelegramEDT/__init__.py | 39 ++++++-------------------- TelegramEDT/await_cmd.py | 15 ++++++++-- TelegramEDT/basic.py | 14 +++++++++- TelegramEDT/edt.py | 20 +++++++++++++- TelegramEDT/kfet.py | 14 +++++++++- TelegramEDT/modules.py | 59 ++++++++++++++++++++++++++++++++++++++++ TelegramEDT/notif.py | 14 +++++++++- TelegramEDT/tomuss.py | 12 +++++++- TelegramEDT/tools.py | 20 +++++++++++++- main.py | 2 +- 10 files changed, 169 insertions(+), 40 deletions(-) create mode 100644 TelegramEDT/modules.py diff --git a/TelegramEDT/__init__.py b/TelegramEDT/__init__.py index eb45c08..f2cd541 100644 --- a/TelegramEDT/__init__.py +++ b/TelegramEDT/__init__.py @@ -5,7 +5,7 @@ from os.path import isdir, isfile from threading import RLock from aiogram import Bot, Dispatcher, types -from aiogram.types import reply_keyboard, ContentType +from aiogram.types import reply_keyboard from aiogram.utils.callback_data import CallbackData from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -64,34 +64,11 @@ def check_id(user: types.User): session.commit() -from TelegramEDT.basic import start, help_cmd -dp.register_message_handler(start, commands="start") -dp.register_message_handler(help_cmd, commands="help") +from TelegramEDT.modules import load_module, load_cmd, unload_cmd +dp.register_message_handler(load_cmd, commands="load") +dp.register_message_handler(unload_cmd, commands="unload") -from TelegramEDT.edt import edt_cmd, edt_query, inline_edt, edt_await, edt_geturl -dp.register_message_handler(edt_cmd, lambda msg: msg.text.lower() == "edt") -dp.register_inline_handler(inline_edt) -dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"])) -dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt") -dp.register_message_handler(edt_geturl, commands="getedt") - -from TelegramEDT.kfet import kfet, kfet_set -dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet") -dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet") - -from TelegramEDT.tomuss import settomuss -dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss") - -from TelegramEDT.notif import notif, notif_cmd, notif_query -dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif") -dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"])) - -from TelegramEDT.await_cmd import await_cmd, have_await_cmd -dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT, ContentType.PHOTO]) - -from TelegramEDT.tools import get_id, get_logs, get_db, eval_cmd, errors -dp.register_message_handler(get_id, commands="getid") -dp.register_message_handler(get_logs, commands="getlogs") -dp.register_message_handler(get_db, commands="getdb") -dp.register_message_handler(eval_cmd, commands="eval") -dp.register_errors_handler(errors) +logger.info("Start loading modules") +for m in ["basic", "edt", "kfet", "tomuss", "notif", "await_cmd", "tools"]: + load_module(m) +logger.info("Modules loading finish") diff --git a/TelegramEDT/await_cmd.py b/TelegramEDT/await_cmd.py index f9bb97a..fa7f3eb 100644 --- a/TelegramEDT/await_cmd.py +++ b/TelegramEDT/await_cmd.py @@ -3,13 +3,13 @@ import re import requests from PIL import Image from aiogram import types -from aiogram.types import ParseMode +from aiogram.types import ParseMode, ContentType from feedparser import parse from ics.parse import ParseError from pyzbar.pyzbar import decode from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema -from TelegramEDT import API_TOKEN, bot, dbL, key, logger, session, check_id +from TelegramEDT import API_TOKEN, bot, dbL, dp, key, logger, session, check_id from TelegramEDT.EDTcalendar import Calendar from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -91,3 +91,14 @@ async def await_cmd(message: types.message): if msg: await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + +def load(): + logger.info("Load await_cmd module") + dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg),content_types=[ContentType.TEXT, + ContentType.PHOTO]) + + +def unload(): + logger.info("Unload await_cmd module") + dp.message_handlers.unregister(await_cmd) diff --git a/TelegramEDT/basic.py b/TelegramEDT/basic.py index 202c630..0fecc0c 100644 --- a/TelegramEDT/basic.py +++ b/TelegramEDT/basic.py @@ -1,7 +1,7 @@ from aiogram import types from aiogram.types import ParseMode -from TelegramEDT import dbL, key, logger, session, check_id +from TelegramEDT import dbL, dp, key, logger, session, check_id from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -22,3 +22,15 @@ async def help_cmd(message: types.Message): with dbL: user = session.query(User).filter_by(id=message.from_user.id).first() await message.reply(lang(user, "help"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + +def load(): + logger.info("Load basic module") + dp.register_message_handler(start, commands="start") + dp.register_message_handler(help_cmd, commands="help") + + +def unload(): + logger.info("Unload basic module") + dp.message_handlers.unregister(start) + dp.message_handlers.unregister(help_cmd) diff --git a/TelegramEDT/edt.py b/TelegramEDT/edt.py index 29ecd6a..78b12b6 100644 --- a/TelegramEDT/edt.py +++ b/TelegramEDT/edt.py @@ -4,7 +4,7 @@ from aiogram import types from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, InputTextMessageContent, \ InlineQueryResultArticle, InlineQuery -from TelegramEDT import dbL, key, logger, posts_cb, session, TIMES, bot, check_id +from TelegramEDT import dbL, dp, key, logger, posts_cb, session, TIMES, bot, check_id from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -77,3 +77,21 @@ async def edt_geturl(message: types.Message): await message.reply(user.resources, reply_markup=key) else: await message.reply(lang(user, "getedt_err"), reply_markup=key) + + +def load(): + logger.info("Load edt module") + dp.register_message_handler(edt_cmd, lambda msg: msg.text.lower() == "edt") + dp.register_inline_handler(inline_edt) + dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"])) + dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt") + dp.register_message_handler(edt_geturl, commands="getedt") + + +def unload(): + logger.info("Unload edt module") + dp.message_handlers.unregister(edt_cmd) + dp.inline_query_handlers.unregister(inline_edt) + dp.callback_query_handlers.unregister(edt_query) + dp.message_handlers.unregister(edt_await) + dp.message_handlers.unregister(edt_geturl) diff --git a/TelegramEDT/kfet.py b/TelegramEDT/kfet.py index 2346540..d8b8696 100644 --- a/TelegramEDT/kfet.py +++ b/TelegramEDT/kfet.py @@ -5,7 +5,7 @@ from aiogram import types from aiogram.types import ParseMode from aiogram.utils import markdown -from TelegramEDT import dbL, key, logger, session, check_id +from TelegramEDT import dbL, dp, key, logger, session, check_id from TelegramEDT.base import User, KFET_URL from TelegramEDT.lang import lang @@ -45,3 +45,15 @@ async def kfet_set(message: types.Message): session.commit() await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + +def load(): + logger.info("Load kfet module") + dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet") + dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet") + + +def unload(): + logger.info("Unload kfet module") + dp.message_handlers.unregister(kfet) + dp.message_handlers.unregister(kfet_set) diff --git a/TelegramEDT/modules.py b/TelegramEDT/modules.py new file mode 100644 index 0000000..4f7485f --- /dev/null +++ b/TelegramEDT/modules.py @@ -0,0 +1,59 @@ +from importlib import import_module + +from aiogram.types import Message + +from TelegramEDT import ADMIN_ID, logger + + +def load_module(module: str) -> bool: + try: + module = import_module(f"TelegramEDT.{module}") + except ModuleNotFoundError: + logger.error(f"Fail to load module {module}, module not found !") + return False + else: + try: + module.load() + except AttributeError: + logger.error(f"Fail to load module {module}, no load function !") + return False + return True + + +def unload_module(module: str) -> bool: + try: + module = import_module(f"TelegramEDT.{module}") + except ModuleNotFoundError: + logger.error(f"Fail to unload module {module}, module not found !") + return False + else: + try: + module.unload() + except AttributeError: + logger.error(f"Fail to unload module {module}, no unload function !") + return False + return True + + +async def load_cmd(message: Message): + logger.info(f"{message.from_user.username} do load command") + if message.from_user.id == ADMIN_ID: + module = message.text[6:] + if load_module(module): + msg = f"Module {module} loaded !" + else: + msg = f"Fail to load module {module} !" + + await message.reply(msg) + + +async def unload_cmd(message: Message): + logger.info(f"{message.from_user.username} do unload command") + if message.from_user.id == ADMIN_ID: + module = message.text[8:] + if unload_module(module): + msg = f"Module {module} unloaded !" + else: + msg = f"Fail to unload module {module} !" + + await message.reply(msg) diff --git a/TelegramEDT/notif.py b/TelegramEDT/notif.py index ee0f409..d68a408 100644 --- a/TelegramEDT/notif.py +++ b/TelegramEDT/notif.py @@ -4,7 +4,7 @@ from aiogram import types from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode from aiogram.utils import markdown -from TelegramEDT import bot, dbL, logger, posts_cb, session, check_id +from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -81,3 +81,15 @@ async def notif_query(query: types.CallbackQuery, callback_data: dict): session.commit() await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN) + + +def load(): + logger.info("Load notif module") + dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif") + dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"])) + + +def unload(): + logger.info("Unload notif module") + dp.message_handlers.unregister(notif_cmd) + dp.callback_query_handlers.unregister(notif_query) diff --git a/TelegramEDT/tomuss.py b/TelegramEDT/tomuss.py index 57c789d..c711b13 100644 --- a/TelegramEDT/tomuss.py +++ b/TelegramEDT/tomuss.py @@ -1,7 +1,7 @@ from aiogram import types from aiogram.types import ParseMode -from TelegramEDT import dbL, key, logger, session, check_id +from TelegramEDT import dbL, dp, key, logger, session, check_id from TelegramEDT.base import User from TelegramEDT.lang import lang @@ -16,3 +16,13 @@ async def settomuss(message: types.Message): session.commit() await message.reply(lang(user, "settomuss_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key) + + +def load(): + logger.info("Load tomuss module") + dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss") + + +def unload(): + logger.info("Unload tomuss module") + dp.message_handlers.unregister(settomuss) diff --git a/TelegramEDT/tools.py b/TelegramEDT/tools.py index 0ce3973..db580ce 100644 --- a/TelegramEDT/tools.py +++ b/TelegramEDT/tools.py @@ -3,7 +3,7 @@ from aiogram.types import ParseMode from aiogram.utils import markdown from aiogram.utils.exceptions import MessageIsTooLong -from TelegramEDT import ADMIN_ID, bot, dbL, key, log_date, logger, session, check_id +from TelegramEDT import ADMIN_ID, bot, dbL, dp, key, log_date, logger, session, check_id from TelegramEDT.base import User @@ -79,3 +79,21 @@ async def errors(*args, **partial_data): sep="\n" ) await bot.send_message(ADMIN_ID, msg, parse_mode=ParseMode.MARKDOWN) + + +def load(): + logger.info("Load tools module") + dp.register_message_handler(get_id, commands="getid") + dp.register_message_handler(get_logs, commands="getlogs") + dp.register_message_handler(get_db, commands="getdb") + dp.register_message_handler(eval_cmd, commands="eval") + dp.register_errors_handler(errors) + + +def unload(): + logger.info("Unload tools module") + dp.message_handlers.unregister(get_id) + dp.message_handlers.unregister(get_logs) + dp.message_handlers.unregister(get_db) + dp.message_handlers.unregister(eval_cmd) + dp.errors_handlers.unregister(errors) diff --git a/main.py b/main.py index 3f6ae16..ec7c3de 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ import asyncio from aiogram.utils import executor -from TelegramEDT import notif, dp +from TelegramEDT.notif import notif, dp loop = asyncio.get_event_loop() loop.create_task(notif())