Refactor CogManager and extension loading #2446

Closed
Tobotimus wants to merge 20 commits from V3/cogmgr_refactor into V3/develop
Tobotimus commented 2019-02-12 23:53:04 +01:00 (Migrated from github.com)

This kicked off with me realising our use of Loader.load_module() as a hacky way to import modules from random locations is deprecated. Looking closer, I realised there were a couple of smelly things here which could be cleaned up. Here's what's changed:

  • Paths in CogManager are now inserted into redbot.ext_cogs.__path__. This means to import any external cog (from install path, cogmanager path, whatever), the import path would be redbot.ext_cogs.cog_to_import. It makes the rest of the logic really straightforward.
  • Importing cog modules is now done with importlib.import_module - this is a high-level function which allows us to delegate all of the nitty gritty import logic to Python itself, and it should be much more reliable. We're also no longer dealing with ModuleSpec objects, we just go straight from cog name to module.
  • We're now using importlib.reload to reload modules like a good boy. This is done from the bottom-up, twice, to make sure most internal imports in the package are updated. Some old imports could hang around if people do lazy relative importing at the module level.
  • Red.load_extension can take either a module, or a straight name. This means loading a package is as simple as Red.load_extension(package_name), no more needing to know what method of CogManager to use.
  • Some internal documentation was added in a module docstring of redbot.core.cog_manager.
This kicked off with me realising our use of [`Loader.load_module()`](https://docs.python.org/3/library/importlib.html#importlib.machinery.SourceFileLoader.load_module) as a hacky way to import modules from random locations is deprecated. Looking closer, I realised there were a couple of smelly things here which could be cleaned up. Here's what's changed: - Paths in CogManager are now inserted into `redbot.ext_cogs.__path__`. This means to import any external cog (from install path, cogmanager path, whatever), the import path would be `redbot.ext_cogs.cog_to_import`. It makes the rest of the logic really straightforward. - Importing cog modules is now done with `importlib.import_module` - this is a high-level function which allows us to delegate all of the nitty gritty import logic to Python itself, and it should be much more reliable. We're also no longer dealing with `ModuleSpec` objects, we just go straight from cog name to module. - We're now using `importlib.reload` to reload modules like a good boy. This is done from the bottom-up, twice, to make sure *most* internal imports in the package are updated. Some old imports could hang around if people do lazy relative importing at the module level. - `Red.load_extension` can take either a module, or a straight name. This means loading a package is as simple as `Red.load_extension(package_name)`, no more needing to know what method of `CogManager` to use. - Some internal documentation was added in a module docstring of `redbot.core.cog_manager`.
calebj (Migrated from github.com) reviewed 2019-02-12 23:53:04 +01:00
tekulvw (Migrated from github.com) reviewed 2019-02-12 23:53:04 +01:00
Twentysix26 (Migrated from github.com) reviewed 2019-02-12 23:53:04 +01:00
calebj commented 2019-02-18 06:38:36 +01:00 (Migrated from github.com)

I haven't noticed any issues so far in using this, so it seems good. However, I would like to see some more widespread battlefield testing with a variety of third party cogs and external modules before giving my 👍.

I haven't noticed any issues so far in using this, so it seems good. However, I would like to see some more widespread ~~battlefield~~ testing with a variety of third party cogs and external modules before giving my :+1:.
Tobotimus commented 2019-07-04 11:20:20 +02:00 (Migrated from github.com)

This is ready for review again and will be targetting 3.2

This is ready for review again and will be targetting 3.2
mikeshardmind commented 2019-11-08 16:01:19 +01:00 (Migrated from github.com)

I still have a few concerns with regard to using importlib.reload in this manner, but I also haven't seen this break any cogs which I use in any way which wouldn't already be broken with the current reload strategy. I'd like to do another pass over this later before merging it, but it should make the reload behavior of cogs more consistent from an end user standpoint at least.

I still have a few concerns with regard to using `importlib.reload` in this manner, but I also haven't seen this break any cogs which I use in any way which wouldn't already be broken with the current reload strategy. I'd like to do another pass over this later before merging it, but it should make the reload behavior of cogs more consistent from an end user standpoint at least.
mikeshardmind (Migrated from github.com) requested changes 2019-11-09 18:24:12 +01:00
@ -615,0 +614,4 @@
await setup(self)
else:
setup(self)
except Exception as e:
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:19:55 +01:00
            module: types.ModuleType = self._cog_mgr.load_cog_module(module)
```suggestion module: types.ModuleType = self._cog_mgr.load_cog_module(module) ```
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:20:22 +01:00
        await ctx.bot._cog_mgr.reorder_path(to_move, to)
```suggestion await ctx.bot._cog_mgr.reorder_path(to_move, to) ```
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:20:46 +01:00
                module = bot._cog_mgr.load_cog_module(name)
```suggestion module = bot._cog_mgr.load_cog_module(name) ```
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:21:00 +01:00
                module = bot._cog_mgr.reload(module)
```suggestion module = bot._cog_mgr.reload(module) ```
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:21:33 +01:00
        module = self.bot._cog_mgr.load_cog_module(cog_name)
```suggestion module = self.bot._cog_mgr.load_cog_module(cog_name) ```
mikeshardmind (Migrated from github.com) commented 2019-11-09 18:21:23 +01:00
        module = self.bot._cog_mgr.reload(module)
```suggestion module = self.bot._cog_mgr.reload(module) ```
Drapersniper commented 2020-07-06 20:42:20 +02:00 (Migrated from github.com)

@Tobotimus is this something you are still actively working on or have you dropped this PR. Unsure whether you are currently working on this locally and just haven't pushed any commits.

@Tobotimus is this something you are still actively working on or have you dropped this PR. Unsure whether you are currently working on this locally and just haven't pushed any commits.
Drapersniper commented 2020-07-23 11:25:46 +02:00 (Migrated from github.com)

Closing due to innactivity, Will convert into an issue so that other users can pick this up.

Closing due to innactivity, Will convert into an issue so that other users can pick this up.

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2446
No description provided.