After some searching and asking around I didn’t find any good
explanation of the simplest way to implement plugins in Python. So, for
posterity’s sake, here’s my solution. I’m sure there’s a better way: I’d
love to hear your suggestions.
First, the requirements. The code cannot have knowledge of how the
plugins are named (.so files, .py, package dirs, etc.). I don’t want to
hard-code the list of plugins, as this defeats its dynamic nature
altogether. I have to be able to iterate across all plugins. Any user
should be able to use the module without knowing that it’s got plugins.
Finally, it’s got to be as simple as possible.
First up, we have whatever/__init__.py
:
import pkgutil
import imp
import os
plugin_path = [os.path.join(__path__[0], "plugins/")]
for loader, name, ispkg in pkgutil.iter_modules(plugin_path):
file, pathname, desc = imp.find_module(name, plugin_path)
imp.load_module(name, file, pathname, desc)
This basically uses Python’s module search to find all contents of the
plugins
sub-directory and load them. Now we have some more scaffolding
in the same directory, as whatever/whatever.py
:
plugins = []
class plugin(object):
"""Abstract plugin base class."""
...
def register_plugin(plugin)
global plugins
plugins += [ plugin ]
# utility functions to iterate across and use plugins...
Finally, each plugin looks something like this, in plugins/foo.py
:
from whatever/whatever import *
class fooplugin(plugin):
"""Concrete class for foo plugin."""
...
register_plugin(fooplugin)
Simple enough, but it took a while to work it out. Unfortunately, it
doesn’t seem possible to merge whatever.py
into __init__.py
as we
have a recursive import problem.
Best
cake ever.
An amusing prank. I
love how they get back in, as if that’ll somehow help.
Reading through this
bug is seriously
depressing to read (until Michael Matz steps in).
It was always complete madness to strip symbols from shipping binaries;
it was always
madness to disable the frame pointer too. (The debuginfo trick is an
excellent solution
to the problems of source-level debugging, by the way, it’s just taken
way, way, too far).
This is why you have to install a 1Gb debuginfo RPM in order to run
OProfile on the kernel. Crazy.
It’s a good read if you like to see certain people behaving like asshats
to their fellow community
members too.
My favourite quote from I Am Neurotic:
“Everyday, at 7:47 am or pm, I always say Boeing, after the Airplane,
and I don’t know why. I’ve gotten into a lot of trouble, and I even say
it in my sleep.”
It’s already extremely difficult to watch US TV, with adverts every 7
minutes, and massive animated
station idents taking up half the screen, but this is particularly
obnoxious:
It’s as if they want you not to
watch.
This T-shirt: yes, or
no?
I only got most of the videos in this music video:
but it’s still good. The song’s not up to much though.
Is CHRISTIAN electronic music the new
underground?
“It used to be that all of the good
new music was in the rave scene and then would trickle out into the
internet and store shelves. Now when I look for traditional electronic
music anywhere, I find that the music seems generally boring or has too
much of a political or sinful feel."
I hate that traditional electronic boring, political, or sinful music
too.
I’m getting more than a little bit tired of Nintendo playing its bloody
games. Yes,
we get it Nintendo, your stuff is popular. Now, please make enough
copies of
Mario Kart and Wii Fit to sell them. It’s not even Christmas.
Mario Kart isn’t even in shop charts. I’ve been past several shops
selling Wii
stuff recently (Game, supermarkets, etc.) and most of them don’t even
have any
*mention* of Mario Kart. This is insane.
Waaa.