<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ZCML directives — zope.component 4.4.1 documentation</title> <link rel="stylesheet" href="_static/alabaster.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/language_data.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="Package configuration" href="configure.html" /> <link rel="prev" title="Persistent Component Management" href="persistentregistry.html" /> <link rel="stylesheet" href="_static/custom.css" type="text/css" /> <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" /> </head><body> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="zcml-directives"> <h1>ZCML directives<a class="headerlink" href="#zcml-directives" title="Permalink to this headline">¶</a></h1> <p>Components may be registered using the registration API exposed by <code class="docutils literal notranslate"><span class="pre">zope.component</span></code> (provideAdapter, provideUtility, etc.). They may also be registered using configuration files. The common way to do that is by using ZCML (Zope Configuration Markup Language), an XML spelling of component registration.</p> <p>In ZCML, each XML element is a <em>directive</em>. There are different top-level directives that let us register components. We will introduce them one by one here.</p> <p>This helper will let us easily execute ZCML snippets:</p> <div class="section" id="adapter"> <h2>adapter<a class="headerlink" href="#adapter" title="Permalink to this headline">¶</a></h2> <p>Adapters play a key role in the Component Architecture. In ZCML, they are registered with the <adapter /> directive.</p> <p>Before we register the first test adapter, we can verify that adapter lookup doesn’t work yet:</p> <p>Then we register the adapter and see that the lookup works:</p> <p>It is also possible to give adapters names. Then the combination of required interface, provided interface and name makes the adapter lookup unique. The name is supplied using the <code class="docutils literal notranslate"><span class="pre">name</span></code> argument to the <adapter /> directive:</p> <div class="section" id="adapter-factories"> <h3>Adapter factories<a class="headerlink" href="#adapter-factories" title="Permalink to this headline">¶</a></h3> <p>It is possible to supply more than one adapter factory. In this case, during adapter lookup each factory will be called and the return value will be given to the next factory. The return value of the last factory is returned as the result of the adapter lookup. For examle:</p> <p>The resulting adapter is an A3, around an A2, around an A1, around the adapted object:</p> <p>Of course, if no factory is provided at all, we will get an error:</p> </div> <div class="section" id="declaring-for-provides-and-name-in-python"> <h3>Declaring <code class="docutils literal notranslate"><span class="pre">for</span></code>, <code class="docutils literal notranslate"><span class="pre">provides</span></code> and <code class="docutils literal notranslate"><span class="pre">name</span></code> in Python<a class="headerlink" href="#declaring-for-provides-and-name-in-python" title="Permalink to this headline">¶</a></h3> <p>The <adapter /> directive can figure out from the in-line Python declaration (using <code class="docutils literal notranslate"><span class="pre">zope.component.adapts()</span></code> or <code class="docutils literal notranslate"><span class="pre">zope.component.adapter()</span></code>, <code class="docutils literal notranslate"><span class="pre">zope.interface.implements</span></code> as well as <code class="docutils literal notranslate"><span class="pre">zope.component.named</span></code>) what the adapter should be registered for and what it provides:</p> <p>Of course, if the adapter has no <code class="docutils literal notranslate"><span class="pre">implements()</span></code> declaration, ZCML can’t figure out what it provides:</p> <p>On the other hand, if the factory implements more than one interface, ZCML can’t figure out what it should provide either:</p> <p>Let’s now register an adapter that has a name specified in Python:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">runSnippet</span><span class="p">(</span><span class="s1">'''</span> <span class="gp">... </span><span class="s1"> <adapter factory="zope.component.testfiles.components.Comp4" />'''</span><span class="p">)</span> </pre></div> </div> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">getAdapter</span><span class="p">(</span><span class="n">Content</span><span class="p">(),</span> <span class="n">IApp</span><span class="p">,</span> <span class="s1">'app'</span><span class="p">)</span><span class="o">.</span><span class="vm">__class__</span> <span class="go"><class 'zope.component.testfiles.components.Comp4'></span> </pre></div> </div> <p>A not so common edge case is registering adapters directly for classes, not for interfaces. For example:</p> <p>This time, any object providing <code class="docutils literal notranslate"><span class="pre">IContent</span></code> won’t work if it’s not an instance of the <code class="docutils literal notranslate"><span class="pre">Content</span></code> class:</p> </div> <div class="section" id="multi-adapters"> <h3>Multi-adapters<a class="headerlink" href="#multi-adapters" title="Permalink to this headline">¶</a></h3> <p>Conventional adapters adapt one object to provide another interface. Multi-adapters adapt several objects at once:</p> <p>You can even adapt an empty list of objects (we call this a null-adapter):</p> <p>Even with multi-adapters, ZCML can figure out the <code class="docutils literal notranslate"><span class="pre">for</span></code> and <code class="docutils literal notranslate"><span class="pre">provides</span></code> parameters from the Python declarations:</p> <p>Chained factories are not supported for multi-adapters, though:</p> <p>And neither for null-adapters:</p> </div> <div class="section" id="protected-adapters"> <h3>Protected adapters<a class="headerlink" href="#protected-adapters" title="Permalink to this headline">¶</a></h3> <p>Adapters can be protected with a permission. First we have to define a permission for which we’ll have to register the <permission /> directive:</p> <p>We see that the adapter is a location proxy now so that the appropriate permissions can be found from the context:</p> <p>We can also go about it a different way. Let’s make a public adapter and wrap the adapter in a security proxy. That often happens when an adapter is turned over to untrusted code:</p> <p>Of course, this still works when we let the ZCML directive handler figure out <code class="docutils literal notranslate"><span class="pre">for</span></code> and <code class="docutils literal notranslate"><span class="pre">provides</span></code> from the Python declarations:</p> <p>It also works with multi adapters:</p> <p>It’s probably not worth mentioning, but when we try to protect an adapter with a permission that doesn’t exist, we’ll obviously get an error:</p> </div> <div class="section" id="trusted-adapters"> <h3>Trusted adapters<a class="headerlink" href="#trusted-adapters" title="Permalink to this headline">¶</a></h3> <p>Trusted adapters are adapters that are trusted to do anything with the objects they are given so that these objects are not security-proxied. They are registered using the <code class="docutils literal notranslate"><span class="pre">trusted</span></code> argument to the <adapter /> directive:</p> <p>With an unproxied object, it’s business as usual:</p> <p>With a security-proxied object, however, we get a security-proxied adapter:</p> <p>While the adapter is security-proxied, the object it adapts is now proxy-free. The adapter has umlimited access to it:</p> <p>We can also protect the trusted adapter with a permission:</p> <p>Again, with an unproxied object, it’s business as usual:</p> <p>With a security-proxied object, we again get a security-proxied adapter:</p> <p>Since we protected the adapter with a permission, we now encounter a location proxy behind the security proxy:</p> <p>There’s one exception to all of this: When you use the public permission (<code class="docutils literal notranslate"><span class="pre">zope.Public</span></code>), there will be no location proxy:</p> <p>We can also explicitply pass the <code class="docutils literal notranslate"><span class="pre">locate</span></code> argument to make sure we get location proxies:</p> </div> </div> <div class="section" id="subscriber"> <h2>subscriber<a class="headerlink" href="#subscriber" title="Permalink to this headline">¶</a></h2> <p>With the <subscriber /> directive you can register subscription adapters or event subscribers with the adapter registry. Consider this very typical example of a <subscriber /> directive:</p> <p>Note how ZCML provides some additional information when registering components, such as the ZCML filename and line numbers:</p> <p>The “fun” behind subscription adapters/subscribers is that when several ones are declared for the same for/provides, they are all found. With regular adapters, the most specific one (and in doubt the one registered last) wins. Consider these two subscribers:</p> <div class="section" id="declaring-for-and-provides-in-python"> <h3>Declaring <code class="docutils literal notranslate"><span class="pre">for</span></code> and <code class="docutils literal notranslate"><span class="pre">provides</span></code> in Python<a class="headerlink" href="#declaring-for-and-provides-in-python" title="Permalink to this headline">¶</a></h3> <p>Like the <adapter /> directive, the <subscriber /> directive can figure out from the in-line Python declaration (using <code class="docutils literal notranslate"><span class="pre">zope.component.adapts()</span></code> or <code class="docutils literal notranslate"><span class="pre">zope.component.adapter()</span></code>) what the subscriber should be registered for:</p> <p>In the same way the directive can figure out what a subscriber provides:</p> <p>A not so common edge case is declaring subscribers directly for classes, not for interfaces. For example:</p> <p>This time, any object providing <code class="docutils literal notranslate"><span class="pre">IContent</span></code> won’t work if it’s not an instance of the <code class="docutils literal notranslate"><span class="pre">Content</span></code> class:</p> </div> <div class="section" id="protected-subscribers"> <h3>Protected subscribers<a class="headerlink" href="#protected-subscribers" title="Permalink to this headline">¶</a></h3> <p>Subscribers can also be protected with a permission. First we have to define a permission for which we’ll have to register the <permission /> directive:</p> </div> <div class="section" id="trusted-subscribers"> <h3>Trusted subscribers<a class="headerlink" href="#trusted-subscribers" title="Permalink to this headline">¶</a></h3> <p>Like trusted adapters, trusted subscribers are subscribers that are trusted to do anything with the objects they are given so that these objects are not security-proxied. In analogy to the <adapter /> directive, they are registered using the <code class="docutils literal notranslate"><span class="pre">trusted</span></code> argument to the <subscriber /> directive:</p> <p>With an unproxied object, it’s business as usual:</p> <p>Now with a proxied object. We will see that the subscriber has unproxied access to it, but the subscriber itself is proxied:</p> <p>There’s no location proxy behind the security proxy:</p> <p>If you want the trusted subscriber to be located, you’ll also have to use the <code class="docutils literal notranslate"><span class="pre">locate</span></code> argument:</p> <p>Again, it’s business as usual with an unproxied object:</p> <p>With a proxied object, we again get a security-proxied subscriber:</p> <p>However, thanks to the <code class="docutils literal notranslate"><span class="pre">locate</span></code> argument, we now have a location proxy behind the security proxy:</p> </div> <div class="section" id="event-subscriber-handlers"> <h3>Event subscriber (handlers)<a class="headerlink" href="#event-subscriber-handlers" title="Permalink to this headline">¶</a></h3> <p>Sometimes, subscribers don’t need to be adapters that actually provide anything. It’s enough that a callable is called for a certain event.</p> <p>In this case, simply getting the subscribers is enough to invoke them:</p> </div> </div> <div class="section" id="utility"> <h2>utility<a class="headerlink" href="#utility" title="Permalink to this headline">¶</a></h2> <p>Apart from adapters (and subscription adapters), the Component Architecture knows a second kind of component: utilities. They are registered using the <utility /> directive.</p> <p>Before we register the first test utility, we can verify that utility lookup doesn’t work yet:</p> <p>Then we register the utility:</p> <p>Like adapters, utilities can also have names. There can be more than one utility registered for a certain interface, as long as they each have a different name.</p> <p>First, we make sure that there’s no utility yet:</p> <p>Then we register it:</p> <p>Utilities can also be registered from a factory. In this case, the ZCML handler calls the factory (without any arguments) and registers the returned value as a utility. Typically, you’d pass a class for the factory:</p> <div class="section" id="declaring-provides-in-python"> <h3>Declaring <code class="docutils literal notranslate"><span class="pre">provides</span></code> in Python<a class="headerlink" href="#declaring-provides-in-python" title="Permalink to this headline">¶</a></h3> <p>Like other directives, <utility /> can also figure out which interface a utility provides from the Python declaration:</p> <p>It won’t work if the component that is to be registered doesn’t provide anything:</p> <p>Or if more than one interface is provided (then the ZCML directive handler doesn’t know under which the utility should be registered):</p> <p>We can repeat the same drill for utility factories:</p> </div> <div class="section" id="declaring-name-in-python"> <h3>Declaring <code class="docutils literal notranslate"><span class="pre">name</span></code> in Python<a class="headerlink" href="#declaring-name-in-python" title="Permalink to this headline">¶</a></h3> <p>Let’s now register a utility that has a name specified in Python:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">runSnippet</span><span class="p">(</span><span class="s1">'''</span> <span class="gp">... </span><span class="s1"> <utility component="zope.component.testfiles.components.comp4" />'''</span><span class="p">)</span> </pre></div> </div> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">zope.component.testfiles.components</span> <span class="k">import</span> <span class="n">comp4</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">getUtility</span><span class="p">(</span><span class="n">IApp</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'app'</span><span class="p">)</span> <span class="ow">is</span> <span class="n">comp4</span> <span class="go">True</span> </pre></div> </div> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">runSnippet</span><span class="p">(</span><span class="s1">'''</span> <span class="gp">... </span><span class="s1"> <utility factory="zope.component.testfiles.components.Comp4" />'''</span><span class="p">)</span> </pre></div> </div> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">getUtility</span><span class="p">(</span><span class="n">IApp</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'app'</span><span class="p">)</span> <span class="ow">is</span> <span class="n">comp4</span> <span class="go">False</span> <span class="gp">>>> </span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">getUtility</span><span class="p">(</span><span class="n">IApp</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'app'</span><span class="p">)</span><span class="o">.</span><span class="vm">__class__</span> <span class="go"><class 'zope.component.testfiles.components.Comp4'></span> </pre></div> </div> </div> <div class="section" id="protected-utilities"> <h3>Protected utilities<a class="headerlink" href="#protected-utilities" title="Permalink to this headline">¶</a></h3> <p>TODO:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">testProtectedUtility</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="sd">"""Test that we can protect a utility.</span> <span class="sd"> Also:</span> <span class="sd"> Check that multiple configurations for the same utility and</span> <span class="sd"> don't interfere.</span> <span class="sd"> """</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">queryUtility</span><span class="p">(</span><span class="n">IV</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span> <span class="n">xmlconfig</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">template</span> <span class="o">%</span> <span class="p">(</span> <span class="sd">'''</span> <span class="sd"> <permission id="tell.everyone" title="Yay" /></span> <span class="sd"> <utility</span> <span class="sd"> component="zope.component.testfiles.components.comp"</span> <span class="sd"> provides="zope.component.testfiles.components.IApp"</span> <span class="sd"> permission="tell.everyone"</span> <span class="sd"> /></span> <span class="sd"> <permission id="top.secret" title="shhhh" /></span> <span class="sd"> <utility</span> <span class="sd"> component="zope.component.testfiles.components.comp"</span> <span class="sd"> provides="zope.component.testfiles.components.IAppb"</span> <span class="sd"> permission="top.secret"</span> <span class="sd"> /></span> <span class="sd"> '''</span> <span class="p">)))</span> <span class="n">utility</span> <span class="o">=</span> <span class="n">ProxyFactory</span><span class="p">(</span><span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">getUtility</span><span class="p">(</span><span class="n">IApp</span><span class="p">))</span> <span class="n">items</span> <span class="o">=</span> <span class="n">getTestProxyItems</span><span class="p">(</span><span class="n">utility</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">items</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'tell.everyone'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'f'</span><span class="p">,</span> <span class="s1">'tell.everyone'</span><span class="p">)</span> <span class="p">])</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">removeSecurityProxy</span><span class="p">(</span><span class="n">utility</span><span class="p">),</span> <span class="n">comp</span><span class="p">)</span> <span class="k">def</span> <span class="nf">testUtilityUndefinedPermission</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">config</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="n">template</span> <span class="o">%</span> <span class="p">(</span> <span class="sd">'''</span> <span class="sd"> <utility</span> <span class="sd"> component="zope.component.testfiles.components.comp"</span> <span class="sd"> provides="zope.component.testfiles.components.IApp"</span> <span class="sd"> permission="zope.UndefinedPermission"</span> <span class="sd"> /></span> <span class="sd"> '''</span> <span class="p">))</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="n">xmlconfig</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="n">testing</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="section" id="interface"> <h2>interface<a class="headerlink" href="#interface" title="Permalink to this headline">¶</a></h2> <p>The <interface /> directive lets us register an interface. Interfaces are registered as named utilities. We therefore needn’t go though all the lookup details again, it is sufficient to see whether the directive handler emits the right actions.</p> <p>First we provide a stub configuration context:</p> <p>Then we provide a test interface that we’d like to register:</p> <p>It doesn’t yet provide <code class="docutils literal notranslate"><span class="pre">ITestType</span></code>:</p> <p>However, after calling the directive handler…</p> <p>…it does provide <code class="docutils literal notranslate"><span class="pre">ITestType</span></code>:</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h1 class="logo"><a href="index.html">zope.component</a></h1> <h3>Navigation</h3> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="narr.html">Zope Component Architecture</a></li> <li class="toctree-l1"><a class="reference internal" href="socketexample.html">The Zope 3 Component Architecture (Socket Example)</a></li> <li class="toctree-l1"><a class="reference internal" href="event.html">Events</a></li> <li class="toctree-l1"><a class="reference internal" href="factory.html">Factories</a></li> <li class="toctree-l1"><a class="reference internal" href="persistentregistry.html">Persistent Component Management</a></li> <li class="toctree-l1 current"><a class="current reference internal" href="#">ZCML directives</a><ul> <li class="toctree-l2"><a class="reference internal" href="#adapter">adapter</a></li> <li class="toctree-l2"><a class="reference internal" href="#subscriber">subscriber</a></li> <li class="toctree-l2"><a class="reference internal" href="#utility">utility</a></li> <li class="toctree-l2"><a class="reference internal" href="#interface">interface</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="configure.html">Package configuration</a></li> <li class="toctree-l1"><a class="reference internal" href="hooks.html">The current component registry</a></li> <li class="toctree-l1"><a class="reference internal" href="testlayer.html">Layers</a></li> <li class="toctree-l1"><a class="reference internal" href="api.html"><code class="docutils literal notranslate"><span class="pre">zope.component</span></code> API Reference</a></li> <li class="toctree-l1"><a class="reference internal" href="hacking.html">Hacking on <code class="docutils literal notranslate"><span class="pre">zope.component</span></code></a></li> </ul> <div class="relations"> <h3>Related Topics</h3> <ul> <li><a href="index.html">Documentation overview</a><ul> <li>Previous: <a href="persistentregistry.html" title="previous chapter">Persistent Component Management</a></li> <li>Next: <a href="configure.html" title="next chapter">Package configuration</a></li> </ul></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="footer"> ©Zope Foundation and Contributors. | Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a> & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a> | <a href="_sources/zcml.rst.txt" rel="nofollow">Page source</a> </div> </body> </html>