Supply a plugin interface

J

Johannes Bauer

Hello group,

I'm developing a GUI application in Python and having a blast so far :)

What I'd like to add: I want the GUI users to supply plugin scripts,
i.e. offer some kind of API. That is, I want the user to write short
Python pieces which look something like

import guiapp

class myplugin():
def __init__(self):
guiapp.add_menu("foobar")

def exec(self, param):
print("foo")

the GUI application should now browse the plugin directory and read
those plugin python files and somehow incorporate (i.e. discover what
modules are there, instanciate, etc.)

How do I do that at runtime with Python?

Kind regards,
Johannes
 
U

Ulrich Eckhardt

Johannes said:
What I'd like to add: I want the GUI users to supply plugin scripts,
i.e. offer some kind of API. That is, I want the user to write short
Python pieces which look something like

import guiapp

class myplugin():
def __init__(self):
guiapp.add_menu("foobar")

def exec(self, param):
print("foo")

the GUI application should now browse the plugin directory and read
those plugin python files and somehow incorporate (i.e. discover what
modules are there, instanciate, etc.)

You will find ways to scan a directory using os.path and related things. In
order to import a module, you add the base directory to sys.path. Since you
have just a variable with the module name (excluding the .py), you can't
use 'import' as it is. Instead, use the __import__ functio (I wonder if
there is anything more elegant) which returns the plugin.

Other than that, better steal ideas than reinventing them. I think e.g.
xchat already provides ways to script the UI, which you could take as an
inspiration.

Have fun!

Uli
 
S

Steven D'Aprano

Hello group,

I'm developing a GUI application in Python and having a blast so far :)

What I'd like to add: I want the GUI users to supply plugin scripts,
i.e. offer some kind of API. That is, I want the user to write short
Python pieces which look something like

import guiapp

class myplugin():
def __init__(self):
guiapp.add_menu("foobar")

def exec(self, param):
print("foo")

"exec" is a reserved word:
File "<stdin>", line 1
def exec(self, param):
^
SyntaxError: invalid syntax

the GUI application should now browse the plugin directory and read
those plugin python files and somehow incorporate (i.e. discover what
modules are there, instanciate, etc.)

How do I do that at runtime with Python?

Untested:

import os
import sys
plugin_dir = os.path.expanduser('~/some/path/')
plugins = set()
for name in os.listdir(plugin_dir):
base, ext = os.path.splitext(name)
if ext in ('.py', '.pyc', '.pyo'):
plugins.add(base)
save_path = sys.path
plugin_modules = []
try:
sys.path = [plugin_dir]
for name in plugins:
plugin_modules.append(__import__(name))
finally:
sys.path = save_path




Hope this is useful.
 
R

R. David Murray

Ulrich Eckhardt said:
You will find ways to scan a directory using os.path and related things. In
order to import a module, you add the base directory to sys.path. Since you
have just a variable with the module name (excluding the .py), you can't
use 'import' as it is. Instead, use the __import__ functio (I wonder if
there is anything more elegant) which returns the plugin.

In 2.7 and 3.1 there will be:

from importlib import import_module

myname = import_module('somename')

which has much more sensible semantics than __import__ does.
 
A

Aahz

the GUI application should now browse the plugin directory and read
those plugin python files and somehow incorporate (i.e. discover what
modules are there, instanciate, etc.)

How do I do that at runtime with Python?

You might consider using execfile() but probably __import__ works better
for your purposes. There's also importlib on PyPI, which is the backport
from 2.7.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top