How to load plugins as objects?

C

censored

Hi. I'm hoping for a bit of programming guidance here ....

I have an application I wrote (it's an irc bot fyi), with which I want to
use plugins.

Basically, I want there to be directory called plugins/ .

In plugins, there should be a bunch of *.py files. Each file should
contain a single class.

When the application launches, I want the interpreter to scan the
plugins/ dir for *.py files, and load each class in each every *.py file
as an object an object in memory.

I want it to so that every time I want the app to perform a new function,
I just write a plugin and stick it in the plugins/ directory.

What's the easiest way to go about this?
 
F

Fredrik Lundh

censored said:
Hi. I'm hoping for a bit of programming guidance here ....

I have an application I wrote (it's an irc bot fyi), with which I want to
use plugins.

Basically, I want there to be directory called plugins/ .

In plugins, there should be a bunch of *.py files. Each file should
contain a single class.

When the application launches, I want the interpreter to scan the
plugins/ dir for *.py files, and load each class in each every *.py file
as an object an object in memory.

I want it to so that every time I want the app to perform a new function,
I just write a plugin and stick it in the plugins/ directory.

What's the easiest way to go about this?

for file in glob.glob("plugins/*.py"):
ns = {}
execfile(file, ns)
for name, object in ns.items():
# pick out interesting objects

</F>
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top