From 6ced7ba945b92d463afc01daf1a0a728505f4db1 Mon Sep 17 00:00:00 2001 From: AAA3A <89632044+AAA3A-AAA3A@users.noreply.github.com> Date: Tue, 19 Jul 2022 17:10:45 +0200 Subject: [PATCH] [Core] Add `--unload-cogs` cli flag. (#5802) * [Core] Add `--unload-cogs` cli. * Fixed error + Reformat. * At @Jack1142's request, the `packages` local variable is no longer a list, but a dictionary with `None` values, to avoid duplication. * Update redbot/core/bot.py Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com> * Update redbot/core/bot.py Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com> * Update bot.py * Update bot.py Co-authored-by: Jakub Kuczys <6032823+jack1142@users.noreply.github.com> --- redbot/core/bot.py | 25 ++++++++++++++----------- redbot/core/cli.py | 3 +++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index 2aa23366a..83a714398 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -8,7 +8,7 @@ import sys import contextlib import weakref import functools -from collections import namedtuple +from collections import namedtuple, OrderedDict from datetime import datetime from enum import IntEnum from importlib.machinery import ModuleSpec @@ -1109,7 +1109,7 @@ class Red( await modlog._init(self) await bank._init() - packages = [] + packages = OrderedDict() last_system_info = await self._config.last_system_info() @@ -1135,10 +1135,13 @@ class Red( python_version_changed = True else: if self._cli_flags.no_cogs is False: - packages.extend(await self._config.packages()) + packages.update(dict.fromkeys(await self._config.packages())) if self._cli_flags.load_cogs: - packages.extend(self._cli_flags.load_cogs) + packages.update(dict.fromkeys(self._cli_flags.load_cogs)) + if self._cli_flags.unload_cogs: + for package in self._cli_flags.unload_cogs: + packages.pop(package, None) system_changed = False machine = platform.machine() @@ -1169,11 +1172,9 @@ class Red( if packages: # Load permissions first, for security reasons try: - packages.remove("permissions") - except ValueError: + packages.move_to_end("permissions", last=False) + except KeyError: pass - else: - packages.insert(0, "permissions") to_remove = [] log.info("Loading packages...") @@ -1197,9 +1198,11 @@ class Red( await self.remove_loaded_package(package) to_remove.append(package) for package in to_remove: - packages.remove(package) - if packages: - log.info("Loaded packages: " + ", ".join(packages)) + del packages[package] + if packages: + log.info("Loaded packages: " + ", ".join(packages)) + else: + log.info("No packages were loaded.") if self.rpc_enabled: await self.rpc.initialize(self.rpc_port) diff --git a/redbot/core/cli.py b/redbot/core/cli.py index dda223b3d..bf37f3a8a 100644 --- a/redbot/core/cli.py +++ b/redbot/core/cli.py @@ -185,6 +185,9 @@ def parse_cli_flags(args): help="Force loading specified cogs from the installed packages. " "Can be used with the --no-cogs flag to load these cogs exclusively.", ) + parser.add_argument( + "--unload-cogs", type=str, nargs="+", help="Force unloading specified cogs." + ) parser.add_argument( "--dry-run", action="store_true",