[Zope-dev] Re: zcml entry points

Tres Seaver tseaver at palladion.com
Mon Oct 22 11:24:50 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martijn Faassen wrote:
> Hey,
> 
> On 10/20/07, Lennart Regebro <regebro at gmail.com> wrote:
>> On 10/20/07, Martijn Faassen <faassen at startifact.com> wrote:
>>> I'd say it is a general concern of a framework to try to avoid how often
>>> you need to repeat yourself. Right now you to use a Zope 3 package you
>>> need to do the following things:
>>>
>>> * list the egg in your setup.py dependencies
>>>
>>> * load the ZCML required
>>>
>>> * import it in your code
>> True. But they do different things.
>>
>> 1. Says "download and install a component"
>> 2. Says "configure a component"
>> 3. Says "use a component"
> 
> Sure, they do different things. Nonetheless, there is repetition that
> could be reduced. In fact
> there's potentially a fourth place now where we reference dependency
> information again:
> 
> 4. Use this version of this component
> 
> (though this may be maintained outside of the actual package)
> 
> In at least 3 places we express dependency information. For different
> *purposes* in each case, but we still state something like:
> 
> 1. "we use dependency X, and please download and install it"
> 2. "we use dependency X, please configure it"
> 3. "we use dependency X, please import the following from it"
> 
> It'd be nice if I only had to say "I want to import from dependency X,
> please make sure it's there and available."

setuptools spells that semantic so:

  >>> import pkg_resources
  >>> loaded = require('zope.interface 3.4.0')

At that point, the 'zope.interface' egg is guaranteed to be on sys.path,
and with the desired version:  the subsequent:

  >>> import zope.interface

will then come from that location.  This presumes that you haven't
already hacked sys.path to include it from somewhere else ('requires'
appends entries there).

Note that if 'grok' itself explicitly pins all its requirements (the
"frozen egg" scenario), then just saying:

  >>> pkg_resources.require('grok')

would have the effect of loading *all* the pinned requirements eggs onto
sys.path.

> Actually this is a little
> bit like zc.resourcelibrary can make resources appear on your page
> headers when you write code that needs it.
> 
> Of course this is sometimes not possible, as there's not enough
> information available, or there are multiple separate ways to
> configure it.
> 
> But there is significant repetition spread out across the codebase.
> It's not in one place, which forces a repetition in referring to the
> component being installed/configured/imported. My response is to think
> about ways to reduce this (while of course, looking for ways to retain
> power and flexibility).

If you expose an 'entry point group" for the automagic ZCML loader to
use, then you don't require any repetition in the client code, because
the plugin provide conforms to a standard interface which allows discovery::

 # Assume that plugins register one or more entry points for the group,
 # 'plone.automatic_zcml', where each entry point is a module name whose
 # ZCML is to be loaded.
 >>> from zope.configuration.xmlconfig import xmlconfig
 >>> for ep in pkg_resources.iter_entry_points('plone.automatic_zcml'):
 ...      module_name = ep.module_name
 ...      # trigger ZCML loading here using 'module_name'
 ...      stream = pkg_resources.resource_stream(module_name,
 ...                                             'configure.zcml')
 ...      xmlconfig(stream)

[1]
http://peak.telecommunity.com/DevCenter/setuptools#dynamic-discovery-of-services-and-plugins

[2]
http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources



Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHHMDB+gerLs4ltQ4RAsGRAKCqpHz3HJjzlscWDAlrXnygKXlNIwCffSsn
aumELzdGgsHQoZDDPSPoTSw=
=GHqp
-----END PGP SIGNATURE-----



More information about the Zope-Dev mailing list