src/build/html/traverser.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>PyAMS namespace traverser &#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="Custom TALES extensions" href="tales.html" />
       
    27     <link rel="prev" title="PyAMS site management" href="site.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="tales.html" title="Custom TALES extensions"
       
    45              accesskey="N">next</a> |</li>
       
    46         <li class="right" >
       
    47           <a href="site.html" title="PyAMS site management"
       
    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="pyams-namespace-traverser">
       
    59 <span id="traverser"></span><h1>PyAMS namespace traverser<a class="headerlink" href="#pyams-namespace-traverser" title="Permalink to this headline">¶</a></h1>
       
    60 <p>PyAMS_utils provide a custom URL traverser, defined in package <code class="xref py py-mod docutils literal"><span class="pre">pyams_utils.traversing</span></code>.</p>
       
    61 <p>The <code class="xref py py-class docutils literal"><span class="pre">NamespaceTraverser</span></code> is a custom traverser based on default
       
    62 Pyramid’s <em>ResourceTreeAdapter</em>, but it adds the ability to use <em>namespaces</em>. Inherited from <em>Zope3</em> concept, a
       
    63 namespace is a resource path element starting with the « <em>++</em> » characters, like this:</p>
       
    64 <div class="highlight-none"><div class="highlight"><pre><span></span>http://localhost:5432/folder/content/++ns++argument/@@view.html
       
    65 </pre></div>
       
    66 </div>
       
    67 <p>In this sample, <em>ns</em> is the namespace name. When the traverser detects a namespace, it looks for several named
       
    68 adapters (or multi-adapters) to the <code class="xref py py-class docutils literal"><span class="pre">ITraversable</span></code> interface
       
    69 defined in <em>zope.traversing</em> package. Adapters lookup with name <em>ns</em> is done for the current <em>context</em> and <em>request</em>,
       
    70 then only for the context and finally for the request, in this order. If a traversing adapter is found, it’s
       
    71 <code class="xref py py-func docutils literal"><span class="pre">traverse()</span></code> method is called, with the <em>attr</em> value as first argument, and the rest of the traversal stack
       
    72 as second one.</p>
       
    73 <p>This is for example how a custom <em>etc</em> namespace traverser is defined:</p>
       
    74 <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">ISiteRoot</span>
       
    75 <span class="kn">from</span> <span class="nn">zope.traversing.interfaces</span> <span class="kn">import</span> <span class="n">ITraversable</span>
       
    76 
       
    77 <span class="kn">from</span> <span class="nn">pyams_utils.adapter</span> <span class="kn">import</span> <span class="n">adapter_config</span><span class="p">,</span> <span class="n">ContextAdapter</span>
       
    78 
       
    79 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;etc&#39;</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">ISiteRoot</span><span class="p">,</span> <span class="n">provides</span><span class="o">=</span><span class="n">ITraversable</span><span class="p">)</span>
       
    80 <span class="k">class</span> <span class="nc">SiteRootEtcTraverser</span><span class="p">(</span><span class="n">ContextAdapter</span><span class="p">):</span>
       
    81     <span class="sd">&quot;&quot;&quot;Site root ++etc++ namespace traverser&quot;&quot;&quot;</span>
       
    82 
       
    83     <span class="k">def</span> <span class="nf">traverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">furtherpath</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
       
    84         <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;site&#39;</span><span class="p">:</span>
       
    85             <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="o">.</span><span class="n">getSiteManager</span><span class="p">()</span>
       
    86         <span class="k">raise</span> <span class="n">NotFound</span>
       
    87 </pre></div>
       
    88 </div>
       
    89 <p>By using an URL like ‘++etc++site’ on your site root, you can then get access to your local site manager.</p>
       
    90 <p><em>argument</em> is not mandatory for the namespace traverser. If it is not provided, the <em>traverse</em> method is called with
       
    91 an empty string (with is a default adapter name) as first argument.</p>
       
    92 <p>Several PyAMS components use custom traversal adapters. For example, getting thumbnails from an image is done
       
    93 through a traversing adapter, which results in nicer URLs than when using classic URLs with arguments…</p>
       
    94 </div>
       
    95 
       
    96 
       
    97           </div>
       
    98         </div>
       
    99       </div>
       
   100       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
       
   101         <div class="sphinxsidebarwrapper">
       
   102   <h4>Previous topic</h4>
       
   103   <p class="topless"><a href="site.html"
       
   104                         title="previous chapter">PyAMS site management</a></p>
       
   105   <h4>Next topic</h4>
       
   106   <p class="topless"><a href="tales.html"
       
   107                         title="next chapter">Custom TALES extensions</a></p>
       
   108 <div id="searchbox" style="display: none" role="search">
       
   109   <h3>Quick search</h3>
       
   110     <form class="search" action="search.html" method="get">
       
   111       <div><input type="text" name="q" /></div>
       
   112       <div><input type="submit" value="Go" /></div>
       
   113       <input type="hidden" name="check_keywords" value="yes" />
       
   114       <input type="hidden" name="area" value="default" />
       
   115     </form>
       
   116 </div>
       
   117 <script type="text/javascript">$('#searchbox').show(0);</script>
       
   118         </div>
       
   119       </div>
       
   120       <div class="clearer"></div>
       
   121     </div>
       
   122     <div class="related" role="navigation" aria-label="related navigation">
       
   123       <h3>Navigation</h3>
       
   124       <ul>
       
   125         <li class="right" style="margin-right: 10px">
       
   126           <a href="genindex.html" title="General Index"
       
   127              >index</a></li>
       
   128         <li class="right" >
       
   129           <a href="tales.html" title="Custom TALES extensions"
       
   130              >next</a> |</li>
       
   131         <li class="right" >
       
   132           <a href="site.html" title="PyAMS site management"
       
   133              >previous</a> |</li>
       
   134         <li class="nav-item nav-item-0"><a href="index.html">PyAMS User Guide 0.1.0 documentation</a> &#187;</li> 
       
   135       </ul>
       
   136     </div>
       
   137     <div class="footer" role="contentinfo">
       
   138         &#169; Copyright 2017, Thierry Florac.
       
   139       Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5.
       
   140     </div>
       
   141   </body>
       
   142 </html>