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