Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106562
| From | Nick Sarbicki <nick.a.sarbicki@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Basic plugin architecture |
| Date | 2016-04-06 09:05 +0000 |
| Message-ID | <mailman.124.1459933522.32530.python-list@python.org> (permalink) |
| References | <CAGuvt93a9sPMSBt+9ybErOsex2YFoAVj8n2KrUnSkHX8oO2P_w@mail.gmail.com> |
Hi,
Question on how people here would design a basic plugin architecture:
I'm playing with the idea of having a pluggable system where the users can
create some simple classes which can then be accessed in a Django app.
I want to make it as __simple__ as possible for the user. Hopefully to the
point of being able to make a .py file and then drag and drop it into the
plugins folder. The result should be a dropdown later on with the list of
available classes.
Providing the list of classes once imported should be simple.
Actually importing the classes dynamically has caused a few issues.
I have a solution but wanted to know if anyone had better ideas.
Folder structure is:
├run.py
└loader/
├─plugins/
│ ├─__init__.py
│ ├─plugin1.py
│ └─plugin2.py
├─__init__.py
├─models.py
├─views.py
└─etc.py
My current solution is:
########loader/plugins/__init__.py########
> import glob
> import importlib
> import os
>
> def is_valid_plugin(plugin):
> return os.path.isfile(plugin) and not
os.path.basename(plugin).startswith('_')
>
> package = 'loader.plugins'
>
> plugins_to_import = [plugin for plugin in
glob.glob(os.path.dirname(__file__) + '/*.py') if is_valid_plugin(plugin)]
>
> plugins = [importlib.import_module('.' + os.path.basename(plugin)[:-3],
package) for plugin in plugins_to_import]
In relative terms I'm looking at the packages from the same position as
run.py, so would import as:
> from loader import plugins
plugins.plugins would then be a list of imported plugin modules.
With this I can use the __dict__ of each plugin module in plugins.plugins
to find which classes we want to use.
This works. But I wonder if there is a better way.
Any suggestions?
- Nick.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Basic plugin architecture Nick Sarbicki <nick.a.sarbicki@gmail.com> - 2016-04-06 09:05 +0000
csiph-web