Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 16b6c7fca2fc9f56193b382cc05af140 > files > 190

python3-zope-component-4.4.1-3.mga7.noarch.rpm


<!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>Adapter Registration APIs &#8212; 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="Factory APIs" href="factory.html" />
    <link rel="prev" title="Utility Registration APIs" href="utility.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="adapter-registration-apis">
<h1>Adapter Registration APIs<a class="headerlink" href="#adapter-registration-apis" title="Permalink to this headline">¶</a></h1>
<div class="section" id="conforming-adapter-lookup">
<h2>Conforming Adapter Lookup<a class="headerlink" href="#conforming-adapter-lookup" title="Permalink to this headline">¶</a></h2>
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">getAdapterInContext()</span></code> and
<code class="xref py py-func docutils literal notranslate"><span class="pre">queryAdapterInContext()</span></code> APIs first check the
context object to see if it already conforms to the requested interface.
If so, the object is returned immediately.  Otherwise, the adapter factory
is looked up in the site manager, and called.</p>
<p>Let’s start by creating a component that supports the <cite>__conform__()</cite> method:</p>
<p>We also gave the component a custom representation, so it will be easier
to use in these tests.</p>
<p>We now have to create a site manager (other than the default global one)
with which we can register adapters for <cite>I1</cite>.</p>
<p>Now we create a new <cite>context</cite> that knows how to get to our custom site
manager.</p>
<p>If an object implements the interface you want to adapt to,
<cite>getAdapterInContext()</cite> should simply return the object.</p>
<p>If an object conforms to the interface you want to adapt to,
<cite>getAdapterInContext()</cite> should simply return the conformed object.</p>
<p>If an adapter isn’t registered for the given object and interface, and you
provide no default, the <cite>getAdapterInContext</cite> API raises ComponentLookupError:</p>
<p>While the <cite>queryAdapterInContext</cite> API returns the default:</p>
<p>If you ask for an adapter for which something’s registered you get the
registered adapter:</p>
</div>
<div class="section" id="named-adapter-lookup">
<h2>Named Adapter Lookup<a class="headerlink" href="#named-adapter-lookup" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">getAdapter</span></code> and <code class="docutils literal notranslate"><span class="pre">queryAdapter</span></code> API functions are similar to
<code class="docutils literal notranslate"><span class="pre">{get|query}AdapterInContext()</span></code> functions, except that they do not care
about the <code class="docutils literal notranslate"><span class="pre">__conform__()</span></code> but also handle named adapters. (Actually, the
name is a required argument.)</p>
<p>If no adapter is registered for the given object, interface, and name,
<code class="docutils literal notranslate"><span class="pre">getAdapter</span></code> raises <code class="docutils literal notranslate"><span class="pre">ComponentLookupError</span></code>, while <code class="docutils literal notranslate"><span class="pre">queryAdapter</span></code>
returns the default:</p>
<p>The ‘requires’ argument to <cite>registerAdapter</cite> must be a sequence, rather than
a single interface:</p>
<p>After register an adapter from <cite>I1</cite> to <cite>I2</cite> with the global site manager:</p>
<p>We can access the adapter using the <cite>getAdapter()</cite> API:</p>
<p>If we search using a non-anonymous name, before registering:</p>
<p>After registering under that name:</p>
</div>
<div class="section" id="invoking-an-interface-to-perform-adapter-lookup">
<h2>Invoking an Interface to Perform Adapter Lookup<a class="headerlink" href="#invoking-an-interface-to-perform-adapter-lookup" title="Permalink to this headline">¶</a></h2>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.component</span></code> registers an adapter hook with
<code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.interface.interface</span></code>, allowing a convenient spelling for
adapter lookup:  just “call” the interface, passing the context:</p>
<p>If the lookup fails, we get a <cite>TypeError</cite>:</p>
<p>unless we pass a default:</p>
</div>
<div class="section" id="registering-adapters-for-arbitrary-objects">
<h2>Registering Adapters For Arbitrary Objects<a class="headerlink" href="#registering-adapters-for-arbitrary-objects" title="Permalink to this headline">¶</a></h2>
<p>Providing an adapter for None says that your adapter can adapt anything
to <cite>I2</cite>.</p>
<p>It can really adapt any arbitrary object:</p>
</div>
<div class="section" id="looking-up-adapters-using-multiple-objects">
<h2>Looking Up Adapters Using Multiple Objects<a class="headerlink" href="#looking-up-adapters-using-multiple-objects" title="Permalink to this headline">¶</a></h2>
<p>Multi-adapters adapt one or more objects to another interface. To make
this demonstration non-trivial, we need to create a second object to be
adapted:</p>
<p>As with regular adapters, if an adapter isn’t registered for the given
objects and interface, the <code class="xref py py-func docutils literal notranslate"><span class="pre">getMultiAdapter()</span></code> API
raises <cite>ComponentLookupError</cite>:</p>
<p>while the <code class="xref py py-func docutils literal notranslate"><span class="pre">queryMultiAdapter()</span></code> API returns the default:</p>
<p>Note that <code class="docutils literal notranslate"><span class="pre">name</span></code> is not a required attribute here.</p>
<p>To test multi-adapters, we also have to create an adapter class that
handles to context objects:</p>
<p>Now we can register the multi-adapter:</p>
<p>Notice how the required interfaces are simply provided by a tuple.</p>
<p>Now we can get the adapter:</p>
</div>
<div class="section" id="finding-more-than-one-adapter">
<h2>Finding More Than One Adapter<a class="headerlink" href="#finding-more-than-one-adapter" title="Permalink to this headline">¶</a></h2>
<p>It is sometimes desireable to get a list of all adapters that are
registered for a particular output interface, given a set of
objects.</p>
<p>Let’s register some adapters first:</p>
<p>Now we get all the adapters that are registered for <code class="docutils literal notranslate"><span class="pre">ob</span></code> that provide
<code class="docutils literal notranslate"><span class="pre">I5</span></code> (note that the names are always text strings, meaning that on
Python 2 the names will be <code class="docutils literal notranslate"><span class="pre">unicode</span></code>):</p>
<p>Note that the output doesn’t include None values. If an adapter
factory returns None, it is as if it wasn’t present.</p>
</div>
<div class="section" id="subscription-adapters">
<h2>Subscription Adapters<a class="headerlink" href="#subscription-adapters" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="event-handlers">
<h2>Event handlers<a class="headerlink" href="#event-handlers" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="helpers-for-declaring-testing-adapters">
<h2>Helpers for Declaring / Testing Adapters<a class="headerlink" href="#helpers-for-declaring-testing-adapters" title="Permalink to this headline">¶</a></h2>
</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"><a class="reference internal" href="../zcml.html">ZCML directives</a></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 current"><a class="reference internal" href="../api.html"><code class="docutils literal notranslate"><span class="pre">zope.component</span></code> API Reference</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="interfaces.html">Interface Definitions</a></li>
<li class="toctree-l2"><a class="reference internal" href="sitemanager.html">Site Manager APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="utility.html">Utility Registration APIs</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Adapter Registration APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="factory.html">Factory APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="interface.html">Interface Registration APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="security.html">Security APIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="persistent.html">Persistent Registries</a></li>
</ul>
</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><a href="../api.html"><code class="docutils literal notranslate"><span class="pre">zope.component</span></code> API Reference</a><ul>
      <li>Previous: <a href="utility.html" title="previous chapter">Utility Registration APIs</a></li>
      <li>Next: <a href="factory.html" title="next chapter">Factory APIs</a></li>
  </ul></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">
      &copy;Zope Foundation and Contributors.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
      
      |
      <a href="../_sources/api/adapter.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>