Sophie

Sophie

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

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>Zope Component Architecture &#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="The Zope 3 Component Architecture (Socket Example)" href="socketexample.html" />
    <link rel="prev" title="zope.component" href="index.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="zope-component-architecture">
<h1>Zope Component Architecture<a class="headerlink" href="#zope-component-architecture" title="Permalink to this headline">¶</a></h1>
<p>This package, together with <cite>zope.interface</cite>, provides facilities for
defining, registering and looking up components.  There are two basic
kinds of components: adapters and utilities.</p>
<div class="section" id="utilities">
<h2>Utilities<a class="headerlink" href="#utilities" title="Permalink to this headline">¶</a></h2>
<p>Utilities are just components that provide an interface and that are
looked up by an interface and a name.  Let’s look at a trivial utility
definition:</p>
<p>We can register an instance this class using <code class="xref py py-func docutils literal notranslate"><span class="pre">provideUtility()</span></code> <a class="footnote-reference" href="#id5" id="id1">[1]</a>:</p>
<p>In this example we registered the utility as providing the <code class="docutils literal notranslate"><span class="pre">IGreeter</span></code>
interface with a name of ‘bob’. We can look the interface up with
either <code class="xref py py-func docutils literal notranslate"><span class="pre">queryUtility()</span></code> or <code class="xref py py-func docutils literal notranslate"><span class="pre">getUtility()</span></code>:</p>
<p><code class="xref py py-func docutils literal notranslate"><span class="pre">queryUtility()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">getUtility()</span></code> differ in how failed lookups are handled:</p>
<p>If a component provides only one interface, as in the example above,
then we can omit the provided interface from the call to <code class="xref py py-func docutils literal notranslate"><span class="pre">provideUtility()</span></code>:</p>
<p>The name defaults to an empty string:</p>
</div>
<div class="section" id="adapters">
<h2>Adapters<a class="headerlink" href="#adapters" title="Permalink to this headline">¶</a></h2>
<p>Adapters are components that are computed from other components to
adapt them to some interface.  Because they are computed from other
objects, they are provided as factories, usually classes.  Here, we’ll
create a greeter for persons, so we can provide personalized greetings
for different people:</p>
<p>The class defines a constructor that takes an argument for every
object adapted.</p>
<p>We used <code class="xref py py-func docutils literal notranslate"><span class="pre">adapter()</span></code> to declare what we adapt.  We can find
out if an object declares that it adapts anything using adaptedBy:</p>
<p>If an object makes no declaration, then None is returned:</p>
<p>If we declare the interfaces adapted and if we provide only one
interface, as in the example above, then we can provide the adapter
very simply <a class="footnote-reference" href="#id5" id="id2">[1]</a>:</p>
<p>For adapters that adapt a single interface to a single interface
without a name, we can get the adapter by simply calling the
interface:</p>
<p>We can also provide arguments to be very specific about what
how to register the adapter.</p>
<p>The arguments can also be provided as keyword arguments:</p>
<p>For named adapters, use <code class="xref py py-func docutils literal notranslate"><span class="pre">queryAdapter()</span></code>, or <code class="xref py py-func docutils literal notranslate"><span class="pre">getAdapter()</span></code>:</p>
<p>If an adapter can’t be found, <code class="xref py py-func docutils literal notranslate"><span class="pre">queryAdapter()</span></code> returns a default value
and <code class="xref py py-func docutils literal notranslate"><span class="pre">getAdapter()</span></code> raises an error:</p>
<p>Adapters can adapt multiple objects:</p>
<p>Note that the declaration-order of the Interfaces beeing adapted to is
important for adapter look up. It must be the the same as the order of
parameters given to the adapter and used to query the adapter. This is
especially the case when different Interfaces are adapt to (opposed to
this example).</p>
<p>To look up a multi-adapter, use either <code class="xref py py-func docutils literal notranslate"><span class="pre">queryMultiAdapter()</span></code> or
<code class="xref py py-func docutils literal notranslate"><span class="pre">getMultiAdapter()</span></code>:</p>
<p>Adapters need not be classes.  Any callable will do.  We use the
adapter decorator to declare that a callable object adapts some interfaces
(or classes):</p>
<p>In this example, the personJob function simply returns the person’s
<code class="docutils literal notranslate"><span class="pre">job</span></code> attribute if present, or None if it’s not present.  An adapter
factory can return None to indicate that adaptation wasn’t possible.
Let’s register this adapter and try it out:</p>
<p>The adaptation failed because sally didn’t have a job.  Let’s give her
one:</p>
</div>
<div class="section" id="subscription-adapters">
<h2>Subscription Adapters<a class="headerlink" href="#subscription-adapters" title="Permalink to this headline">¶</a></h2>
<p>Unlike regular adapters, subscription adapters are used when we want
all of the adapters that adapt an object to a particular adapter.</p>
<p>Consider a validation problem.  We have objects and we want to assess
whether they meet some sort of standards.  We define a validation
interface:</p>
<p>Perhaps we have documents:</p>
<p>Now, we may want to specify various validation rules for
documents. For example, we might require that the summary be a single
line:</p>
<p>Or we might require the body to be at least 1000 characters in length:</p>
<p>We can register these as subscription adapters <a class="footnote-reference" href="#id5" id="id3">[1]</a>:</p>
<p>We can then use the subscribers to validate objects:</p>
</div>
<div class="section" id="handlers">
<h2>Handlers<a class="headerlink" href="#handlers" title="Permalink to this headline">¶</a></h2>
<p>Handlers are subscription adapter factories that don’t produce
anything.  They do all of their work when called.  Handlers
are typically used to handle events.</p>
<p>Event subscribers are different from other subscription adapters in
that the caller of event subscribers doesn’t expect to interact with
them in any direct way.  For example, an event publisher doesn’t
expect to get any return value.  Because subscribers don’t need to
provide an API to their callers, it is more natural to define them
with functions, rather than classes.  For example, in a
document-management system, we might want to record creation times for
documents:</p>
<p>In this example, we have a function that takes an event and performs
some processing.  It doesn’t actually return anything.  This is a
special case of a subscription adapter that adapts an event to
nothing.  All of the work is done when the adapter “factory” is
called.  We call subscribers that don’t actually create anything
“handlers”.  There are special APIs for registering and calling
them.</p>
<p>To register the subscriber above, we define a document-created event:</p>
<p>We’ll also change our handler definition to:</p>
<p>This marks the handler as an adapter of <code class="docutils literal notranslate"><span class="pre">IDocumentCreated</span></code> events.</p>
<p>Now we’ll register the handler  <a class="footnote-reference" href="#id5" id="id4">[1]</a>:</p>
<p>Now, if we can create an event and use the <code class="xref py py-func docutils literal notranslate"><span class="pre">handle()</span></code> function to call
handlers registered for the event:</p>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id2">2</a>, <a class="fn-backref" href="#id3">3</a>, <a class="fn-backref" href="#id4">4</a>)</em> CAUTION: This API should only be used from test or
application-setup code. This API shouldn’t be used by regular
library modules, as component registration is a configuration
activity.</td></tr>
</tbody>
</table>
</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 current"><a class="current reference internal" href="#">Zope Component Architecture</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#utilities">Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="#adapters">Adapters</a></li>
<li class="toctree-l2"><a class="reference internal" href="#subscription-adapters">Subscription Adapters</a></li>
<li class="toctree-l2"><a class="reference internal" href="#handlers">Handlers</a></li>
</ul>
</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"><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="index.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">zope.component</span></code></a></li>
      <li>Next: <a href="socketexample.html" title="next chapter">The Zope 3 Component Architecture (Socket Example)</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">
      &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/narr.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>