MINI MINI MANI MO

Path : /usr/share/doc/python-zope-component-4.1.0/html/
File Upload :
Current File : //usr/share/doc/python-zope-component-4.1.0/html/hooks.html


<!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="Content-Type" content="text/html; charset=utf-8" />
    
    <title>The current component registry &mdash; zope.configuration 4.0 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '4.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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="top" title="zope.configuration 4.0 documentation" href="index.html" />
    <link rel="next" title="Layers" href="testlayer.html" />
    <link rel="prev" title="Package configuration" href="configure.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="testlayer.html" title="Layers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="configure.html" title="Package configuration"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">zope.configuration 4.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="the-current-component-registry">
<h1>The current component registry<a class="headerlink" href="#the-current-component-registry" title="Permalink to this headline">¶</a></h1>
<p>There can be any number of component registries in an application. One of them
is the global component registry, and there is also the concept of a currently
used component registry. Component registries other than the global one are
associated with objects called sites. The <tt class="docutils literal"><span class="pre">zope.component.hooks</span></tt> module
provides an API to set and access the current site as well as manipulate the
adapter hook associated with it.</p>
<p>As long as we haven&#8217;t set a site, none is being considered current:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">zope.component.hooks</span> <span class="kn">import</span> <span class="n">getSite</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
</pre></div>
</div>
<p>We can also ask for the current component registry (aka site manager
historically); it will return the global one if no current site is set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">zope.component.hooks</span> <span class="kn">import</span> <span class="n">getSiteManager</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getSiteManager</span><span class="p">()</span>
<span class="go">&lt;BaseGlobalComponents base&gt;</span>
</pre></div>
</div>
<p>Let&#8217;s set a site now. A site has to be an object that provides the
<tt class="docutils literal"><span class="pre">getSiteManager</span></tt> method, which is specified by
<tt class="docutils literal"><span class="pre">zope.component.interfaces.IPossibleSite</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">zope.interface.registry</span> <span class="kn">import</span> <span class="n">Components</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Site</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">registry</span> <span class="o">=</span> <span class="n">Components</span><span class="p">(</span><span class="s">&#39;components&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">getSiteManager</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">registry</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">zope.component.hooks</span> <span class="kn">import</span> <span class="n">setSite</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site1</span> <span class="o">=</span> <span class="n">Site</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">setSite</span><span class="p">(</span><span class="n">site1</span><span class="p">)</span>
</pre></div>
</div>
<p>After this, the newly set site is considered the currently active one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">getSite</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site1</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getSiteManager</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site1</span><span class="o">.</span><span class="n">registry</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If we set another site, that one will be considered current:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">site2</span> <span class="o">=</span> <span class="n">Site</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site2</span><span class="o">.</span><span class="n">registry</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">site1</span><span class="o">.</span><span class="n">registry</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">setSite</span><span class="p">(</span><span class="n">site2</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">getSite</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site2</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getSiteManager</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site2</span><span class="o">.</span><span class="n">registry</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Finally we can unset the site and the global component registry is used again:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">setSite</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getSiteManager</span><span class="p">()</span>
<span class="go">&lt;BaseGlobalComponents base&gt;</span>
</pre></div>
</div>
<div class="section" id="context-manager">
<h2>Context manager<a class="headerlink" href="#context-manager" title="Permalink to this headline">¶</a></h2>
<p>There also is a context manager for setting the site, which is especially
useful when writing tests:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">zope.component.hooks</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">hooks</span><span class="o">.</span><span class="n">site</span><span class="p">(</span><span class="n">site2</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">getSite</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site2</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
</pre></div>
</div>
<p>The site is properly restored even if the body of the with statement
raises an exception:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">zope</span><span class="o">.</span><span class="n">component</span><span class="o">.</span><span class="n">hooks</span><span class="o">.</span><span class="n">site</span><span class="p">(</span><span class="n">site2</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">getSite</span><span class="p">()</span> <span class="ow">is</span> <span class="n">site2</span>
<span class="gp">... </span>   <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;An error in the body&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">An error in the body</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">getSite</span><span class="p">()</span>
<span class="go">None</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The current component registry</a><ul>
<li><a class="reference internal" href="#context-manager">Context manager</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="configure.html"
                        title="previous chapter">Package configuration</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="testlayer.html"
                        title="next chapter">Layers</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/hooks.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <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>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="testlayer.html" title="Layers"
             >next</a> |</li>
        <li class="right" >
          <a href="configure.html" title="Package configuration"
             >previous</a> |</li>
        <li><a href="index.html">zope.configuration 4.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Zope Foundation Contributors.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>

OHA YOOOO