Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > f57b2cbc6bd4a90f10089095b1f7fb88 > files > 90

python3-routes-2.4.1-1.mga7.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Porting Routes to a WSGI Web Framework &#8212; Routes 2.4.1 documentation</title>
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/language_data.js"></script>
    
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Routes Modules" href="modules/index.html" />
    <link rel="prev" title="Glossary" href="glossary.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="modules/index.html" title="Routes Modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="glossary.html" title="Glossary"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Routes 2.4.1 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="porting-routes-to-a-wsgi-web-framework">
<h1>Porting Routes to a WSGI Web Framework<a class="headerlink" href="#porting-routes-to-a-wsgi-web-framework" title="Permalink to this headline">¶</a></h1>
<div class="section" id="routesmiddleware">
<h2>RoutesMiddleware<a class="headerlink" href="#routesmiddleware" title="Permalink to this headline">¶</a></h2>
<p>An application can create a raw mapper object and call its <code class="docutils literal notranslate"><span class="pre">.match</span></code> and
<code class="docutils literal notranslate"><span class="pre">.generate</span></code> methods.  However, WSGI applications probably want to use
the <code class="docutils literal notranslate"><span class="pre">RoutesMiddleware</span></code> as Pylons does:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># In myapp/config/middleware.py</span>
<span class="kn">from</span> <span class="nn">routes.middleware</span> <span class="k">import</span> <span class="n">RoutesMiddleware</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">RoutesMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="nb">map</span><span class="p">)</span>     <span class="c1"># ``map`` is a routes.Mapper.</span>
</pre></div>
</div>
<p>The middleware matches the requested URL and sets the following WSGI
variables:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;wsgiorg.routing_args&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">((</span><span class="n">url</span><span class="p">,</span> <span class="n">match</span><span class="p">))</span>
<span class="n">environ</span><span class="p">[</span><span class="s1">&#39;routes.route&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">route</span>
<span class="n">environ</span><span class="p">[</span><span class="s1">&#39;routes.url&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">url</span>
</pre></div>
</div>
<p>where <code class="docutils literal notranslate"><span class="pre">match</span></code> is the routing variables dict, <code class="docutils literal notranslate"><span class="pre">route</span></code> is the matched route,
and <code class="docutils literal notranslate"><span class="pre">url</span></code> is a <code class="docutils literal notranslate"><span class="pre">URLGenerator</span></code> object.  In Pylons, <code class="docutils literal notranslate"><span class="pre">match</span></code> is used by the
dispatcher, and <code class="docutils literal notranslate"><span class="pre">url</span></code> is accessible as <code class="docutils literal notranslate"><span class="pre">pylons.url</span></code>.</p>
<p>The middleware handles redirect routes itself, issuing the appropriate
redirect.  The application is not called in this case.</p>
<p>To debug routes, turn on debug logging for the “routes.middleware” logger.</p>
<p>See the Routes source code for other features which may have been added.</p>
</div>
<div class="section" id="url-resolution">
<h2>URL Resolution<a class="headerlink" href="#url-resolution" title="Permalink to this headline">¶</a></h2>
<p>When the URL is looked up, it should be matched against the Mapper. When
matching an incoming URL, it is assumed that the URL path is the only string
being matched. All query args should be stripped before matching:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">m</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;/articles/</span><span class="si">{year}</span><span class="s1">/</span><span class="si">{month}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">controller</span><span class="o">=</span><span class="s1">&#39;blog&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="n">year</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>

<span class="n">m</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;/articles/2003/10&#39;</span><span class="p">)</span>
<span class="c1"># {&#39;controller&#39;:&#39;blog&#39;, &#39;action&#39;:&#39;view&#39;, &#39;year&#39;:&#39;2003&#39;, &#39;month&#39;:&#39;10&#39;}</span>
</pre></div>
</div>
<p>Matching a URL will return a dict of the match results, if you’d like to
differentiate between where the argument came from you can use routematch which
will return the Route object that has all these details:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">m</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;/articles/</span><span class="si">{year}</span><span class="s1">/</span><span class="si">{month}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">controller</span><span class="o">=</span><span class="s1">&#39;blog&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="n">year</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>

<span class="n">result</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">routematch</span><span class="p">(</span><span class="s1">&#39;/articles/2003/10&#39;</span><span class="p">)</span>
<span class="c1"># result is a tuple of the match dict and the Route object</span>

<span class="c1"># result[0] - {&#39;controller&#39;:&#39;blog&#39;, &#39;action&#39;:&#39;view&#39;, &#39;year&#39;:&#39;2003&#39;, &#39;month&#39;:&#39;10&#39;}</span>
<span class="c1"># result[1] - Route object</span>
<span class="c1"># result[1].defaults - {&#39;controller&#39;:&#39;blog&#39;, &#39;action&#39;:&#39;view&#39;, &#39;year&#39;:None}</span>
<span class="c1"># result[1].hardcoded - [&#39;controller&#39;, &#39;action&#39;]</span>
</pre></div>
</div>
<p>Your integration code is then expected to dispatch to a controller and action
in the dict. How it does this is entirely up to the framework integrator. Your
integration should also typically provide the web developer a mechanism to
access the additional dict values.</p>
</div>
<div class="section" id="request-configuration">
<h2>Request Configuration<a class="headerlink" href="#request-configuration" title="Permalink to this headline">¶</a></h2>
<p>If you intend to support <code class="docutils literal notranslate"><span class="pre">url_for()</span></code> and <code class="docutils literal notranslate"><span class="pre">redirect_to()</span></code>, they depend on a
singleton object which requires additional configuration.  You’re better off
not supporting them at all because they will be deprecated soon.
<code class="docutils literal notranslate"><span class="pre">URLGenerator</span></code> is the forward-compatible successor to <code class="docutils literal notranslate"><span class="pre">url_for()</span></code>.
<code class="docutils literal notranslate"><span class="pre">redirect_to()</span></code> is better done in the web framework`as in
<code class="docutils literal notranslate"><span class="pre">pylons.controllers.util.redirect_to()</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">url_for()</span></code> and <code class="docutils literal notranslate"><span class="pre">redirect_to()</span></code> need information on the current request,
and since they can be called from anywhere they don’t have direct access to the
WSGI environment.  To remedy this, Routes provides a thread-safe singleton class
called “request_config”, which holds the request information for the current
thread. You should update this after matching the incoming URL but before
executing any code that might call the two functions.  Here is an example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">routes</span> <span class="k">import</span> <span class="n">request_config</span>

<span class="n">config</span> <span class="o">=</span> <span class="n">request_config</span><span class="p">()</span>

<span class="n">config</span><span class="o">.</span><span class="n">mapper</span> <span class="o">=</span> <span class="n">m</span>                  <span class="c1"># Your mapper object</span>
<span class="n">config</span><span class="o">.</span><span class="n">mapper_dict</span> <span class="o">=</span> <span class="n">result</span>        <span class="c1"># The dict from m.match for this URL request</span>
<span class="n">config</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">hostname</span>             <span class="c1"># The server hostname</span>
<span class="n">config</span><span class="o">.</span><span class="n">protocol</span> <span class="o">=</span> <span class="n">port</span>             <span class="c1"># Protocol used, http, https, etc.</span>
<span class="n">config</span><span class="o">.</span><span class="n">redirect</span> <span class="o">=</span> <span class="n">redir_func</span>       <span class="c1"># A redirect function used by your framework, that is</span>
                                   <span class="c1"># expected to take as the first non-keyword arg a single</span>
                                   <span class="c1"># full or relative URL</span>
</pre></div>
</div>
<p>See the docstring for <code class="docutils literal notranslate"><span class="pre">request_config</span></code> in routes/__init__.py to make sure
you’ve initialized everything necessary.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Porting Routes to a WSGI Web Framework</a><ul>
<li><a class="reference internal" href="#routesmiddleware">RoutesMiddleware</a></li>
<li><a class="reference internal" href="#url-resolution">URL Resolution</a></li>
<li><a class="reference internal" href="#request-configuration">Request Configuration</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="glossary.html"
                        title="previous chapter">Glossary</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="modules/index.html"
                        title="next chapter">Routes Modules</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/porting.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="modules/index.html" title="Routes Modules"
             >next</a> |</li>
        <li class="right" >
          <a href="glossary.html" title="Glossary"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Routes 2.4.1 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2005-2017, Ben Bangert, Mike Orr, and numerous contributers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.3.
    </div>
  </body>
</html>