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