473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Application Plugin Framework

Ron
Hello,

I'm attempting to develop a plugin framework for an application that I'm
working on. I wish to develop something in which all plugins exist in a
directory tree. The framework need only be given the root of the tree. The
framework then uses os.path.walk to search all for all files named
'plugin.pyc'. These are then loaded using imp.load_compiled(). They need
contain only one variable called 'plugin' which is a reference to an
instance of the plugin object. This is extrated from the loaded module
using getattr. After that, the plugins are easy to work with since they
implement a standard interface. Or so the theory goes. And this does work
fine provided the plugin is implemented entirely within that one file. If
there are support modules that the main plugin module imports, which exist
in the plugin's directory, then I get problems. The imports fail. I get
"ImportError: No module named <support module name>"

Here's PluginManager.py:
####################

import os
class PluginManager( object ):
def __init__( self, pluginDir ):
self._plugins = { }

os.path.walk( pluginDir, self._addPlugin, None )

def _addPlugin( self, arg, dirname, names ):
import imp

for filename in names:
fullFilename = os.path.join( dirname, filename )
if os.path.isfile( fullFilename ) and (filename == 'plugin.pyc'):
module = imp.load_compiled( 'plugin', fullFilename )
self._plugins[ plugin.name ] = getattr( module, 'plugin' )
return

PM = PluginManager( r'C:\Personal\SWDev\ModuleTest' ) # Root of the
plugin directory tree

print 'Plugin List: ', PM._plugins.keys()

for name,plugin in PM._plugins.iteritems():
plugin.doSomething( )

print 'done!'

#######################
My plugin.py file is in C:\Personal\SWDev\ModuleTest\Plugins\MyPlugin. It's
called plugin.pyc (which I compiled from the following source by importing
it into the interactive python shell.
#######################

import work
class MyPlugin( object ):
def __init__( self ):
self.name = 'MyPlugin'

def doSomething( self ):
work.foo( )
plugin = MyPlugin( )

#######################
Finally, work.py is in the same directory as plugin.py
#######################

def foo( ):
print 'foo called'

######################
Does anybody have any thoughts on how to get this, or something similar, to
work? I really just want to be able to load plugins into my app without
having to modify my app's source code, or modify some list of plugins. I
also would like to allow each plugin to have its own subdirectory so they
are easily installed or removed.

Thanks for your help.


Nov 22 '05 #1
2 3295
Ron,
I'm attempting to develop a plugin framework for an application that I'm
working on. I wish to develop something in which all plugins exist in a
directory tree.


The PIL of the effbot is doing exactly this. (Python Image Library). I
know it, because I had to work around that dynamic for freezing it
with py2exe :)

www.effbot.org, search for PIL

There you can find how this problem was solved by the effbot. Guess
that should be good enough for mortals.

Harald

Nov 22 '05 #2
Ron wrote:
Hello,

I'm attempting to develop a plugin framework for an application that I'm
working on. I wish to develop something in which all plugins exist in a
directory tree. The framework need only be given the root of the tree. The
framework then uses os.path.walk to search all for all files named
'plugin.pyc'. These are then loaded using imp.load_compiled(). They need
contain only one variable called 'plugin' which is a reference to an
instance of the plugin object. This is extrated from the loaded module
using getattr. After that, the plugins are easy to work with since they
implement a standard interface. Or so the theory goes. And this does work
fine provided the plugin is implemented entirely within that one file. If
there are support modules that the main plugin module imports, which exist
in the plugin's directory, then I get problems. The imports fail. I get
"ImportError: No module named <support module name>"

Here's PluginManager.py:
####################

import os
class PluginManager( object ):
def __init__( self, pluginDir ):
self._plugins = { }

os.path.walk( pluginDir, self._addPlugin, None )

def _addPlugin( self, arg, dirname, names ):
import imp

for filename in names:
fullFilename = os.path.join( dirname, filename )
if os.path.isfile( fullFilename ) and (filename == 'plugin.pyc'):
module = imp.load_compiled( 'plugin', fullFilename )
self._plugins[ plugin.name ] = getattr( module, 'plugin' )
return

PM = PluginManager( r'C:\Personal\SWDev\ModuleTest' ) # Root of the
plugin directory tree

print 'Plugin List: ', PM._plugins.keys()

for name,plugin in PM._plugins.iteritems():
plugin.doSomething( )

print 'done!'

#######################
My plugin.py file is in C:\Personal\SWDev\ModuleTest\Plugins\MyPlugin. It's
called plugin.pyc (which I compiled from the following source by importing
it into the interactive python shell.
#######################

import work
class MyPlugin( object ):
def __init__( self ):
self.name = 'MyPlugin'

def doSomething( self ):
work.foo( )
plugin = MyPlugin( )

#######################
Finally, work.py is in the same directory as plugin.py
#######################

def foo( ):
print 'foo called'

######################


Hi Ron,

the import directive does not lookup work.py in the same directory as
plugin.py. It searches through the directory of PluginManager.py and
otherwise searches through the directories of sys.path. The solution is
referencing work.py relative to the dir of the PluginManager.
MyFramework/
__init__.py
PluginManager.py
Plugins/
plugin.py
work.py
#######################
plugin.py
#######################
import MyFramework.Plugins.work as work

# do some stuff...

If import still causes trouble add the path of MyFramework to sys.path.
Regards,
Kay

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Roses Femerling | last post by:
Dear pythonnians :) Hopeful somebody can help me about implementing plugin support. I'm working on a python/wxpython app that needs plugin support. My problem is: The way my app works is a...
1
by: Derek | last post by:
Hi All, I am developing a Windows based application that consists of several different modules. These modules are effectively separate from each other yet share information in a common database....
7
by: Saso Zagoranski | last post by:
Hi! I mentioned this in my other post today but at that time I though I had it all figured out. When I got a reply regarding my other questing I saw that I don't :) Here's my problem: I...
2
by: Chua Wen Ching | last post by:
Hi there, I had some doubts on creatings plugins. As most example on the internet shows how to write plugins onto a plugin host which is normally a windows form program. 1) Can i replace...
2
by: Edvard Majakari | last post by:
Hi, My idea is to create a system working as follows: each module knows path to plugin directory, and that directory contains modules which may add hooks to some points in the code. Inspired...
24
by: Rajiv | last post by:
Hello, Can some one guide me on how do i make a .NET application which upon installing on the target system doesnot require a .NET Framework Installed? Bye Rajiv
3
by: redefined.horizons | last post by:
Its the Java developer again... I'm working on an application framework that I would like to implement in Python. Part of the application framework is a plug-in model that is similar to the one...
1
by: JGAllen23 | last post by:
I have an application that has an interface for people to create plugins and use them. Right now I just search for dlls that use my interface in a specific folder. I want to create a filetype so...
22
by: Wildemar Wildenburger | last post by:
To make it short: Is there something like this already? There seem to loads of python frameworks for Web-Apps, but I have a hard time finding one for desktop-apps. I imagine it wouldn't be too...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.