On Tue, Dec 15, 2009 at 4:53 PM, Tom Davis <[email protected]> wrote:
> Yeah, I would never rely on the likes of directory sniffing. You're also
> making the assumption that anything in the drivers directory contains a
> driver, the connection class is named a certain way, etc.
If this API is in libcloud, those assumptions will be enforced -- if someone
puts code that doesn't work with that expectation, it'll break and someone
will have to fix it.
> It's full of too
> many holes. Throwing out the subject of interfaces entirely, this is still
> much more reliably achieved by a metaclass.
I don't understand how a metaclass would help here? I'm assuming that
lazily loading modules is a requirement here (i.e., you shouldn't have the
Slicehost module loaded unless you are using Slicehost). So... without
loading the module there's a limit to what you can do.
There are other techniques for loading plugins like this, but I find they
lead to unnecessary overhead and indirection (e.g., egg entry points).
There are perhaps a couple lighter ways that would be possible. E.g.,
drivers (that don't ship with libcloud) could install a .pth file like:
import libcloud_driver_registry; libcloud_driver_registry.register('driver
name', 'module_name')
And then libcloud_driver_registry would be a really small module that just
accepts these registrations. This is if you have drivers located in
third-party packages. With eggs you'd have an entry point, and do
pkg_resources.list_entry_points('libcloud.drivers'). Which... actually
isn't so bad, but the first time you call that all entry points for all
libraries get parsed. But if you support direct import, that overhead will
only be incurred for code that wants to see the available drivers. So...
entry points are reasonable.
One thing I forgot to mention about my example code is it doesn't work with
zipped packages. You can declare libcloud as being zip-unfriendly, or you
can use some fancier APIs that abstract out listdir etc (they are in
pkg_resources somewhere).
--
Ian Bicking | http://blog.ianbicking.org |
http://topplabs.org/civichacker