I'm currently looking for a way to implement a dynamic Extensions system into my plugin. I've come up with this folder structure thus far:
Code: Select all
./plugins/myplugin/myplugin.py
./plugins/myplugin/extensions/__init__.py
./plugins/myplugin/extensions/ext1.py
./plugins/myplugin/extensions/ext2.py
Syntax: Select all
# ./plugins/myplugin/myplugin.py
from myplugin.extensions import ext1
from myplugin.extensions import ext2
This is all good, however, what if I don't know the extension's name? Like when somebody else wrote an extension and put it into the extensions folder.
The bad way works, but that's now what I'm looking for:
Code: Select all
from myplugin.extensions import *
I want to be able to do something like this:
Syntax: Select all
# ./plugins/myplugin/extensions/__init__.py
class Extension(object):
# some code that identifies this object as a plugin Extension
extensions = list() # extension manager that holds Extension objects
Syntax: Select all
# ./plugins/myplugin/extensions/ext1.py
from myplugin.extensions import Extension
from myplugin.extensions import extensions
class Ext1(Extension):
# some code
extensions.append(Ext1())
so I can have some control over those extensions like loading and unloading by server command:
Syntax: Select all
# ./plugins/myplugin/myplugin.py
from myplugin.extensions import extensions
# dome something with the extension manager
In my mind, this can work. However, ext1.py and ext2.py will never get loaded into memory because nothing imports them.
What can I do about this?
My goal is to expose certain listeners from the main plugin which the extensions can listen on and do something about them.
Thank you very much!