Skip to content

Commit

Permalink
pyrofork: Ignore excluded plugins
Browse files Browse the repository at this point in the history
currently if you specify a plugin to exclude in pyrogram,
the client first imports it (https://github.com/Mayuri-Chan/pyrofork/blob/caea59cc17fe96bec40b4c55852ce404c4908fa0/pyrogram/client.py#L874)
and add_handler() (https://github.com/Mayuri-Chan/pyrofork/blob/caea59cc17fe96bec40b4c55852ce404c4908fa0/pyrogram/client.py#L880)
and then after this it uses remove_handler() (https://github.com/Mayuri-Chan/pyrofork/blob/caea59cc17fe96bec40b4c55852ce404c4908fa0/pyrogram/client.py#L948) exclude that handler
this usually works well in most case,
but in a few case if the module to exclude has an error,
this is not handled and stops the program,
so need to fix this modules even if it not the target of interest (as it is in exclude=[])

Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Sep 10, 2024
1 parent 0f3313a commit 4ecb99f
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,24 @@ def load_plugins(self):
root = plugins["root"]
include = plugins.get("include", [])
exclude = plugins.get("exclude", [])
exclude_plugins = []
if exclude:
for i in exclude:
plugin = root.replace(".", "/") + "/" + i.replace(".", "/")
if Path(plugin).is_dir():
for j in sorted(Path(plugin).rglob("*.py")):
exclude_plugins.append(str(j).replace("/", ".")[:-3])
else:
exclude_plugins.append(plugin.replace("/", "."))

count = 0

if not include:
for path in sorted(Path(root.replace(".", "/")).rglob("*.py")):
module_path = '.'.join(path.parent.parts + (path.stem,))
if exclude_plugins and module_path in exclude_plugins:
log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {module_path}")
continue
module = import_module(module_path)

for name in vars(module).keys():
Expand All @@ -889,6 +901,9 @@ def load_plugins(self):
else:
for path, handlers in include:
module_path = root.replace("/",".") + "." + path
if exclude_plugins and module_path in exclude_plugins:
log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {module_path}")
continue
warn_non_existent_functions = True

try:
Expand Down Expand Up @@ -921,41 +936,6 @@ def load_plugins(self):
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.name, name, module_path))

if exclude:
for path, handlers in exclude:
module_path = root.replace("/",".") + "." + path
warn_non_existent_functions = True

try:
module = import_module(module_path)
except ImportError:
log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path)
continue

if "__path__" in dir(module):
log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path)
continue

if handlers is None:
handlers = vars(module).keys()
warn_non_existent_functions = False

for name in handlers:
# noinspection PyBroadException
try:
for handler, group in getattr(module, name).handlers:
if isinstance(handler, Handler) and isinstance(group, int):
self.remove_handler(handler, group)

log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
self.name, type(handler).__name__, name, group, module_path))

count -= 1
except Exception:
if warn_non_existent_functions:
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.name, name, module_path))

if count > 0:
log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format(
self.name, count, "s" if count > 1 else "", root))
Expand Down

0 comments on commit 4ecb99f

Please sign in to comment.