Skip to content

Commit 0bfecfa

Browse files
committed
feat(plugins): support multiple root directories
1 parent ce0fa69 commit 0bfecfa

1 file changed

Lines changed: 81 additions & 73 deletions

File tree

pyrogram/client.py

Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class Client(Methods):
169169
170170
plugins (``dict``, *optional*):
171171
Smart Plugins settings as dict, e.g.: *dict(root="plugins")*.
172+
The ``root`` key can be a single string or a list of strings to load plugins from multiple directories,
173+
e.g.: *dict(root=["plugins", "extra_plugins"])*.
172174
173175
parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
174176
Set the global parse mode of the client. By default, texts are parsed using both Markdown and HTML styles.
@@ -991,101 +993,107 @@ def load_plugins(self):
991993
include = plugins.get("include", [])
992994
exclude = plugins.get("exclude", [])
993995

996+
if isinstance(root, list):
997+
roots = root
998+
else:
999+
roots = [root]
1000+
9941001
count = 0
9951002

996-
if not include:
997-
for path in sorted(Path(root.replace(".", "/")).rglob("*.py")):
998-
module_path = '.'.join(path.parent.parts + (path.stem,))
999-
module = import_module(module_path)
1003+
for root in roots:
1004+
if not include:
1005+
for path in sorted(Path(root.replace(".", "/")).rglob("*.py")):
1006+
module_path = '.'.join(path.parent.parts + (path.stem,))
1007+
module = import_module(module_path)
10001008

1001-
for name in vars(module).keys():
1002-
# noinspection PyBroadException
1003-
try:
1004-
for handler, group in getattr(module, name).handlers:
1005-
if isinstance(handler, Handler) and isinstance(group, int):
1006-
self.add_handler(handler, group)
1009+
for name in vars(module).keys():
1010+
# noinspection PyBroadException
1011+
try:
1012+
for handler, group in getattr(module, name).handlers:
1013+
if isinstance(handler, Handler) and isinstance(group, int):
1014+
self.add_handler(handler, group)
10071015

1008-
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1009-
self.name, type(handler).__name__, name, group, module_path))
1016+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1017+
self.name, type(handler).__name__, name, group, module_path))
10101018

1011-
count += 1
1012-
except Exception:
1013-
pass
1014-
else:
1015-
for path, handlers in include:
1016-
module_path = root + "." + path
1017-
warn_non_existent_functions = True
1019+
count += 1
1020+
except Exception:
1021+
pass
1022+
else:
1023+
for path, handlers in include:
1024+
module_path = root + "." + path
1025+
warn_non_existent_functions = True
10181026

1019-
try:
1020-
module = import_module(module_path)
1021-
except ImportError:
1022-
log.warning('[%s] [LOAD] Ignoring non-existent module "%s"', self.name, module_path)
1023-
continue
1027+
try:
1028+
module = import_module(module_path)
1029+
except ImportError:
1030+
log.warning('[%s] [LOAD] Ignoring non-existent module "%s"', self.name, module_path)
1031+
continue
10241032

1025-
if "__path__" in dir(module):
1026-
log.warning('[%s] [LOAD] Ignoring namespace "%s"', self.name, module_path)
1027-
continue
1033+
if "__path__" in dir(module):
1034+
log.warning('[%s] [LOAD] Ignoring namespace "%s"', self.name, module_path)
1035+
continue
10281036

1029-
if handlers is None:
1030-
handlers = vars(module).keys()
1031-
warn_non_existent_functions = False
1037+
if handlers is None:
1038+
handlers = vars(module).keys()
1039+
warn_non_existent_functions = False
10321040

1033-
for name in handlers:
1034-
# noinspection PyBroadException
1035-
try:
1036-
for handler, group in getattr(module, name).handlers:
1037-
if isinstance(handler, Handler) and isinstance(group, int):
1038-
self.add_handler(handler, group)
1041+
for name in handlers:
1042+
# noinspection PyBroadException
1043+
try:
1044+
for handler, group in getattr(module, name).handlers:
1045+
if isinstance(handler, Handler) and isinstance(group, int):
1046+
self.add_handler(handler, group)
10391047

1040-
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1041-
self.name, type(handler).__name__, name, group, module_path))
1048+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1049+
self.name, type(handler).__name__, name, group, module_path))
10421050

1043-
count += 1
1044-
except Exception:
1045-
if warn_non_existent_functions:
1046-
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
1047-
self.name, name, module_path))
1051+
count += 1
1052+
except Exception:
1053+
if warn_non_existent_functions:
1054+
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
1055+
self.name, name, module_path))
10481056

1049-
if exclude:
1050-
for path, handlers in exclude:
1051-
module_path = root + "." + path
1052-
warn_non_existent_functions = True
1057+
if exclude:
1058+
for path, handlers in exclude:
1059+
module_path = root + "." + path
1060+
warn_non_existent_functions = True
10531061

1054-
try:
1055-
module = import_module(module_path)
1056-
except ImportError:
1057-
log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path)
1058-
continue
1062+
try:
1063+
module = import_module(module_path)
1064+
except ImportError:
1065+
log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path)
1066+
continue
10591067

1060-
if "__path__" in dir(module):
1061-
log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path)
1062-
continue
1068+
if "__path__" in dir(module):
1069+
log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path)
1070+
continue
10631071

1064-
if handlers is None:
1065-
handlers = vars(module).keys()
1066-
warn_non_existent_functions = False
1072+
if handlers is None:
1073+
handlers = vars(module).keys()
1074+
warn_non_existent_functions = False
10671075

1068-
for name in handlers:
1069-
# noinspection PyBroadException
1070-
try:
1071-
for handler, group in getattr(module, name).handlers:
1072-
if isinstance(handler, Handler) and isinstance(group, int):
1073-
self.remove_handler(handler, group)
1076+
for name in handlers:
1077+
# noinspection PyBroadException
1078+
try:
1079+
for handler, group in getattr(module, name).handlers:
1080+
if isinstance(handler, Handler) and isinstance(group, int):
1081+
self.remove_handler(handler, group)
10741082

1075-
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
1076-
self.name, type(handler).__name__, name, group, module_path))
1083+
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
1084+
self.name, type(handler).__name__, name, group, module_path))
10771085

1078-
count -= 1
1079-
except Exception:
1080-
if warn_non_existent_functions:
1081-
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
1082-
self.name, name, module_path))
1086+
count -= 1
1087+
except Exception:
1088+
if warn_non_existent_functions:
1089+
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
1090+
self.name, name, module_path))
10831091

10841092
if count > 0:
10851093
log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format(
1086-
self.name, count, "s" if count > 1 else "", root))
1094+
self.name, count, "s" if count > 1 else "", ", ".join(roots)))
10871095
else:
1088-
log.warning('[%s] No plugin loaded from "%s"', self.name, root)
1096+
log.warning('[%s] No plugin loaded from "%s"', self.name, ", ".join(roots))
10891097

10901098
async def handle_download(self, packet):
10911099
file_id, directory, file_name, in_memory, file_size, progress, progress_args = packet

0 commit comments

Comments
 (0)