Basic plugin architecture

Nick Sarbicki nick.a.sarbicki at gmail.com
Wed Apr 6 05:05:10 EDT 2016


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.



More information about the Python-list mailing list