From ad5c3f2d18b827790e5b201725b2b10c77666ab9 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sat, 7 Apr 2018 23:20:16 +0300 Subject: [PATCH] \m/ --- stdborg.py | 9 ++++++++ stdplugins/testplugin.py | 5 +++++ test.py | 13 ----------- uniborg/_core.py | 0 uniborg/hacks.py | 7 ++++++ uniborg/telethon.py | 47 +++++++++++++++++++++++++++++++++++----- 6 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 stdborg.py create mode 100644 stdplugins/testplugin.py delete mode 100644 test.py create mode 100644 uniborg/_core.py create mode 100644 uniborg/hacks.py diff --git a/stdborg.py b/stdborg.py new file mode 100644 index 0000000..4a23470 --- /dev/null +++ b/stdborg.py @@ -0,0 +1,9 @@ +import logging + +from uniborg import Uniborg + +logging.basicConfig(level=logging.INFO) + +borg = Uniborg("stdborg", plugin_path="stdplugins") + +borg.run() diff --git a/stdplugins/testplugin.py b/stdplugins/testplugin.py new file mode 100644 index 0000000..5635119 --- /dev/null +++ b/stdplugins/testplugin.py @@ -0,0 +1,5 @@ +from telethon import events + +@borg.on(events.NewMessage) +async def asdf(e): + print(e.raw_text) diff --git a/test.py b/test.py deleted file mode 100644 index 770eb61..0000000 --- a/test.py +++ /dev/null @@ -1,13 +0,0 @@ -from uniborg import Uniborg -from telethon import events - -borg = Uniborg("uniborg") - -print(borg.uid) -print(dir(borg)) - -@borg.on(events.NewMessage) -async def asdf(e): - print(e.raw_text) - -borg.run() diff --git a/uniborg/_core.py b/uniborg/_core.py new file mode 100644 index 0000000..e69de29 diff --git a/uniborg/hacks.py b/uniborg/hacks.py new file mode 100644 index 0000000..19fb210 --- /dev/null +++ b/uniborg/hacks.py @@ -0,0 +1,7 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +class ReverseList(list): + def __iter__(self): + return reversed(self) diff --git a/uniborg/telethon.py b/uniborg/telethon.py index 7272b1d..b258338 100644 --- a/uniborg/telethon.py +++ b/uniborg/telethon.py @@ -3,25 +3,62 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. import asyncio +import importlib.util import logging +from pathlib import Path from telethon import TelegramClient import telethon.utils +from . import hacks + class Uniborg(TelegramClient): - def __init__(self, session, **kwargs): - self.logger = logging.getLogger(session) + def __init__(self, session, *, plugin_path="plugins", **kwargs): + # TODO: handle non-string session + self._name = session + self._logger = logging.getLogger(session) + self._plugins = {} + self._plugin_path = plugin_path super().__init__(session, 17349, "344583e45741c457fe1862106095a5eb", # yarr **kwargs) - asyncio.get_event_loop().run_until_complete(self.async_init()) + # This is a hack, please avert your eyes + # We want this in order for the most recently added handler to take + # precedence + self._event_builders = hacks.ReverseList() - async def async_init(self): + self._loop.run_until_complete(self._async_init()) + + core_plugin = Path(__file__).parent / "_core.py" + self.load_plugin_from_file(core_plugin) + + for p in Path().glob(f"{self._plugin_path}/*.py"): + self.load_plugin_from_file(p) + + async def _async_init(self): await self.start() self.uid = telethon.utils.get_peer_id(await self.get_me()) def run(self): - asyncio.get_event_loop().run_forever() + self._loop.run_forever() + + def load_plugin(self, shortname): + self.load_plugin_from_file(f"{self._plugin_path}/{shortname}.py") + + def load_plugin_from_file(self, path): + path = Path(path) + shortname = path.stem + name = f"_UniborgPlugins.{self._name}.{shortname}" + + spec = importlib.util.spec_from_file_location(name, path) + mod = importlib.util.module_from_spec(spec) + + mod.borg = self + mod.logger = logging.getLogger(shortname) + + spec.loader.exec_module(mod) + self._plugins[shortname] = mod + self._logger.info(f"Successfully loaded plugin {shortname}")