/usr/share/doc/python-werkzeug-doc/html/utils.html is in python-werkzeug-doc 0.10.4+dfsg1-1ubuntu1.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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | <!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>Utilities — Werkzeug 0.10.4 documentation</title>
<link rel="stylesheet" href="_static/werkzeug.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.10.4',
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="Werkzeug 0.10.4 documentation" href="index.html" />
<link rel="next" title="URL Helpers" href="urls.html" />
<link rel="prev" title="Data Structures" href="datastructures.html" />
</head>
<body role="document">
<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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="urls.html" title="URL Helpers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="datastructures.html" title="Data Structures"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Werkzeug 0.10.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="utilities">
<h1>Utilities<a class="headerlink" href="#utilities" title="Permalink to this headline">¶</a></h1>
<p>Various utility functions shipped with Werkzeug.</p>
<div class="section" id="module-werkzeug.utils">
<span id="html-helpers"></span><h2>HTML Helpers<a class="headerlink" href="#module-werkzeug.utils" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.utils.HTMLBuilder">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">HTMLBuilder</code><span class="sig-paren">(</span><em>dialect</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.HTMLBuilder" title="Permalink to this definition">¶</a></dt>
<dd><p>Helper object for HTML generation.</p>
<p>Per default there are two instances of that class. The <cite>html</cite> one, and
the <cite>xhtml</cite> one for those two dialects. The class uses keyword parameters
and positional parameters to generate small snippets of HTML.</p>
<p>Keyword parameters are converted to XML/SGML attributes, positional
arguments are used as children. Because Python accepts positional
arguments before keyword arguments it’s a good idea to use a list with the
star-syntax for some children:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">html</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="n">class_</span><span class="o">=</span><span class="s1">'foo'</span><span class="p">,</span> <span class="o">*</span><span class="p">[</span><span class="n">html</span><span class="o">.</span><span class="n">a</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="n">href</span><span class="o">=</span><span class="s1">'foo.html'</span><span class="p">),</span> <span class="s1">' '</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">html</span><span class="o">.</span><span class="n">a</span><span class="p">(</span><span class="s1">'bar'</span><span class="p">,</span> <span class="n">href</span><span class="o">=</span><span class="s1">'bar.html'</span><span class="p">)])</span>
<span class="go">u'<p class="foo"><a href="foo.html">foo</a> <a href="bar.html">bar</a></p>'</span>
</pre></div>
</div>
<p>This class works around some browser limitations and can not be used for
arbitrary SGML/XML generation. For that purpose lxml and similar
libraries exist.</p>
<p>Calling the builder escapes the string passed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">html</span><span class="o">.</span><span class="n">p</span><span class="p">(</span><span class="n">html</span><span class="p">(</span><span class="s2">"<foo>"</span><span class="p">))</span>
<span class="go">u'<p>&lt;foo&gt;</p>'</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.escape">
<code class="descclassname">werkzeug.utils.</code><code class="descname">escape</code><span class="sig-paren">(</span><em>s</em>, <em>quote=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace special characters “&”, “<”, “>” and (”) to HTML-safe sequences.</p>
<p>There is a special handling for <cite>None</cite> which escapes to an empty string.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.9: </span><cite>quote</cite> is now implicitly on.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>s</strong> – the string to escape.</li>
<li><strong>quote</strong> – ignored.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.unescape">
<code class="descclassname">werkzeug.utils.</code><code class="descname">unescape</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.unescape" title="Permalink to this definition">¶</a></dt>
<dd><p>The reverse function of <cite>escape</cite>. This unescapes all the HTML
entities, not only the XML entities inserted by <cite>escape</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>s</strong> – the string to unescape.</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="general-helpers">
<h2>General Helpers<a class="headerlink" href="#general-helpers" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.utils.cached_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">cached_property</code><span class="sig-paren">(</span><em>func</em>, <em>name=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.cached_property" title="Permalink to this definition">¶</a></dt>
<dd><p>A decorator that converts a function into a lazy property. The
function wrapped is called the first time to retrieve the result
and then that calculated result is used the next time you access
the value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cached_property</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># calculate something important here</span>
<span class="k">return</span> <span class="mi">42</span>
</pre></div>
</div>
<p>The class has to have a <cite>__dict__</cite> in order for this property to
work.</p>
</dd></dl>
<dl class="class">
<dt id="werkzeug.utils.environ_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">environ_property</code><span class="sig-paren">(</span><em>name</em>, <em>default=None</em>, <em>load_func=None</em>, <em>dump_func=None</em>, <em>read_only=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.environ_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Maps request attributes to environment variables. This works not only
for the Werzeug request object, but also any other class with an
environ attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">environ</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'key'</span><span class="p">:</span> <span class="s1">'value'</span><span class="p">}</span>
<span class="gp">... </span> <span class="n">test</span> <span class="o">=</span> <span class="n">environ_property</span><span class="p">(</span><span class="s1">'key'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">var</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">var</span><span class="o">.</span><span class="n">test</span>
<span class="go">'value'</span>
</pre></div>
</div>
<p>If you pass it a second value it’s used as default if the key does not
exist, the third one can be a converter that takes a value and converts
it. If it raises <code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code> or <code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code> the default value
is used. If no default value is provided <cite>None</cite> is used.</p>
<p>Per default the property is read only. You have to explicitly enable it
by passing <code class="docutils literal"><span class="pre">read_only=False</span></code> to the constructor.</p>
</dd></dl>
<dl class="class">
<dt id="werkzeug.utils.header_property">
<em class="property">class </em><code class="descclassname">werkzeug.utils.</code><code class="descname">header_property</code><span class="sig-paren">(</span><em>name</em>, <em>default=None</em>, <em>load_func=None</em>, <em>dump_func=None</em>, <em>read_only=None</em>, <em>doc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.header_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <cite>environ_property</cite> but for headers.</p>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.parse_cookie">
<code class="descclassname">werkzeug.utils.</code><code class="descname">parse_cookie</code><span class="sig-paren">(</span><em>header</em>, <em>charset='utf-8'</em>, <em>errors='replace'</em>, <em>cls=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.parse_cookie" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse a cookie. Either from a string or WSGI environ.</p>
<p>Per default encoding errors are ignored. If you want a different behavior
you can set <cite>errors</cite> to <code class="docutils literal"><span class="pre">'replace'</span></code> or <code class="docutils literal"><span class="pre">'strict'</span></code>. In strict mode a
<code class="xref py py-exc docutils literal"><span class="pre">HTTPUnicodeError</span></code> is raised.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5: </span>This function now returns a <code class="xref py py-class docutils literal"><span class="pre">TypeConversionDict</span></code> instead of a
regular dict. The <cite>cls</cite> parameter was added.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>header</strong> – the header to be used to parse the cookie. Alternatively
this can be a WSGI environment.</li>
<li><strong>charset</strong> – the charset for the cookie values.</li>
<li><strong>errors</strong> – the error behavior for the charset decoding.</li>
<li><strong>cls</strong> – an optional dict class to use. If this is not specified
or <cite>None</cite> the default <code class="xref py py-class docutils literal"><span class="pre">TypeConversionDict</span></code> is
used.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.dump_cookie">
<code class="descclassname">werkzeug.utils.</code><code class="descname">dump_cookie</code><span class="sig-paren">(</span><em>key</em>, <em>value=''</em>, <em>max_age=None</em>, <em>expires=None</em>, <em>path='/'</em>, <em>domain=None</em>, <em>secure=False</em>, <em>httponly=False</em>, <em>charset='utf-8'</em>, <em>sync_expires=True</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.dump_cookie" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new Set-Cookie header without the <code class="docutils literal"><span class="pre">Set-Cookie</span></code> prefix
The parameters are the same as in the cookie Morsel object in the
Python standard library but it accepts unicode data, too.</p>
<p>On Python 3 the return value of this function will be a unicode
string, on Python 2 it will be a native string. In both cases the
return value is usually restricted to ascii as the vast majority of
values are properly escaped, but that is no guarantee. If a unicode
string is returned it’s tunneled through latin1 as required by
PEP 3333.</p>
<p>The return value is not ASCII safe if the key contains unicode
characters. This is technically against the specification but
happens in the wild. It’s strongly recommended to not use
non-ASCII values for the keys.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>max_age</strong> – should be a number of seconds, or <cite>None</cite> (default) if
the cookie should last only as long as the client’s
browser session. Additionally <cite>timedelta</cite> objects
are accepted, too.</li>
<li><strong>expires</strong> – should be a <cite>datetime</cite> object or unix timestamp.</li>
<li><strong>path</strong> – limits the cookie to a given path, per default it will
span the whole domain.</li>
<li><strong>domain</strong> – Use this if you want to set a cross-domain cookie. For
example, <code class="docutils literal"><span class="pre">domain=".example.com"</span></code> will set a cookie
that is readable by the domain <code class="docutils literal"><span class="pre">www.example.com</span></code>,
<code class="docutils literal"><span class="pre">foo.example.com</span></code> etc. Otherwise, a cookie will only
be readable by the domain that set it.</li>
<li><strong>secure</strong> – The cookie will only be available via HTTPS</li>
<li><strong>httponly</strong> – disallow JavaScript to access the cookie. This is an
extension to the cookie standard and probably not
supported by all browsers.</li>
<li><strong>charset</strong> – the encoding for unicode values.</li>
<li><strong>sync_expires</strong> – automatically set expires if max_age is defined
but expires not.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.redirect">
<code class="descclassname">werkzeug.utils.</code><code class="descname">redirect</code><span class="sig-paren">(</span><em>location</em>, <em>code=302</em>, <em>Response=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a response object (a WSGI application) that, if called,
redirects the client to the target location. Supported codes are 301,
302, 303, 305, and 307. 300 is not supported because it’s not a real
redirect and 304 because it’s the answer for a request with a request
with defined If-Modified-Since headers.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>The location can now be a unicode string that is encoded using
the <code class="xref py py-func docutils literal"><span class="pre">iri_to_uri()</span></code> function.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.10: </span>The class used for the Response object can now be passed in.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>location</strong> – the location the response should redirect to.</li>
<li><strong>code</strong> – the redirect status code. defaults to 302.</li>
<li><strong>Response</strong> (<em>class</em>) – a Response class to use when instantiating a
response. The default is <a class="reference internal" href="wrappers.html#werkzeug.wrappers.Response" title="werkzeug.wrappers.Response"><code class="xref py py-class docutils literal"><span class="pre">werkzeug.wrappers.Response</span></code></a> if
unspecified.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.append_slash_redirect">
<code class="descclassname">werkzeug.utils.</code><code class="descname">append_slash_redirect</code><span class="sig-paren">(</span><em>environ</em>, <em>code=301</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.append_slash_redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>Redirects to the same URL but with a slash appended. The behavior
of this function is undefined if the path ends with a slash already.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – the WSGI environment for the request that triggers
the redirect.</li>
<li><strong>code</strong> – the status code for the redirect.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.import_string">
<code class="descclassname">werkzeug.utils.</code><code class="descname">import_string</code><span class="sig-paren">(</span><em>import_name</em>, <em>silent=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.import_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Imports an object based on a string. This is useful if you want to
use import paths as endpoints or something similar. An import path can
be specified either in dotted notation (<code class="docutils literal"><span class="pre">xml.sax.saxutils.escape</span></code>)
or with a colon as object delimiter (<code class="docutils literal"><span class="pre">xml.sax.saxutils:escape</span></code>).</p>
<p>If <cite>silent</cite> is True the return value will be <cite>None</cite> if the import fails.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>import_name</strong> – the dotted name for the object to import.</li>
<li><strong>silent</strong> – if set to <cite>True</cite> import errors are ignored and
<cite>None</cite> is returned instead.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">imported object</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.find_modules">
<code class="descclassname">werkzeug.utils.</code><code class="descname">find_modules</code><span class="sig-paren">(</span><em>import_path</em>, <em>include_packages=False</em>, <em>recursive=False</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.find_modules" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds all the modules below a package. This can be useful to
automatically import all views / controllers so that their metaclasses /
function decorators have a chance to register themselves on the
application.</p>
<p>Packages are not returned unless <cite>include_packages</cite> is <cite>True</cite>. This can
also recursively list modules but in that case it will import all the
packages to get the correct load path of that module.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>import_name</strong> – the dotted name for the package to find child modules.</li>
<li><strong>include_packages</strong> – set to <cite>True</cite> if packages should be returned, too.</li>
<li><strong>recursive</strong> – set to <cite>True</cite> if recursion should happen.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">generator</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.validate_arguments">
<code class="descclassname">werkzeug.utils.</code><code class="descname">validate_arguments</code><span class="sig-paren">(</span><em>func</em>, <em>args</em>, <em>kwargs</em>, <em>drop_extra=True</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.validate_arguments" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks if the function accepts the arguments and keyword arguments.
Returns a new <code class="docutils literal"><span class="pre">(args,</span> <span class="pre">kwargs)</span></code> tuple that can safely be passed to
the function without causing a <cite>TypeError</cite> because the function signature
is incompatible. If <cite>drop_extra</cite> is set to <cite>True</cite> (which is the default)
any extra positional or keyword arguments are dropped automatically.</p>
<p>The exception raised provides three attributes:</p>
<dl class="docutils">
<dt><cite>missing</cite></dt>
<dd>A set of argument names that the function expected but where
missing.</dd>
<dt><cite>extra</cite></dt>
<dd>A dict of keyword arguments that the function can not handle but
where provided.</dd>
<dt><cite>extra_positional</cite></dt>
<dd>A list of values that where given by positional argument but the
function cannot accept.</dd>
</dl>
<p>This can be useful for decorators that forward user submitted data to
a view function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.utils</span> <span class="kn">import</span> <span class="n">ArgumentValidationError</span><span class="p">,</span> <span class="n">validate_arguments</span>
<span class="k">def</span> <span class="nf">sanitize</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">proxy</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">to_dict</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">validate_arguments</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">request</span><span class="p">,),</span> <span class="n">data</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ArgumentValidationError</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">BadRequest</span><span class="p">(</span><span class="s1">'The browser failed to transmit all '</span>
<span class="s1">'the data expected.'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">f</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">kwargs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">proxy</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>func</strong> – the function the validation is performed against.</li>
<li><strong>args</strong> – a tuple of positional arguments.</li>
<li><strong>kwargs</strong> – a dict of keyword arguments.</li>
<li><strong>drop_extra</strong> – set to <cite>False</cite> if you don’t want extra arguments
to be silently dropped.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">tuple in the form <code class="docutils literal"><span class="pre">(args,</span> <span class="pre">kwargs)</span></code>.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.secure_filename">
<code class="descclassname">werkzeug.utils.</code><code class="descname">secure_filename</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.secure_filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Pass it a filename and it will return a secure version of it. This
filename can then safely be stored on a regular file system and passed
to <code class="xref py py-func docutils literal"><span class="pre">os.path.join()</span></code>. The filename returned is an ASCII only string
for maximum portability.</p>
<p>On windows systems the function also makes sure that the file is not
named after one of the special device files.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">secure_filename</span><span class="p">(</span><span class="s2">"My cool movie.mov"</span><span class="p">)</span>
<span class="go">'My_cool_movie.mov'</span>
<span class="gp">>>> </span><span class="n">secure_filename</span><span class="p">(</span><span class="s2">"../../../etc/passwd"</span><span class="p">)</span>
<span class="go">'etc_passwd'</span>
<span class="gp">>>> </span><span class="n">secure_filename</span><span class="p">(</span><span class="s1">u'i contain cool </span><span class="se">\xfc</span><span class="s1">ml</span><span class="se">\xe4</span><span class="s1">uts.txt'</span><span class="p">)</span>
<span class="go">'i_contain_cool_umlauts.txt'</span>
</pre></div>
</div>
<p>The function might return an empty filename. It’s your responsibility
to ensure that the filename is unique and that you generate random
filename if the function returned an empty one.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.</span></p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>filename</strong> – the filename to secure</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.utils.bind_arguments">
<code class="descclassname">werkzeug.utils.</code><code class="descname">bind_arguments</code><span class="sig-paren">(</span><em>func</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.utils.bind_arguments" title="Permalink to this definition">¶</a></dt>
<dd><p>Bind the arguments provided into a dict. When passed a function,
a tuple of arguments and a dict of keyword arguments <cite>bind_arguments</cite>
returns a dict of names as the function would see it. This can be useful
to implement a cache decorator that uses the function arguments to build
the cache key based on the values of the arguments.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>func</strong> – the function the arguments should be bound for.</li>
<li><strong>args</strong> – tuple of positional arguments.</li>
<li><strong>kwargs</strong> – a dict of keyword arguments.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a <code class="xref py py-class docutils literal"><span class="pre">dict</span></code> of bound keyword arguments.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="url-helpers">
<h2>URL Helpers<a class="headerlink" href="#url-helpers" title="Permalink to this headline">¶</a></h2>
<p>Please refer to <a class="reference internal" href="urls.html"><em>URL Helpers</em></a>.</p>
</div>
<div class="section" id="module-werkzeug.useragents">
<span id="useragent-parsing"></span><h2>UserAgent Parsing<a class="headerlink" href="#module-werkzeug.useragents" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.useragents.UserAgent">
<em class="property">class </em><code class="descclassname">werkzeug.useragents.</code><code class="descname">UserAgent</code><span class="sig-paren">(</span><em>environ_or_string</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.useragents.UserAgent" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a user agent. Pass it a WSGI environment or a user agent
string and you can inspect some of the details from the user agent
string via the attributes. The following attributes exist:</p>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.string">
<code class="descname">string</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.string" title="Permalink to this definition">¶</a></dt>
<dd><p>the raw user agent string</p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.platform">
<code class="descname">platform</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.platform" title="Permalink to this definition">¶</a></dt>
<dd><p>the browser platform. The following platforms are currently
recognized:</p>
<ul class="simple">
<li><cite>aix</cite></li>
<li><cite>amiga</cite></li>
<li><cite>android</cite></li>
<li><cite>bsd</cite></li>
<li><cite>chromeos</cite></li>
<li><cite>hpux</cite></li>
<li><cite>iphone</cite></li>
<li><cite>ipad</cite></li>
<li><cite>irix</cite></li>
<li><cite>linux</cite></li>
<li><cite>macos</cite></li>
<li><cite>sco</cite></li>
<li><cite>solaris</cite></li>
<li><cite>wii</cite></li>
<li><cite>windows</cite></li>
</ul>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.browser">
<code class="descname">browser</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.browser" title="Permalink to this definition">¶</a></dt>
<dd><p>the name of the browser. The following browsers are currently
recognized:</p>
<ul class="simple">
<li><cite>aol</cite> *</li>
<li><cite>ask</cite> *</li>
<li><cite>camino</cite></li>
<li><cite>chrome</cite></li>
<li><cite>firefox</cite></li>
<li><cite>galeon</cite></li>
<li><cite>google</cite> *</li>
<li><cite>kmeleon</cite></li>
<li><cite>konqueror</cite></li>
<li><cite>links</cite></li>
<li><cite>lynx</cite></li>
<li><cite>msie</cite></li>
<li><cite>msn</cite></li>
<li><cite>netscape</cite></li>
<li><cite>opera</cite></li>
<li><cite>safari</cite></li>
<li><cite>seamonkey</cite></li>
<li><cite>webkit</cite></li>
<li><cite>yahoo</cite> *</li>
</ul>
<p>(Browsers maked with a star (<code class="docutils literal"><span class="pre">*</span></code>) are crawlers.)</p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.version">
<code class="descname">version</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.version" title="Permalink to this definition">¶</a></dt>
<dd><p>the version of the browser</p>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.useragents.UserAgent.language">
<code class="descname">language</code><a class="headerlink" href="#werkzeug.useragents.UserAgent.language" title="Permalink to this definition">¶</a></dt>
<dd><p>the language of the browser</p>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="module-werkzeug.security">
<span id="security-helpers"></span><h2>Security Helpers<a class="headerlink" href="#module-werkzeug.security" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6.1.</span></p>
</div>
<dl class="function">
<dt id="werkzeug.security.generate_password_hash">
<code class="descclassname">werkzeug.security.</code><code class="descname">generate_password_hash</code><span class="sig-paren">(</span><em>password</em>, <em>method='pbkdf2:sha1'</em>, <em>salt_length=8</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.generate_password_hash" title="Permalink to this definition">¶</a></dt>
<dd><p>Hash a password with the given method and salt with with a string of
the given length. The format of the string returned includes the method
that was used so that <a class="reference internal" href="#werkzeug.security.check_password_hash" title="werkzeug.security.check_password_hash"><code class="xref py py-func docutils literal"><span class="pre">check_password_hash()</span></code></a> can check the hash.</p>
<p>The format for the hashed string looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre>method$salt$hash
</pre></div>
</div>
<p>This method can <strong>not</strong> generate unsalted passwords but it is possible
to set the method to plain to enforce plaintext passwords. If a salt
is used, hmac is used internally to salt the password.</p>
<p>If PBKDF2 is wanted it can be enabled by setting the method to
<code class="docutils literal"><span class="pre">pbkdf2:method:iterations</span></code> where iterations is optional:</p>
<div class="highlight-python"><div class="highlight"><pre>pbkdf2:sha1:2000$salt$hash
pbkdf2:sha1$salt$hash
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>password</strong> – the password to hash.</li>
<li><strong>method</strong> – the hash method to use (one that hashlib supports). Can
optionally be in the format <code class="docutils literal"><span class="pre">pbkdf2:<method>[:iterations]</span></code>
to enable PBKDF2.</li>
<li><strong>salt_length</strong> – the length of the salt in letters.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.security.check_password_hash">
<code class="descclassname">werkzeug.security.</code><code class="descname">check_password_hash</code><span class="sig-paren">(</span><em>pwhash</em>, <em>password</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.check_password_hash" title="Permalink to this definition">¶</a></dt>
<dd><p>check a password against a given salted and hashed password value.
In order to support unsalted legacy passwords this method supports
plain text passwords, md5 and sha1 hashes (both salted and unsalted).</p>
<p>Returns <cite>True</cite> if the password matched, <cite>False</cite> otherwise.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>pwhash</strong> – a hashed string like returned by
<a class="reference internal" href="#werkzeug.security.generate_password_hash" title="werkzeug.security.generate_password_hash"><code class="xref py py-func docutils literal"><span class="pre">generate_password_hash()</span></code></a>.</li>
<li><strong>password</strong> – the plaintext password to compare against the hash.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.security.safe_str_cmp">
<code class="descclassname">werkzeug.security.</code><code class="descname">safe_str_cmp</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.safe_str_cmp" title="Permalink to this definition">¶</a></dt>
<dd><p>This function compares strings in somewhat constant time. This
requires that the length of at least one string is known in advance.</p>
<p>Returns <cite>True</cite> if the two strings are equal, or <cite>False</cite> if they are not.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="werkzeug.security.safe_join">
<code class="descclassname">werkzeug.security.</code><code class="descname">safe_join</code><span class="sig-paren">(</span><em>directory</em>, <em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.safe_join" title="Permalink to this definition">¶</a></dt>
<dd><p>Safely join <cite>directory</cite> and <cite>filename</cite>. If this cannot be done,
this function returns <code class="docutils literal"><span class="pre">None</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>directory</strong> – the base directory.</li>
<li><strong>filename</strong> – the untrusted filename relative to that directory.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.security.pbkdf2_hex">
<code class="descclassname">werkzeug.security.</code><code class="descname">pbkdf2_hex</code><span class="sig-paren">(</span><em>data</em>, <em>salt</em>, <em>iterations=1000</em>, <em>keylen=None</em>, <em>hashfunc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.pbkdf2_hex" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#werkzeug.security.pbkdf2_bin" title="werkzeug.security.pbkdf2_bin"><code class="xref py py-func docutils literal"><span class="pre">pbkdf2_bin()</span></code></a>, but returns a hex-encoded string.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>data</strong> – the data to derive.</li>
<li><strong>salt</strong> – the salt for the derivation.</li>
<li><strong>iterations</strong> – the number of iterations.</li>
<li><strong>keylen</strong> – the length of the resulting key. If not provided,
the digest size will be used.</li>
<li><strong>hashfunc</strong> – the hash function to use. This can either be the
string name of a known hash function, or a function
from the hashlib module. Defaults to sha1.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="werkzeug.security.pbkdf2_bin">
<code class="descclassname">werkzeug.security.</code><code class="descname">pbkdf2_bin</code><span class="sig-paren">(</span><em>data</em>, <em>salt</em>, <em>iterations=1000</em>, <em>keylen=None</em>, <em>hashfunc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.security.pbkdf2_bin" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a binary digest for the PBKDF2 hash algorithm of <cite>data</cite>
with the given <cite>salt</cite>. It iterates <cite>iterations</cite> times and produces a
key of <cite>keylen</cite> bytes. By default, SHA-1 is used as hash function;
a different hashlib <cite>hashfunc</cite> can be provided.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.9.</span></p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>data</strong> – the data to derive.</li>
<li><strong>salt</strong> – the salt for the derivation.</li>
<li><strong>iterations</strong> – the number of iterations.</li>
<li><strong>keylen</strong> – the length of the resulting key. If not provided
the digest size will be used.</li>
<li><strong>hashfunc</strong> – the hash function to use. This can either be the
string name of a known hash function or a function
from the hashlib module. Defaults to sha1.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
<img class="logo" src="_static/werkzeug.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Utilities</a><ul>
<li><a class="reference internal" href="#module-werkzeug.utils">HTML Helpers</a></li>
<li><a class="reference internal" href="#general-helpers">General Helpers</a></li>
<li><a class="reference internal" href="#url-helpers">URL Helpers</a></li>
<li><a class="reference internal" href="#module-werkzeug.useragents">UserAgent Parsing</a></li>
<li><a class="reference internal" href="#module-werkzeug.security">Security Helpers</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="datastructures.html" title="previous chapter">Data Structures</a></li>
<li>Next: <a href="urls.html" title="next chapter">URL Helpers</a></li>
</ul></li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/utils.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<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="footer">
© Copyright 2011, The Werkzeug Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|