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)
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 the modules even if it not the target of interest (as it is in exclude=[])

Co-authored-by: RabbitFoRed <73241991+RabbitFored@users.noreply.github.com>
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 and RabbitFored committed Sep 13, 2024
1 parent 0f3313a commit 593ab49
Showing 1 changed file with 132 additions and 60 deletions.
192 changes: 132 additions & 60 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ async def load_session(self):
print(e)

def load_plugins(self):

if self.plugins:
plugins = self.plugins.copy()

Expand All @@ -863,42 +864,116 @@ def load_plugins(self):

if plugins.get("enabled", True):
root = plugins["root"]

include = plugins.get("include", [])
exclude = plugins.get("exclude", [])

count = 0

if not include:
for path in sorted(Path(root.replace(".", "/")).rglob("*.py")):
module_path = '.'.join(path.parent.parts + (path.stem,))
module = import_module(module_path)
exclude_plugins = []
exclude_handlers = {}

for name in vars(module).keys():
# noinspection PyBroadException
try:
for handler, group in getattr(module, name).handlers:
if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group)
if exclude:
for path, handler in exclude:
module_path = os.path.join(
root.replace(".", "/"), path.replace(".", "/")
)
if handler is None:
exclude_plugins.append(module_path.replace("/", "."))
else:
exclude_handlers[module_path.replace("/", ".")] = handler

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

count += 1
except Exception:
pass
if not include:
for root, dirnames, filenames in os.walk(root.replace(".", "/")):
namespace = root.replace("/", ".")
if namespace in exclude_plugins:
log.warning(
'[%s] [LOAD] Ignoring namespace "%s"', self.name, namespace
)
continue
else:
for filename in filenames:
if filename.endswith(".py"):
module_path = namespace + "." + filename[:-3]
if module_path in exclude_plugins:
log.warning(
'[%s] [LOAD] Ignoring namespace "%s"',
self.name,
module_path,
)
continue
else:
module = import_module(module_path)

for name in vars(module).keys():

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

if (
module_path in exclude_handlers
and name
in exclude_handlers[module_path]
):
exclude_handlers[
module_path
].remove(name)
log.warning(
'[{}] [LOAD] Ignoring function "{}" from group {} in "{}"'.format(
self.name,
name,
group,
module_path,
)
)
continue

self.add_handler(handler, group)

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

count += 1
except Exception as e:
pass
else:
for path, handlers in include:
module_path = root.replace("/",".") + "." + path
module_path = root.replace("/", ".") + "." + path
if module_path in exclude_plugins:
log.warning(
'[%s] [LOAD] Ignoring namespace "%s"', self.name, module_path
)
continue

warn_non_existent_functions = True

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

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

if handlers is None:
Expand All @@ -910,55 +985,52 @@ def load_plugins(self):
try:
for handler, group in getattr(module, name).handlers:
if isinstance(handler, Handler) and isinstance(group, int):
if (
module_path in exclude_handlers
and name in exclude_handlers[module_path]
):
exclude_handlers[module_path].remove(name)
log.warning(
'[{}] [LOAD] Ignoring function "{}" from group {} in "{}"'.format(
self.name, name, group, module_path
)
)
continue
self.add_handler(handler, group)

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

count += 1
except Exception:
if warn_non_existent_functions:
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))
log.warning(
'[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.name, name, 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))
for module in exclude_handlers:
for handler in exclude_handlers[module]:
log.warning(
'[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.name, handler, module
)
)

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

Expand Down

0 comments on commit 593ab49

Please sign in to comment.