Server IP : 213.176.29.180 / Your IP : 3.141.25.100 Web Server : Apache System : Linux 213.176.29.180.hostiran.name 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64 User : webtaragh ( 1001) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0755) : /lib/../share/tk8.6/../doc/bind/../libaio/../python3-libs/../python3-zope-component/html/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
<!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 — zope.component 4.3.0 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> <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 <cite>provideUtility</cite> <a class="footnote-reference" href="#id5" id="id1">[1]</a>:</p> <p>In this example we registered the utility as providing the <cite>IGreeter</cite> interface with a name of ‘bob’. We can look the interface up with either <cite>queryUtility</cite> or <cite>getUtility</cite>:</p> <p><cite>queryUtility</cite> and <cite>getUtility</cite> 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 <cite>provideUtility</cite>:</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 <cite>component.adapts</cite> 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 <cite>queryAdapter</cite>, or <cite>getAdapter</cite>:</p> <p>If an adapter can’t be found, <cite>queryAdapter</cite> returns a default value and <cite>getAdapter</cite> 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 <cite>queryMultiAdapter</cite> or <cite>getMultiAdapter</cite>:</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 <cite>job</cite> 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 <cite>IDocumentCreated</cite> 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 <cite>handle</cite> 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"> ©Zope Foundation and Contributors. | Powered by <a href="http://sphinx-doc.org/">Sphinx 1.7.6</a> & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a> | <a href="_sources/narr.rst.txt" rel="nofollow">Page source</a> </div> </body> </html>