docs/build/html/zca.html
changeset 79 e12e18e99a68
parent 78 54aeb42c0d80
child 80 137705383aaf
equal deleted inserted replaced
78:54aeb42c0d80 79:e12e18e99a68
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       
     2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     3 
       
     4 
       
     5 <html xmlns="http://www.w3.org/1999/xhtml">
       
     6   <head>
       
     7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
     8     
       
     9     <title>Managing ZCA with PyAMS &#8212; PyAMS_utils 0.1.4 documentation</title>
       
    10     
       
    11     <link rel="stylesheet" href="_static/pyramid.css" type="text/css" />
       
    12     <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
       
    13     
       
    14     <script type="text/javascript">
       
    15       var DOCUMENTATION_OPTIONS = {
       
    16         URL_ROOT:    './',
       
    17         VERSION:     '0.1.4',
       
    18         COLLAPSE_INDEX: false,
       
    19         FILE_SUFFIX: '.html',
       
    20         HAS_SOURCE:  true
       
    21       };
       
    22     </script>
       
    23     <script type="text/javascript" src="_static/jquery.js"></script>
       
    24     <script type="text/javascript" src="_static/underscore.js"></script>
       
    25     <script type="text/javascript" src="_static/doctools.js"></script>
       
    26     <link rel="index" title="Index" href="genindex.html" />
       
    27     <link rel="search" title="Search" href="search.html" />
       
    28     <link rel="top" title="PyAMS_utils 0.1.4 documentation" href="index.html" />
       
    29     <link rel="next" title="PyAMS site management" href="site.html" />
       
    30     <link rel="prev" title="Installing PyAMS" href="install.html" />
       
    31 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Neuton&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
       
    32 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
       
    33 <!--[if lte IE 6]>
       
    34 <link rel="stylesheet" href="_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
       
    35 <![endif]-->
       
    36 
       
    37   </head>
       
    38   <body role="document">
       
    39 
       
    40     <div class="related" role="navigation" aria-label="related navigation">
       
    41       <h3>Navigation</h3>
       
    42       <ul>
       
    43         <li class="right" style="margin-right: 10px">
       
    44           <a href="genindex.html" title="General Index"
       
    45              accesskey="I">index</a></li>
       
    46         <li class="right" >
       
    47           <a href="py-modindex.html" title="Python Module Index"
       
    48              >modules</a> |</li>
       
    49         <li class="right" >
       
    50           <a href="site.html" title="PyAMS site management"
       
    51              accesskey="N">next</a> |</li>
       
    52         <li class="right" >
       
    53           <a href="install.html" title="Installing PyAMS"
       
    54              accesskey="P">previous</a> |</li>
       
    55         <li class="nav-item nav-item-0"><a href="index.html">PyAMS_utils 0.1.4 documentation</a> &#187;</li> 
       
    56       </ul>
       
    57     </div>  
       
    58 
       
    59     <div class="document">
       
    60       <div class="documentwrapper">
       
    61         <div class="bodywrapper">
       
    62           <div class="body" role="main">
       
    63             
       
    64   <div class="section" id="managing-zca-with-pyams">
       
    65 <span id="zca"></span><h1>Managing ZCA with PyAMS<a class="headerlink" href="#managing-zca-with-pyams" title="Permalink to this headline">¶</a></h1>
       
    66 <p>The <strong>Zope Component Architecture</strong> (aka ZCA) is used by the Pyramid framework &#8220;under the hood&#8221; to handle interfaces,
       
    67 adapters and utilities. You don&#8217;t <strong>have to</strong> use it in your own applications. But you can.</p>
       
    68 <p>The ZCA is mainly adding elements like <strong>interfaces</strong>, <strong>adapters</strong> and <strong>utilities</strong> to the Python language. It
       
    69 allows you to write a framework or an application by using <strong>components</strong> which can be extended easily.</p>
       
    70 <p>You will find several useful resources about ZCA concepts on the internet.</p>
       
    71 <div class="section" id="local-utilities">
       
    72 <h2>Local utilities<a class="headerlink" href="#local-utilities" title="Permalink to this headline">¶</a></h2>
       
    73 <p>In ZCA, a <strong>utility</strong> is a <strong>registered</strong> component which provides an <strong>interface</strong>. This interface is the
       
    74 <strong>contract</strong> which defines features provided by the component which implements it.</p>
       
    75 <p>When a Pyramid application starts, a <strong>global registry</strong> is created to register a whole set of utilities and
       
    76 adapters; this registration can be done via ZCML directives or via native Python code.
       
    77 In addition, PyAMS allows you to define <strong>local utilities</strong>, which are stored and registered in the ZODB via a <strong>site
       
    78 manager</strong>.</p>
       
    79 </div>
       
    80 <div class="section" id="defining-site-root">
       
    81 <h2>Defining site root<a class="headerlink" href="#defining-site-root" title="Permalink to this headline">¶</a></h2>
       
    82 <p>One of PyAMS pre-requisites is to use the ZODB, at least to store the site root application, it&#8217;s configuration and a
       
    83 set of local utilities. <a class="reference internal" href="site.html#site"><span class="std std-ref">PyAMS site management</span></a> describes application startup and <strong>local site manager</strong>
       
    84 initialization process.</p>
       
    85 <p>This site can be used to store <strong>local utilities</strong> whose configuration, which is easily available to site
       
    86 administrators through management interface, is stored in the ZODB.</p>
       
    87 </div>
       
    88 <div class="section" id="registering-global-utilities">
       
    89 <h2>Registering global utilities<a class="headerlink" href="#registering-global-utilities" title="Permalink to this headline">¶</a></h2>
       
    90 <p><strong>Global utilities</strong> are components providing an interface which are registered in the global registry.
       
    91 PyAMS_utils package provides custom annotations to register global utilities without using ZCML. For example, a skin
       
    92 is nothing more than a simple utility providing the <em>ISkin</em> interface:</p>
       
    93 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_default_theme.layer</span> <span class="kn">import</span> <span class="n">IPyAMSDefaultLayer</span>
       
    94 <span class="kn">from</span> <span class="nn">pyams_skin.interfaces</span> <span class="kn">import</span> <span class="n">ISkin</span>
       
    95 <span class="kn">from</span> <span class="nn">pyams_utils.registry</span> <span class="kn">import</span> <span class="n">utility_config</span>
       
    96 
       
    97 <span class="nd">@utility_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;PyAMS default skin&#39;</span><span class="p">,</span> <span class="n">provides</span><span class="o">=</span><span class="n">ISkin</span><span class="p">)</span>
       
    98 <span class="k">class</span> <span class="nc">PyAMSDefaultSkin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
       
    99     <span class="sd">&quot;&quot;&quot;PyAMS default skin&quot;&quot;&quot;</span>
       
   100 
       
   101     <span class="n">label</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;PyAMS default skin&quot;</span><span class="p">)</span>
       
   102     <span class="n">layer</span> <span class="o">=</span> <span class="n">IPyAMSDefaultLayer</span>
       
   103 </pre></div>
       
   104 </div>
       
   105 <p>This annotation registers a utility, named <em>PyAMS default skin</em>, providing the <em>ISkin</em> interface. It&#8217;s the developer
       
   106 responsibility to provide all attributes and methods required by the provided interface.</p>
       
   107 </div>
       
   108 <div class="section" id="registering-local-utilities">
       
   109 <h2>Registering local utilities<a class="headerlink" href="#registering-local-utilities" title="Permalink to this headline">¶</a></h2>
       
   110 <p>A local utility is a persistent object, registered in a <em>local site manager</em>, and providing a specific interface (if
       
   111 a component provides several interfaces, it can be registered several times).</p>
       
   112 <p>Some components can be required by a given package, and created automatically via the <em>pyams_upgrade</em> command line
       
   113 script; this process relies on the <em>ISiteGenerations</em> interface, for example for the timezone utility, a component
       
   114 provided by PyAMS_utils package to handle server timezone and display times correctly:</p>
       
   115 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_utils.interfaces.site</span> <span class="kn">import</span> <span class="n">ISiteGenerations</span>
       
   116 <span class="kn">from</span> <span class="nn">pyams_utils.interfaces.timezone</span> <span class="kn">import</span> <span class="n">IServerTimezone</span>
       
   117 
       
   118 <span class="kn">from</span> <span class="nn">persistent</span> <span class="kn">import</span> <span class="n">Persistent</span>
       
   119 <span class="kn">from</span> <span class="nn">pyams_utils.registry</span> <span class="kn">import</span> <span class="n">utility_config</span>
       
   120 <span class="kn">from</span> <span class="nn">pyams_utils.site</span> <span class="kn">import</span> <span class="n">check_required_utilities</span>
       
   121 <span class="kn">from</span> <span class="nn">pyramid.events</span> <span class="kn">import</span> <span class="n">subscriber</span>
       
   122 <span class="kn">from</span> <span class="nn">zope.container.contained</span> <span class="kn">import</span> <span class="n">Contained</span>
       
   123 <span class="kn">from</span> <span class="nn">zope.interface</span> <span class="kn">import</span> <span class="n">implementer</span>
       
   124 <span class="kn">from</span> <span class="nn">zope.schema.fieldproperty</span> <span class="kn">import</span> <span class="n">FieldProperty</span>
       
   125 
       
   126 <span class="nd">@implementer</span><span class="p">(</span><span class="n">IServerTimezone</span><span class="p">)</span>
       
   127 <span class="k">class</span> <span class="nc">ServerTimezoneUtility</span><span class="p">(</span><span class="n">Persistent</span><span class="p">,</span> <span class="n">Contained</span><span class="p">):</span>
       
   128 
       
   129     <span class="n">timezone</span> <span class="o">=</span> <span class="n">FieldProperty</span><span class="p">(</span><span class="n">IServerTimezone</span><span class="p">[</span><span class="s">&#39;timezone&#39;</span><span class="p">])</span>
       
   130 
       
   131 <span class="n">REQUIRED_UTILITIES</span> <span class="o">=</span> <span class="p">((</span><span class="n">IServerTimezone</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">ServerTimezoneUtility</span><span class="p">,</span> <span class="s">&#39;Server timezone&#39;</span><span class="p">),)</span>
       
   132 
       
   133 <span class="nd">@subscriber</span><span class="p">(</span><span class="n">INewLocalSite</span><span class="p">)</span>
       
   134 <span class="k">def</span> <span class="nf">handle_new_local_site</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
       
   135     <span class="sd">&quot;&quot;&quot;Create a new ServerTimezoneUtility when a site is created&quot;&quot;&quot;</span>
       
   136     <span class="n">site</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">manager</span><span class="o">.</span><span class="n">__parent__</span>
       
   137     <span class="n">check_required_utilities</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">REQUIRED_UTILITIES</span><span class="p">)</span>
       
   138 
       
   139 <span class="nd">@utility_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;PyAMS timezone&#39;</span><span class="p">,</span> <span class="n">provides</span><span class="o">=</span><span class="n">ISiteGenerations</span><span class="p">)</span>
       
   140 <span class="k">class</span> <span class="nc">TimezoneGenerationsChecker</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
       
   141     <span class="sd">&quot;&quot;&quot;Timezone generations checker&quot;&quot;&quot;</span>
       
   142 
       
   143     <span class="n">generation</span> <span class="o">=</span> <span class="mi">1</span>
       
   144 
       
   145     <span class="k">def</span> <span class="nf">evolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">site</span><span class="p">,</span> <span class="n">current</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
       
   146         <span class="sd">&quot;&quot;&quot;Check for required utilities&quot;&quot;&quot;</span>
       
   147         <span class="n">check_required_utilities</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">REQUIRED_UTILITIES</span><span class="p">)</span>
       
   148 </pre></div>
       
   149 </div>
       
   150 <p>Some utilities can also be created manually by an administrator through the management interface, and registered
       
   151 automatically after their creation. For example, this is how a ZEO connection utility (which is managing settings to
       
   152 define a ZEO connection) is registered:</p>
       
   153 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_utils.interfaces.site</span> <span class="kn">import</span> <span class="n">IOptionalUtility</span>
       
   154 <span class="kn">from</span> <span class="nn">pyams_utils.interfaces.zeo</span> <span class="kn">import</span> <span class="n">IZEOConnection</span>
       
   155 <span class="kn">from</span> <span class="nn">zope.annotation.interfaces</span> <span class="kn">import</span> <span class="n">IAttributeAnnotatable</span>
       
   156 <span class="kn">from</span> <span class="nn">zope.lifecycleevent.interfaces</span> <span class="kn">import</span> <span class="n">IObjectAddedEvent</span><span class="p">,</span> <span class="n">IObjectRemovedEvent</span>
       
   157 
       
   158 <span class="kn">from</span> <span class="nn">persistent</span> <span class="kn">import</span> <span class="n">Persistent</span>
       
   159 <span class="kn">from</span> <span class="nn">pyramid.events</span> <span class="kn">import</span> <span class="n">subscriber</span>
       
   160 <span class="kn">from</span> <span class="nn">zope.container.contained</span> <span class="kn">import</span> <span class="n">Contained</span>
       
   161 
       
   162 <span class="nd">@implementer</span><span class="p">(</span><span class="n">IZEOConnection</span><span class="p">)</span>
       
   163 <span class="k">class</span> <span class="nc">ZEOConnection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
       
   164     <span class="sd">&quot;&quot;&quot;ZEO connection object. See source code to get full implementation...&quot;&quot;&quot;</span>
       
   165 
       
   166 <span class="nd">@implementer</span><span class="p">(</span><span class="n">IOptionalUtility</span><span class="p">,</span> <span class="n">IAttributeAnnotatable</span><span class="p">)</span>
       
   167 <span class="k">class</span> <span class="nc">ZEOConnectionUtility</span><span class="p">(</span><span class="n">ZEOConnection</span><span class="p">,</span> <span class="n">Persistent</span><span class="p">,</span> <span class="n">Contained</span><span class="p">):</span>
       
   168     <span class="sd">&quot;&quot;&quot;Persistent ZEO connection utility&quot;&quot;&quot;</span>
       
   169 
       
   170 <span class="nd">@subscriber</span><span class="p">(</span><span class="n">IObjectAddedEvent</span><span class="p">,</span> <span class="n">context_selector</span><span class="o">=</span><span class="n">IZEOConnection</span><span class="p">)</span>
       
   171 <span class="k">def</span> <span class="nf">handle_added_connection</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
       
   172     <span class="sd">&quot;&quot;&quot;Register new ZEO connection when added&quot;&quot;&quot;</span>
       
   173     <span class="n">manager</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">newParent</span>
       
   174     <span class="n">manager</span><span class="o">.</span><span class="n">registerUtility</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">object</span><span class="p">,</span> <span class="n">IZEOConnection</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
       
   175 
       
   176 <span class="nd">@subscriber</span><span class="p">(</span><span class="n">IObjectRemovedEvent</span><span class="p">,</span> <span class="n">context_selector</span><span class="o">=</span><span class="n">IZEOConnection</span><span class="p">)</span>
       
   177 <span class="k">def</span> <span class="nf">handle_removed_connection</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
       
   178     <span class="sd">&quot;&quot;&quot;Un-register ZEO connection when deleted&quot;&quot;&quot;</span>
       
   179     <span class="n">manager</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">oldParent</span>
       
   180     <span class="n">manager</span><span class="o">.</span><span class="n">unregisterUtility</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">object</span><span class="p">,</span> <span class="n">IZEOConnection</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
       
   181 </pre></div>
       
   182 </div>
       
   183 <p><em>context_selector</em> is a custom subscriber predicate, so that subscriber event is activated only if object concerned
       
   184 by an event is providing given interface.</p>
       
   185 </div>
       
   186 <div class="section" id="looking-for-utilities">
       
   187 <h2>Looking for utilities<a class="headerlink" href="#looking-for-utilities" title="Permalink to this headline">¶</a></h2>
       
   188 <p>ZCA provides the <em>getUtility</em> and <em>queryUtility</em> functions to look for a utility. But these methods only applies to
       
   189 global registry.</p>
       
   190 <p>PyAMS package provides equivalent functions, which are looking for components into local registry before looking into
       
   191 the global one. For example:</p>
       
   192 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_security.interfaces</span> <span class="kn">import</span> <span class="n">ISecurityManager</span>
       
   193 <span class="kn">from</span> <span class="nn">pyams_utils.registry</span> <span class="kn">import</span> <span class="n">query_utility</span>
       
   194 
       
   195 <span class="n">manager</span> <span class="o">=</span> <span class="n">query_utility</span><span class="p">(</span><span class="n">ISecurityManager</span><span class="p">)</span>
       
   196 <span class="k">if</span> <span class="n">manager</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
       
   197     <span class="k">print</span><span class="p">(</span><span class="s">&quot;Manager is there!&quot;</span><span class="p">)</span>
       
   198 </pre></div>
       
   199 </div>
       
   200 <p>All ZCA utility functions have been ported to use local registry: <em>registered_utilities</em>, <em>query_utility</em>,
       
   201 <em>get_utility</em>, <em>get_utilities_for</em>, <em>get_all_utilities_registered_for</em> functions all follow the equivalent ZCA
       
   202 functions API, but are looking for utilities in the local registry before looking in the global registry.</p>
       
   203 </div>
       
   204 <div class="section" id="registering-adapters">
       
   205 <h2>Registering adapters<a class="headerlink" href="#registering-adapters" title="Permalink to this headline">¶</a></h2>
       
   206 <p>An adapter is also a kind of utility. But instead of <em>just</em> providing an interface, it adapts an input object,
       
   207 providing a given interface, to provide another interface. An adapter can also be named, so that you can choose which
       
   208 adapter to use at a given time.</p>
       
   209 <p>PyAMS_utils provide another annotation, to help registering adapters without using ZCML files. An adapter can be a
       
   210 function which directly returns an object providing the requested interface, or an object which provides the interface.</p>
       
   211 <p>The first example is an adapter which adapts any persistent object to get it&#8217;s associated transaction manager:</p>
       
   212 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">persistent.interfaces</span> <span class="kn">import</span> <span class="n">IPersistent</span>
       
   213 <span class="kn">from</span> <span class="nn">transaction.interfaces</span> <span class="kn">import</span> <span class="n">ITransactionManager</span>
       
   214 <span class="kn">from</span> <span class="nn">ZODB.interfaces</span> <span class="kn">import</span> <span class="n">IConnection</span>
       
   215 
       
   216 <span class="kn">from</span> <span class="nn">pyams_utils.adapter</span> <span class="kn">import</span> <span class="n">adapter_config</span>
       
   217 
       
   218 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">context</span><span class="o">=</span><span class="n">IPersistent</span><span class="p">,</span> <span class="n">provides</span><span class="o">=</span><span class="n">ITransactionManager</span><span class="p">)</span>
       
   219 <span class="k">def</span> <span class="nf">get_transaction_manager</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
       
   220     <span class="n">conn</span> <span class="o">=</span> <span class="n">IConnection</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
       
   221     <span class="k">try</span><span class="p">:</span>
       
   222         <span class="k">return</span> <span class="n">conn</span><span class="o">.</span><span class="n">transaction_manager</span>
       
   223     <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
       
   224         <span class="k">return</span> <span class="n">conn</span><span class="o">.</span><span class="n">_txn_mgr</span>
       
   225 </pre></div>
       
   226 </div>
       
   227 <p>This is another adapter which adapts any contained object to the <em>IPathElements</em> interface; this interface can be
       
   228 used to build index that you can use to find objects based on a parent object:</p>
       
   229 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_utils.interfaces.traversing</span> <span class="kn">import</span> <span class="n">IPathElements</span>
       
   230 <span class="kn">from</span> <span class="nn">zope.intid.interfaces</span> <span class="kn">import</span> <span class="n">IIntIds</span>
       
   231 <span class="kn">from</span> <span class="nn">zope.location.interfaces</span> <span class="kn">import</span> <span class="n">IContained</span>
       
   232 
       
   233 <span class="kn">from</span> <span class="nn">pyams_utils.adapter</span> <span class="kn">import</span> <span class="n">ContextAdapter</span>
       
   234 <span class="kn">from</span> <span class="nn">pyams_utils.registry</span> <span class="kn">import</span> <span class="n">query_utility</span>
       
   235 <span class="kn">from</span> <span class="nn">pyramid.location</span> <span class="kn">import</span> <span class="n">lineage</span>
       
   236 
       
   237 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">context</span><span class="o">=</span><span class="n">IContained</span><span class="p">,</span> <span class="n">provides</span><span class="o">=</span><span class="n">IPathElements</span><span class="p">)</span>
       
   238 <span class="k">class</span> <span class="nc">PathElementsAdapter</span><span class="p">(</span><span class="n">ContextAdapter</span><span class="p">):</span>
       
   239     <span class="sd">&quot;&quot;&quot;Contained object path elements adapter&quot;&quot;&quot;</span>
       
   240 
       
   241     <span class="nd">@property</span>
       
   242     <span class="k">def</span> <span class="nf">parents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
       
   243         <span class="n">intids</span> <span class="o">=</span> <span class="n">query_utility</span><span class="p">(</span><span class="n">IIntIds</span><span class="p">)</span>
       
   244         <span class="k">if</span> <span class="n">intids</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
       
   245             <span class="k">return</span> <span class="p">[]</span>
       
   246         <span class="k">return</span> <span class="p">[</span><span class="n">intids</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span> <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="n">lineage</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)]</span>
       
   247 </pre></div>
       
   248 </div>
       
   249 <p>An adapter can also be a multi-adapter, when several input objects are requested to provide a given interface. For
       
   250 example, many adapters require a context and a request, eventually a view, to provide another feature. This is how,
       
   251 for example, we define a custom <em>name</em> column in a security manager table displaying a list of plug-ins:</p>
       
   252 <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyams_zmi.layer</span> <span class="kn">import</span> <span class="n">IAdminLayer</span>
       
   253 <span class="kn">from</span> <span class="nn">z3c.table.interfaces</span> <span class="kn">import</span> <span class="n">IColumn</span>
       
   254 
       
   255 <span class="kn">from</span> <span class="nn">pyams_skin.table</span> <span class="kn">import</span> <span class="n">I18nColumn</span>
       
   256 <span class="kn">from</span> <span class="nn">z3c.table.column</span> <span class="kn">import</span> <span class="n">GetAttrColumn</span>
       
   257 
       
   258 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="n">Interface</span><span class="p">,</span> <span class="n">IAdminLayer</span><span class="p">,</span> <span class="n">SecurityManagerPluginsTable</span><span class="p">),</span> <span class="n">provides</span><span class="o">=</span><span class="n">IColumn</span><span class="p">)</span>
       
   259 <span class="k">class</span> <span class="nc">SecurityManagerPluginsNameColumn</span><span class="p">(</span><span class="n">I18nColumn</span><span class="p">,</span> <span class="n">GetAttrColumn</span><span class="p">):</span>
       
   260     <span class="sd">&quot;&quot;&quot;Security manager plugins name column&quot;&quot;&quot;</span>
       
   261 
       
   262     <span class="n">_header</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;Name&quot;</span><span class="p">)</span>
       
   263     <span class="n">attrName</span> <span class="o">=</span> <span class="s">&#39;title&#39;</span>
       
   264     <span class="n">weight</span> <span class="o">=</span> <span class="mi">10</span>
       
   265 </pre></div>
       
   266 </div>
       
   267 </div>
       
   268 <div class="section" id="registering-vocabularies">
       
   269 <h2>Registering vocabularies<a class="headerlink" href="#registering-vocabularies" title="Permalink to this headline">¶</a></h2>
       
   270 <p>A <strong>vocabulary</strong> is a custom factory which can be used as source for several field types, like <em>choices</em> or <em>lists</em>.
       
   271 Vocabularies have to be registered in a custom registry, so PyAMS_utils provide another annotation to register them.
       
   272 This example is based on the <em>Timezone</em> component which allows you to select a timezone between a list of references:</p>
       
   273 <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pytz</span>
       
   274 <span class="kn">from</span> <span class="nn">pyams_utils.vocabulary</span> <span class="kn">import</span> <span class="n">vocabulary_config</span>
       
   275 <span class="kn">from</span> <span class="nn">zope.schema.vocabulary</span> <span class="kn">import</span> <span class="n">SimpleTerm</span><span class="p">,</span> <span class="n">SimpleVocabulary</span>
       
   276 
       
   277 <span class="nd">@vocabulary_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;PyAMS timezones&#39;</span><span class="p">)</span>
       
   278 <span class="k">class</span> <span class="nc">TimezonesVocabulary</span><span class="p">(</span><span class="n">SimpleVocabulary</span><span class="p">):</span>
       
   279     <span class="sd">&quot;&quot;&quot;Timezones vocabulary&quot;&quot;&quot;</span>
       
   280 
       
   281     <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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
       
   282         <span class="n">terms</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleTerm</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">pytz</span><span class="o">.</span><span class="n">all_timezones</span><span class="p">]</span>
       
   283         <span class="nb">super</span><span class="p">(</span><span class="n">TimezonesVocabulary</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">terms</span><span class="p">)</span>
       
   284 </pre></div>
       
   285 </div>
       
   286 </div>
       
   287 </div>
       
   288 
       
   289 
       
   290           </div>
       
   291         </div>
       
   292       </div>
       
   293       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
       
   294         <div class="sphinxsidebarwrapper">
       
   295   <h3><a href="index.html">Table Of Contents</a></h3>
       
   296   <ul>
       
   297 <li><a class="reference internal" href="#">Managing ZCA with PyAMS</a><ul>
       
   298 <li><a class="reference internal" href="#local-utilities">Local utilities</a></li>
       
   299 <li><a class="reference internal" href="#defining-site-root">Defining site root</a></li>
       
   300 <li><a class="reference internal" href="#registering-global-utilities">Registering global utilities</a></li>
       
   301 <li><a class="reference internal" href="#registering-local-utilities">Registering local utilities</a></li>
       
   302 <li><a class="reference internal" href="#looking-for-utilities">Looking for utilities</a></li>
       
   303 <li><a class="reference internal" href="#registering-adapters">Registering adapters</a></li>
       
   304 <li><a class="reference internal" href="#registering-vocabularies">Registering vocabularies</a></li>
       
   305 </ul>
       
   306 </li>
       
   307 </ul>
       
   308 
       
   309   <h4>Previous topic</h4>
       
   310   <p class="topless"><a href="install.html"
       
   311                         title="previous chapter">Installing PyAMS</a></p>
       
   312   <h4>Next topic</h4>
       
   313   <p class="topless"><a href="site.html"
       
   314                         title="next chapter">PyAMS site management</a></p>
       
   315 <div id="searchbox" style="display: none" role="search">
       
   316   <h3>Quick search</h3>
       
   317     <form class="search" action="search.html" method="get">
       
   318       <div><input type="text" name="q" /></div>
       
   319       <div><input type="submit" value="Go" /></div>
       
   320       <input type="hidden" name="check_keywords" value="yes" />
       
   321       <input type="hidden" name="area" value="default" />
       
   322     </form>
       
   323 </div>
       
   324 <script type="text/javascript">$('#searchbox').show(0);</script>
       
   325         </div>
       
   326       </div>
       
   327       <div class="clearer"></div>
       
   328     </div>
       
   329     <div class="related" role="navigation" aria-label="related navigation">
       
   330       <h3>Navigation</h3>
       
   331       <ul>
       
   332         <li class="right" style="margin-right: 10px">
       
   333           <a href="genindex.html" title="General Index"
       
   334              >index</a></li>
       
   335         <li class="right" >
       
   336           <a href="py-modindex.html" title="Python Module Index"
       
   337              >modules</a> |</li>
       
   338         <li class="right" >
       
   339           <a href="site.html" title="PyAMS site management"
       
   340              >next</a> |</li>
       
   341         <li class="right" >
       
   342           <a href="install.html" title="Installing PyAMS"
       
   343              >previous</a> |</li>
       
   344         <li class="nav-item nav-item-0"><a href="index.html">PyAMS_utils 0.1.4 documentation</a> &#187;</li> 
       
   345       </ul>
       
   346     </div>
       
   347     <div class="footer" role="contentinfo">
       
   348         &#169; Copyright 2016, Thierry Florac &lt;tflorac@ulthar.net&gt;.
       
   349       Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.8.
       
   350     </div>
       
   351   </body>
       
   352 </html>