/usr/share/doc/python-webob-doc/modules/dec.html is in python-webob-doc 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | <!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="Content-Type" content="text/html; charset=utf-8" />
<title>webob.dec – WSGIfy decorator — WebOb 1.2.3 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.2.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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>
<link rel="top" title="WebOb 1.2.3 documentation" href="../index.html" />
<link rel="next" title="webob.static – Serving static files" href="static.html" />
<link rel="prev" title="webob – Request/Response objects" href="webob.html" />
</head>
<body>
<div class="related">
<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="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="static.html" title="webob.static – Serving static files"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="webob.html" title="webob – Request/Response objects"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">WebOb 1.2.3 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-webob.dec">
<span id="webob-dec-wsgify-decorator"></span><h1><a class="reference internal" href="#module-webob.dec" title="webob.dec"><tt class="xref py py-mod docutils literal"><span class="pre">webob.dec</span></tt></a> – WSGIfy decorator</h1>
<p>Decorators to wrap functions to make them WSGI applications.</p>
<p>The main decorator <a class="reference internal" href="#webob.dec.wsgify" title="webob.dec.wsgify"><tt class="xref py py-class docutils literal"><span class="pre">wsgify</span></tt></a> turns a function into a WSGI
application (while also allowing normal calling of the method with an
instantiated request).</p>
<div class="section" id="decorator">
<h2>Decorator</h2>
<dl class="class">
<dt id="webob.dec.wsgify">
<em class="property">class </em><tt class="descclassname">webob.dec.</tt><tt class="descname">wsgify</tt><big>(</big><em>func=None</em>, <em>RequestClass=None</em>, <em>args=()</em>, <em>kwargs=None</em>, <em>middleware_wraps=None</em><big>)</big></dt>
<dd><p>Turns a request-taking, response-returning function into a WSGI
app</p>
<p>You can use this like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@wsgify</span>
<span class="k">def</span> <span class="nf">myfunc</span><span class="p">(</span><span class="n">req</span><span class="p">):</span>
<span class="k">return</span> <span class="n">webob</span><span class="o">.</span><span class="n">Response</span><span class="p">(</span><span class="s">'hey there'</span><span class="p">)</span>
</pre></div>
</div>
<p>With that <tt class="docutils literal"><span class="pre">myfunc</span></tt> will be a WSGI application, callable like
<tt class="docutils literal"><span class="pre">app_iter</span> <span class="pre">=</span> <span class="pre">myfunc(environ,</span> <span class="pre">start_response)</span></tt>. You can also call
it like normal, e.g., <tt class="docutils literal"><span class="pre">resp</span> <span class="pre">=</span> <span class="pre">myfunc(req)</span></tt>. (You can also wrap
methods, like <tt class="docutils literal"><span class="pre">def</span> <span class="pre">myfunc(self,</span> <span class="pre">req)</span></tt>.)</p>
<p>If you raise exceptions from <tt class="xref py py-mod docutils literal"><span class="pre">webob.exc</span></tt> they will be turned
into WSGI responses.</p>
<p>There are also several parameters you can use to customize the
decorator. Most notably, you can use a <tt class="xref py py-class docutils literal"><span class="pre">webob.Request</span></tt>
subclass, like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyRequest</span><span class="p">(</span><span class="n">webob</span><span class="o">.</span><span class="n">Request</span><span class="p">):</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">is_local</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">remote_addr</span> <span class="o">==</span> <span class="s">'127.0.0.1'</span>
<span class="nd">@wsgify</span><span class="p">(</span><span class="n">RequestClass</span><span class="o">=</span><span class="n">MyRequest</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">myfunc</span><span class="p">(</span><span class="n">req</span><span class="p">):</span>
<span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">is_local</span><span class="p">:</span>
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">'hi!'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">webob</span><span class="o">.</span><span class="n">exc</span><span class="o">.</span><span class="n">HTTPForbidden</span>
</pre></div>
</div>
<p>Another customization you can add is to add <cite>args</cite> (positional
arguments) or <cite>kwargs</cite> (of course, keyword arguments). While
generally not that useful, you can use this to create multiple
WSGI apps from one function, like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">simplejson</span>
<span class="k">def</span> <span class="nf">serve_json</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">json_obj</span><span class="p">):</span>
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">json_obj</span><span class="p">),</span>
<span class="n">content_type</span><span class="o">=</span><span class="s">'application/json'</span><span class="p">)</span>
<span class="n">serve_ob1</span> <span class="o">=</span> <span class="n">wsgify</span><span class="p">(</span><span class="n">serve_json</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">ob1</span><span class="p">,))</span>
<span class="n">serve_ob2</span> <span class="o">=</span> <span class="n">wsgify</span><span class="p">(</span><span class="n">serve_json</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">ob2</span><span class="p">,))</span>
</pre></div>
</div>
<p>You can return several things from a function:</p>
<ul class="simple">
<li>A <tt class="xref py py-class docutils literal"><span class="pre">webob.Response</span></tt> object (or subclass)</li>
<li><em>Any</em> WSGI application</li>
<li>None, and then <tt class="docutils literal"><span class="pre">req.response</span></tt> will be used (a pre-instantiated
Response object)</li>
<li>A string, which will be written to <tt class="docutils literal"><span class="pre">req.response</span></tt> and then that
response will be used.</li>
<li>Raise an exception from <tt class="xref py py-mod docutils literal"><span class="pre">webob.exc</span></tt></li>
</ul>
<p>Also see <a class="reference internal" href="#webob.dec.wsgify.middleware" title="webob.dec.wsgify.middleware"><tt class="xref py py-func docutils literal"><span class="pre">wsgify.middleware()</span></tt></a> for a way to make middleware.</p>
<p>You can also subclass this decorator; the most useful things to do
in a subclass would be to change <cite>RequestClass</cite> or override
<cite>call_func</cite> (e.g., to add <tt class="docutils literal"><span class="pre">req.urlvars</span></tt> as keyword arguments to
the function).</p>
<dl class="attribute">
<dt id="webob.dec.wsgify.RequestClass">
<tt class="descname">RequestClass</tt></dt>
<dd><p>alias of <tt class="xref py py-class docutils literal"><span class="pre">Request</span></tt></p>
</dd></dl>
<dl class="method">
<dt id="webob.dec.wsgify.call_func">
<tt class="descname">call_func</tt><big>(</big><em>req</em>, <em>*args</em>, <em>**kwargs</em><big>)</big></dt>
<dd><p>Call the wrapped function; override this in a subclass to
change how the function is called.</p>
</dd></dl>
<dl class="method">
<dt id="webob.dec.wsgify.clone">
<tt class="descname">clone</tt><big>(</big><em>func=None</em>, <em>**kw</em><big>)</big></dt>
<dd><p>Creates a copy/clone of this object, but with some
parameters rebound</p>
</dd></dl>
<dl class="method">
<dt id="webob.dec.wsgify.get">
<tt class="descname">get</tt><big>(</big><em>url</em>, <em>**kw</em><big>)</big></dt>
<dd><p>Run a GET request on this application, returning a Response.</p>
<p>This creates a request object using the given URL, and any
other keyword arguments are set on the request object (e.g.,
<tt class="docutils literal"><span class="pre">last_modified=datetime.now()</span></tt>).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">resp</span> <span class="o">=</span> <span class="n">myapp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/article?id=10'</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="webob.dec.wsgify.middleware">
<em class="property">classmethod </em><tt class="descname">middleware</tt><big>(</big><em>middle_func=None</em>, <em>app=None</em>, <em>**kw</em><big>)</big></dt>
<dd><p>Creates middleware</p>
<p>Use this like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@wsgify.middleware</span>
<span class="k">def</span> <span class="nf">restrict_ip</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">req</span><span class="p">,</span> <span class="n">ips</span><span class="p">):</span>
<span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">remote_addr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">ips</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">webob</span><span class="o">.</span><span class="n">exc</span><span class="o">.</span><span class="n">HTTPForbidden</span><span class="p">(</span><span class="s">'Bad IP: </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">req</span><span class="o">.</span><span class="n">remote_addr</span><span class="p">)</span>
<span class="k">return</span> <span class="n">app</span>
<span class="nd">@wsgify</span>
<span class="k">def</span> <span class="nf">app</span><span class="p">(</span><span class="n">req</span><span class="p">):</span>
<span class="k">return</span> <span class="s">'hi'</span>
<span class="n">wrapped</span> <span class="o">=</span> <span class="n">restrict_ip</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">ips</span><span class="o">=</span><span class="p">[</span><span class="s">'127.0.0.1'</span><span class="p">])</span>
</pre></div>
</div>
<p>Or if you want to write output-rewriting middleware:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@wsgify.middleware</span>
<span class="k">def</span> <span class="nf">all_caps</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">req</span><span class="p">):</span>
<span class="n">resp</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="n">resp</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">body</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="k">return</span> <span class="n">resp</span>
<span class="n">wrapped</span> <span class="o">=</span> <span class="n">all_caps</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that you must call <tt class="docutils literal"><span class="pre">req.get_response(app)</span></tt> to get a WebOb
response object. If you are not modifying the output, you can just
return the app.</p>
<p>As you can see, this method doesn’t actually create an application, but
creates “middleware” that can be bound to an application, along with
“configuration” (that is, any other keyword arguments you pass when
binding the application).</p>
</dd></dl>
<dl class="method">
<dt id="webob.dec.wsgify.post">
<tt class="descname">post</tt><big>(</big><em>url</em>, <em>POST=None</em>, <em>**kw</em><big>)</big></dt>
<dd><p>Run a POST request on this application, returning a Response.</p>
<p>The second argument (<cite>POST</cite>) can be the request body (a
string), or a dictionary or list of two-tuples, that give the
POST body.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">resp</span> <span class="o">=</span> <span class="n">myapp</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">'/article/new'</span><span class="p">,</span>
<span class="nb">dict</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">'My Day'</span><span class="p">,</span>
<span class="n">content</span><span class="o">=</span><span class="s">'I ate a sandwich'</span><span class="p">))</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="webob.dec.wsgify.request">
<tt class="descname">request</tt><big>(</big><em>url</em>, <em>**kw</em><big>)</big></dt>
<dd><p>Run a request on this application, returning a Response.</p>
<p>This can be used for DELETE, PUT, etc requests. E.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">resp</span> <span class="o">=</span> <span class="n">myapp</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">'/article/1'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">'PUT'</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s">'New article'</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">webob.dec</span></tt> – WSGIfy decorator</a><ul>
<li><a class="reference internal" href="#decorator">Decorator</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="webob.html"
title="previous chapter"><tt class="docutils literal"><span class="pre">webob</span></tt> – Request/Response objects</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="static.html"
title="next chapter"><tt class="docutils literal"><span class="pre">webob.static</span></tt> – Serving static files</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/dec.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<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>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<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="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="static.html" title="webob.static – Serving static files"
>next</a> |</li>
<li class="right" >
<a href="webob.html" title="webob – Request/Response objects"
>previous</a> |</li>
<li><a href="../index.html">WebOb 1.2.3 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2011, Ian Bicking and contributors.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
</div>
</body>
</html>
|