Skip to content

Conversation

@ahmubashshir
Copy link
Contributor

  • hooks: Implement hook loader module
  • engine: Use trackma.hooks to load hooks

This approach seems cleaner to me than #769, and also has the added benefit of not breaking unless python ignores __path__

Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
@molkoback
Copy link
Contributor

molkoback commented Apr 24, 2025

Wait, how does this work? At which point do you make the found hooks members trackma.hooks so you can import them using import_module(f'trackma.hooks.{name}')?

The idea of making hooks their separate module is good though. It should probably be a class to avoid __path__.

@ahmubashshir
Copy link
Contributor Author

ahmubashshir commented Apr 24, 2025

Wait, how does this work? At which point do you make the found hooks members trackma.hooks so you can import them using import_module(f'trackma.hooks.{name}')?

the __path__ is set after hooks.init() is called, so to use import_module(...) for trackma.hooks.* you'd have to call that.

The idea of making hooks their separate module is good though. It should probably be a class to avoid __path__.

Everything is done by python... I implemented it using sys.meta_path first, and it's also documented by python1...

either __path__ or sys.path/sys.meta_path must be modified to resolve modules using import_module, and setting __path__ seems cleanest way that's least fragile2.

Footnotes

  1. https://docs.python.org/3/tutorial/modules.html#packages-in-multiple-directories

  2. https://docs.python.org/3/reference/import.html#package-path-rules

Copy link
Collaborator

@FichteFoll FichteFoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea to use __path__ to magically group all hooks into a trackma.hooks package sounds neat and offloading some of the hook logic into a separate file is also appreciated since engine.py definitely does too much currently.

However, I don't feel confident enough to make the shot regarding using import_module here.

(This means it's not a blocker, but I also don't want to call the shot to merge it.)

Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants