From 9e3cf9edf76e14d7238a4e2a0f35020690279c71 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 22 Oct 2018 20:23:50 +0200 Subject: [PATCH 1/2] Use "with" context manager in examples --- docs/source/resources/AutoAuthorization.rst | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/source/resources/AutoAuthorization.rst b/docs/source/resources/AutoAuthorization.rst index d7a099fe..b5f3a94a 100644 --- a/docs/source/resources/AutoAuthorization.rst +++ b/docs/source/resources/AutoAuthorization.rst @@ -35,11 +35,8 @@ Example: password="password" # (if you have one) ) - app.start() - - print(app.get_me()) - - app.stop() + with app: + print(app.get_me()) Sign Up ------- @@ -67,8 +64,5 @@ Example: last_name="" # Can be an empty string ) - app.start() - - print(app.get_me()) - - app.stop() \ No newline at end of file + with app: + print(app.get_me()) From 99bdaae365b4b09ac7fd9e07d13e074b0e819a57 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 23 Oct 2018 15:43:49 +0200 Subject: [PATCH 2/2] Allow nested folders for smart plugins --- pyrogram/client/client.py | 70 ++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 2d1e211e..9b45e941 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -34,6 +34,7 @@ from configparser import ConfigParser from datetime import datetime from hashlib import sha256, md5 from importlib import import_module +from pathlib import Path from signal import signal, SIGINT, SIGTERM, SIGABRT from threading import Thread @@ -980,41 +981,42 @@ class Client(Methods, BaseClient): def load_plugins(self): if self.plugins_dir is not None: - try: - dirs = os.listdir(self.plugins_dir) - except FileNotFoundError: - log.warning('No plugin loaded: "{}" directory is missing'.format(self.plugins_dir)) + plugins_count = 0 + + for path in Path(self.plugins_dir).rglob("*.py"): + file_path = os.path.splitext(str(path))[0] + import_path = [] + + while file_path: + file_path, tail = os.path.split(file_path) + import_path.insert(0, tail) + + import_path = ".".join(import_path) + module = import_module(import_path) + + for name in dir(module): + # noinspection PyBroadException + try: + handler, group = getattr(module, name) + + if isinstance(handler, Handler) and isinstance(group, int): + self.add_handler(handler, group) + + log.info('{}("{}") from "{}" loaded in group {}'.format( + type(handler).__name__, name, import_path, group)) + + plugins_count += 1 + except Exception: + pass + + if plugins_count > 0: + log.warning('Successfully loaded {} plugin{} from "{}"'.format( + plugins_count, + "s" if plugins_count > 1 else "", + self.plugins_dir + )) else: - plugins_dir = self.plugins_dir.lstrip("./").replace("/", ".") - plugins_count = 0 - - for i in dirs: - module = import_module("{}.{}".format(plugins_dir, i.split(".")[0])) - - for j in dir(module): - # noinspection PyBroadException - try: - handler, group = getattr(module, j) - - if isinstance(handler, Handler) and isinstance(group, int): - self.add_handler(handler, group) - - log.info('{}("{}") from "{}/{}" loaded in group {}'.format( - type(handler).__name__, j, self.plugins_dir, i, group) - ) - - plugins_count += 1 - except Exception: - pass - - if plugins_count > 0: - log.warning('Successfully loaded {} plugin{} from "{}"'.format( - plugins_count, - "s" if plugins_count > 1 else "", - self.plugins_dir - )) - else: - log.warning('No plugin loaded: "{}" doesn\'t contain any valid plugin'.format(self.plugins_dir)) + log.warning('No plugin loaded: "{}" doesn\'t contain any valid plugin'.format(self.plugins_dir)) def save_session(self): auth_key = base64.b64encode(self.auth_key).decode()