OpenQuizz
Une application de gestion des contenus pédagogiques
werkzeug.utils Namespace Reference

Data Structures

class  ArgumentValidationError
 
class  cached_property
 
class  environ_property
 
class  header_property
 
class  HTMLBuilder
 
class  ImportStringError
 

Functions

def invalidate_cached_property (obj, name)
 
def get_content_type (mimetype, charset)
 
def detect_utf_encoding (data)
 
def format_string (string, context)
 
def secure_filename (filename)
 
def escape (s)
 
def unescape (s)
 
def redirect (location, code=302, Response=None)
 
def append_slash_redirect (environ, code=301)
 
def import_string (import_name, silent=False)
 
def find_modules (import_path, include_packages=False, recursive=False)
 
def validate_arguments (func, args, kwargs, drop_extra=True)
 
def bind_arguments (func, args, kwargs)
 

Variables

 html
 
 xhtml
 

Function Documentation

◆ append_slash_redirect()

def werkzeug.utils.append_slash_redirect (   environ,
  code = 301 
)
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.

:param environ: the WSGI environment for the request that triggers
                the redirect.
:param code: the status code for the redirect.

◆ bind_arguments()

def werkzeug.utils.bind_arguments (   func,
  args,
  kwargs 
)
Bind the arguments provided into a dict.  When passed a function,
a tuple of arguments and a dict of keyword arguments `bind_arguments`
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.

:param func: the function the arguments should be bound for.
:param args: tuple of positional arguments.
:param kwargs: a dict of keyword arguments.
:return: a :class:`dict` of bound keyword arguments.

◆ detect_utf_encoding()

def werkzeug.utils.detect_utf_encoding (   data)
Detect which UTF encoding was used to encode the given bytes.

The latest JSON standard (:rfc:`8259`) suggests that only UTF-8 is
accepted. Older documents allowed 8, 16, or 32. 16 and 32 can be big
or little endian. Some editors or libraries may prepend a BOM.

:internal:

:param data: Bytes in unknown UTF encoding.
:return: UTF encoding name

.. versionadded:: 0.15

◆ escape()

def werkzeug.utils.escape (   s)
Replace special characters "&", "<", ">" and (") to HTML-safe sequences.

There is a special handling for `None` which escapes to an empty string.

.. versionchanged:: 0.9
   `quote` is now implicitly on.

:param s: the string to escape.
:param quote: ignored.

◆ find_modules()

def werkzeug.utils.find_modules (   import_path,
  include_packages = False,
  recursive = False 
)
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.

Packages are not returned unless `include_packages` is `True`.  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.

:param import_path: the dotted name for the package to find child modules.
:param include_packages: set to `True` if packages should be returned, too.
:param recursive: set to `True` if recursion should happen.
:return: generator

◆ format_string()

def werkzeug.utils.format_string (   string,
  context 
)
String-template format a string:

>>> format_string('$foo and ${foo}s', dict(foo=42))
'42 and 42s'

This does not do any attribute lookup etc.  For more advanced string
formattings have a look at the `werkzeug.template` module.

:param string: the format string.
:param context: a dict with the variables to insert.

◆ get_content_type()

def werkzeug.utils.get_content_type (   mimetype,
  charset 
)
Returns the full content type string with charset for a mimetype.

If the mimetype represents text, the charset parameter will be
appended, otherwise the mimetype is returned unchanged.

:param mimetype: The mimetype to be used as content type.
:param charset: The charset to be appended for text mimetypes.
:return: The content type.

.. versionchanged:: 0.15
    Any type that ends with ``+xml`` gets a charset, not just those
    that start with ``application/``. Known text types such as
    ``application/javascript`` are also given charsets.

◆ import_string()

def werkzeug.utils.import_string (   import_name,
  silent = False 
)
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 (``xml.sax.saxutils.escape``)
or with a colon as object delimiter (``xml.sax.saxutils:escape``).

If `silent` is True the return value will be `None` if the import fails.

:param import_name: the dotted name for the object to import.
:param silent: if set to `True` import errors are ignored and
               `None` is returned instead.
:return: imported object

◆ invalidate_cached_property()

def werkzeug.utils.invalidate_cached_property (   obj,
  name 
)
Invalidates the cache for a :class:`cached_property`:

>>> class Test(object):
...     @cached_property
...     def magic_number(self):
...         print("recalculating...")
...         return 42
...
>>> var = Test()
>>> var.magic_number
recalculating...
42
>>> var.magic_number
42
>>> invalidate_cached_property(var, "magic_number")
>>> var.magic_number
recalculating...
42

You must pass the name of the cached property as the second argument.

◆ redirect()

def werkzeug.utils.redirect (   location,
  code = 302,
  Response = None 
)
Returns a response object (a WSGI application) that, if called,
redirects the client to the target location. Supported codes are
301, 302, 303, 305, 307, and 308. 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.

.. versionadded:: 0.6
   The location can now be a unicode string that is encoded using
   the :func:`iri_to_uri` function.

.. versionadded:: 0.10
    The class used for the Response object can now be passed in.

:param location: the location the response should redirect to.
:param code: the redirect status code. defaults to 302.
:param class Response: a Response class to use when instantiating a
    response. The default is :class:`werkzeug.wrappers.Response` if
    unspecified.

◆ secure_filename()

def werkzeug.utils.secure_filename (   filename)
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 :func:`os.path.join`.  The filename returned is an ASCII only string
for maximum portability.

On windows systems the function also makes sure that the file is not
named after one of the special device files.

>>> secure_filename("My cool movie.mov")
'My_cool_movie.mov'
>>> secure_filename("../../../etc/passwd")
'etc_passwd'
>>> secure_filename(u'i contain cool \xfcml\xe4uts.txt')
'i_contain_cool_umlauts.txt'

The function might return an empty filename.  It's your responsibility
to ensure that the filename is unique and that you abort or
generate a random filename if the function returned an empty one.

.. versionadded:: 0.5

:param filename: the filename to secure

◆ unescape()

def werkzeug.utils.unescape (   s)
The reverse function of `escape`.  This unescapes all the HTML
entities, not only the XML entities inserted by `escape`.

:param s: the string to unescape.

◆ validate_arguments()

def werkzeug.utils.validate_arguments (   func,
  args,
  kwargs,
  drop_extra = True 
)
Checks if the function accepts the arguments and keyword arguments.
Returns a new ``(args, kwargs)`` tuple that can safely be passed to
the function without causing a `TypeError` because the function signature
is incompatible.  If `drop_extra` is set to `True` (which is the default)
any extra positional or keyword arguments are dropped automatically.

The exception raised provides three attributes:

`missing`
    A set of argument names that the function expected but where
    missing.

`extra`
    A dict of keyword arguments that the function can not handle but
    where provided.

`extra_positional`
    A list of values that where given by positional argument but the
    function cannot accept.

This can be useful for decorators that forward user submitted data to
a view function::

    from werkzeug.utils import ArgumentValidationError, validate_arguments

    def sanitize(f):
        def proxy(request):
            data = request.values.to_dict()
            try:
                args, kwargs = validate_arguments(f, (request,), data)
            except ArgumentValidationError:
                raise BadRequest('The browser failed to transmit all '
                                 'the data expected.')
            return f(*args, **kwargs)
        return proxy

:param func: the function the validation is performed against.
:param args: a tuple of positional arguments.
:param kwargs: a dict of keyword arguments.
:param drop_extra: set to `False` if you don't want extra arguments
                   to be silently dropped.
:return: tuple in the form ``(args, kwargs)``.

Variable Documentation

◆ html

html

◆ xhtml

xhtml