473,385 Members | 1,901 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,385 software developers and data experts.

"Plugin" architecture - how to do?

I'm making a program that consists of a main engine + plugins. Both
are in Python. My question is, how do I go about importing arbitrary
code and have it be able to use the engine's functions, classes, etc?
First, here's how it's laid out on the filesystem:

-mainstarterscript.py
-<engine>
-__init__.py
-[whole bunch of files]
-<plugin>
-__init__.py
-main.py
-[whole bunch of files]

So you'd execute the whole thing with ./mainstarterscript.py, which
imports engine. Engine initializes, then executes plugin somehow.
Plugin needs to be able to call functions in engine, and vice versa.
Although various IPC methods would probably work, I think there's
gotta be an easier and more straightforward way to do it.

I use execfile to execute the plugin's __init__.py script. I insert
some globals from engine into it so that __init__ can use them.
__init__.py also imports plugin/main.py, which uses functions the
engine functions, and so must import __init__.py. The problem is,
when main imports __init__, the inserted globals aren't there, so it
dies. So I've tried to make some globals in __init__ that simply map
them to the engine's globals. Here's roughly how:
#plugin/__init__.py

engine_globals = {}

if __name__ == '__builtin__': #run via execfile
global engine_globals
engine_globals = globals() #I don't really want all the globals, but
in my real code I go through them one by one
else: #imported from one of the other scripts in plugin dir.
pass

import main #among other things
# end __init__.py

# plugins/main.py
import __init__

__init__.engine_globals.['some_function'](args)
#do other stuff
#end main.py

#engine/__init__.py
def some_function(args):
pass

sys.argv.append('plugins')
execfile('plugins/__init__.py')

When main.py imports plungin/__init__, it reinitializes engine_globals
to {}, of course. But I can't think of a way not to initialize it to
something, thus overwriting the original, because it needs to be in
the namespace.. Essentially, I want a global to not reset itself if
it's already set.

So how do I go about doing this? Am I on the right track? Should I
be __import__'ing plugins instead of execfile'ing them?

Thanks,
Nick

Apr 5 '07 #1
4 2577
On Apr 5, 10:57 am, anglozax...@gmail.com wrote:
I'm making a program that consists of a main engine + plugins. Both
are in Python. My question is, how do I go about importing arbitrary
code and have it be able to use the engine's functions, classes, etc?
For a true plugin architecture, you don't have the main engine calling
methods on the plugin. What you do is have an API on your engine with
methods the plugin can call and events it can hook into. The events
are how the engine communicates state to any plugins, without having
to know who they are or what they do. Your engine has a configuration
that tells it what plugins to load (which the plugins presumably
modified when they installed themselves) or otherwise has some
standard way that the engine can figure out what plugins need to be
loaded.

Now granted, I don't specifically know how to do this via python..
but, maybe what I've said will send you in the right direction.

-Nate
Apr 6 '07 #2
On Apr 6, 9:59 am, "Nate Finch" <nate.fi...@gmail.comwrote:
On Apr 5, 10:57 am, anglozax...@gmail.com wrote:
I'm making a program that consists of a main engine + plugins. Both
are in Python. My question is, how do I go about importing arbitrary
code and have it be able to use the engine's functions, classes, etc?

For a truepluginarchitecture, you don't have the main engine calling
methods on theplugin. What you do is have an API on your engine with
methods theplugincan call and events it can hook into. The events
are how the engine communicates state to any plugins, without having
to know who they are or what they do. Your engine has a configuration
that tells it what plugins to load (which the plugins presumably
modified when they installed themselves) or otherwise has some
standard way that the engine can figure out what plugins need to be
loaded.

Now granted, I don't specifically know how to do this via python..
but, maybe what I've said will send you in the right direction.

-Nate
Alright that's a good suggestion. But the problem I'm having is that
I don't know how to execute the plugin. I think I know how to do the
architecture, just that there needs to be some crazy circular
importing going on and I was wondering if there was a smarter way to
attack the problem.

Thanks,
Nick

Apr 9 '07 #3
Take a look at Trac. This might give you some ideas.

http://trac.edgewall.org/wiki/TracDe...ntArchitecture

Apr 10 '07 #4
On Apr 10, 10:26 pm, c james <cja...@callone.netwrote:
Take a look at Trac. This might give you some ideas.

http://trac.edgewall.org/wiki/TracDe...ntArchitecture
Thanks cJames, that's exactly what I'm looking for.

Jun 5 '07 #5

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

Similar topics

2
by: marco | last post by:
Hi, first of all ; sorry for my poor english ; i'm french ... and i hope you can understand below I use python (and wxpython) on a win32 platform, to build a simple "home theater pc". I want...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
1
by: BillyMac | last post by:
Hello I emply a "plug-in" architecture for a .NET Windows service. At OnStart, the service browses for DLL's in in sub-folders and loads them dyanamically using Assembly.LoadFrom() The...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
2
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get...
4
benchpolo
by: benchpolo | last post by:
I modified my existing MsAccess (.mdb) database with addtional queries and report. When I updated the version in my other pc with the new version when I loaded the database an error popped-out that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.