[Twisted-Python] General question on components, interfaces, etc.

Terry Jones terry at jon.es
Mon Jun 19 15:59:49 MDT 2006


>>>>> "glyph" == glyph  <glyph at divmod.com> writes:
glyph> It allows you to use the same language to talk about plugins as you
glyph> would about any other kind of adapter.  Effectively, the plugin
glyph> system is a way to adapt your "system environment" to an object
glyph> which provides a given interface.  If you use the twisted plugin
glyph> system (or some other system which uses Interface objects) then it
glyph> is pretty easy to factor your code such that implementations of the
glyph> desired interface can be obtained implicitly through the environment
glyph> or explicitly by user code passing in an implementation -- or by
glyph> passing in an object that is adaptable to that interface, and then
glyph> doing adaptation.

Hi, thanks for the explanation. I have another basic question...

If were not using a component-based approach and instead I simply
implemented the interface I wanted (in, say, myImp.py), then I could
"import myImp" in multiple source files and everything would be fairly
straightforward (if inflexible when it came to using different
implementations).

If I allow a user or sysadmin to specify the implementation to use on the
command line, then my main() in main.py can use some kind of plugin
approach to find and load the right source file. That's presumably going to
use some form of __import__, such as

  myImp = __import__(....)

so I then have a module-level global variable in main.py called myImp. But
my other source files do not yet know what to import.

It seems like an obvious solution is to have other source files that want
to use the implementation refer to main.myImp instead of importing anything
themselves.  Or, equivalently, one could have a plugin.py module with an
init() function that loads all required plugins and makes them accessible
as module-level global variables for others to use to get to classes and
methods in the loaded implementation.

Is that the recommended way to deal with accessing dynamically imported
components?

Regards,
Terry




More information about the Twisted-Python mailing list