OpenQuizz
Une application de gestion des contenus pédagogiques
Parser Class Reference
Inheritance diagram for Parser:

Public Member Functions

def __init__ (self, typing.Optional[str] location=None, *typing.Optional[str] unknown=_UNKNOWN_DEFAULT_PARAM, typing.Optional[ErrorHandler] error_handler=None, typing.Optional[typing.Type] schema_class=None)
 
def parse (self, ArgMap argmap, typing.Optional[Request] req=None, *typing.Optional[str] location=None, typing.Optional[str] unknown=_UNKNOWN_DEFAULT_PARAM, ValidateArg validate=None, typing.Optional[int] error_status_code=None, typing.Optional[typing.Mapping[str, str]] error_headers=None)
 
typing.Optional[Requestget_default_request (self)
 
typing.Optional[Requestget_request_from_view_args (self, typing.Callable view, typing.Tuple args, typing.Mapping[str, typing.Any] kwargs)
 
typing.Callable[..., typing.Callable] use_args (self, ArgMap argmap, typing.Optional[Request] req=None, *typing.Optional[str] location=None, typing.Optional[str] unknown=_UNKNOWN_DEFAULT_PARAM, bool as_kwargs=False, ValidateArg validate=None, typing.Optional[int] error_status_code=None, typing.Optional[typing.Mapping[str, str]] error_headers=None)
 
typing.Callable use_kwargs (self, *args, **kwargs)
 
def location_loader (self, str name)
 
ErrorHandler error_handler (self, ErrorHandler func)
 
Mapping pre_load (self, Mapping location_data, *ma.Schema schema, Request req, str location)
 
typing.Any load_json (self, Request req, ma.Schema schema)
 
def load_json_or_form (self, Request req, ma.Schema schema)
 
def load_querystring (self, Request req, ma.Schema schema)
 
def load_form (self, Request req, ma.Schema schema)
 
def load_headers (self, Request req, ma.Schema schema)
 
def load_cookies (self, Request req, ma.Schema schema)
 
def load_files (self, Request req, ma.Schema schema)
 
typing.NoReturn handle_error (self, ValidationError error, Request req, ma.Schema schema, *int error_status_code, typing.Mapping[str, str] error_headers)
 

Data Fields

 location
 
 schema_class
 
 unknown
 
 error_callback
 

Static Public Attributes

 str
 
 Type
 
 int
 

Detailed Description

Base parser class that provides high-level implementation for parsing
a request.

Descendant classes must provide lower-level implementations for reading
data from  different locations, e.g. ``load_json``, ``load_querystring``,
etc.

:param str location: Default location to use for data
:param str unknown: A default value to pass for ``unknown`` when calling the
    schema's ``load`` method. Defaults to EXCLUDE for non-body
    locations and RAISE for request bodies. Pass ``None`` to use the
    schema's setting instead.
:param callable error_handler: Custom error handler function.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
typing.Optional[str]   location = None,
*typing.Optional[str]   unknown = _UNKNOWN_DEFAULT_PARAM,
typing.Optional[ErrorHandler]   error_handler = None,
typing.Optional[typing.Type]   schema_class = None 
)

Member Function Documentation

◆ error_handler()

ErrorHandler error_handler (   self,
ErrorHandler  func 
)
Decorator that registers a custom error handling function. The
function should receive the raised error, request object,
`marshmallow.Schema` instance used to parse the request, error status code,
and headers to use for the error response. Overrides
the parser's ``handle_error`` method.

Example: ::

    from webargs import flaskparser

    parser = flaskparser.FlaskParser()


    class CustomError(Exception):
pass


    @parser.error_handler
    def handle_error(error, req, schema, *, error_status_code, error_headers):
raise CustomError(error.messages)

:param callable func: The error callback to register.

◆ get_default_request()

typing.Optional[Request] get_default_request (   self)
Optional override. Provides a hook for frameworks that use thread-local
request objects.

Reimplemented in FlaskParser, and BottleParser.

◆ get_request_from_view_args()

typing.Optional[Request] get_request_from_view_args (   self,
typing.Callable  view,
typing.Tuple  args,
typing.Mapping[str, typing.Any]  kwargs 
)
Optional override. Returns the request object to be parsed, given a view
function's args and kwargs.

Used by the `use_args` and `use_kwargs` to get a request object from a
view's arguments.

:param callable view: The view function or method being decorated by
    `use_args` or `use_kwargs`
:param tuple args: Positional arguments passed to ``view``.
:param dict kwargs: Keyword arguments passed to ``view``.

◆ handle_error()

typing.NoReturn handle_error (   self,
ValidationError  error,
Request  req,
ma.Schema  schema,
*int  error_status_code,
typing.Mapping[str, str]   error_headers 
)
Called if an error occurs while parsing args. By default, just logs and
raises ``error``.

◆ load_cookies()

def load_cookies (   self,
Request  req,
ma.Schema  schema 
)
Load the cookies from the request or return `missing` if no value
can be found.

◆ load_files()

def load_files (   self,
Request  req,
ma.Schema  schema 
)
Load files from the request or return `missing` if no values can be
found.

◆ load_form()

def load_form (   self,
Request  req,
ma.Schema  schema 
)
Load the form data of a request object or return `missing` if no
value can be found.

◆ load_headers()

def load_headers (   self,
Request  req,
ma.Schema  schema 
)
Load the headers or return `missing` if no value can be found.

◆ load_json()

typing.Any load_json (   self,
Request  req,
ma.Schema  schema 
)
Load JSON from a request object or return `missing` if no value can
be found.

◆ load_json_or_form()

def load_json_or_form (   self,
Request  req,
ma.Schema  schema 
)
Load data from a request, accepting either JSON or form-encoded
data.

The data will first be loaded as JSON, and, if that fails, it will be
loaded as a form post.

◆ load_querystring()

def load_querystring (   self,
Request  req,
ma.Schema  schema 
)
Load the query string of a request object or return `missing` if no
value can be found.

◆ location_loader()

def location_loader (   self,
str  name 
)
Decorator that registers a function for loading a request location.
The wrapped function receives a schema and a request.

The schema will usually not be relevant, but it's important in some
cases -- most notably in order to correctly load multidict values into
list fields. Without the schema, there would be no way to know whether
to simply `.get()` or `.getall()` from a multidict for a given value.

Example: ::

    from webargs import core
    parser = core.Parser()

    @parser.location_loader("name")
    def load_data(request, schema):
return request.data

:param str name: The name of the location to register.

◆ parse()

def parse (   self,
ArgMap  argmap,
typing.Optional[Request]   req = None,
*typing.Optional[str]   location = None,
typing.Optional[str]   unknown = _UNKNOWN_DEFAULT_PARAM,
ValidateArg   validate = None,
typing.Optional[int]   error_status_code = None,
typing.Optional[typing.Mapping[str, str]]   error_headers = None 
)
Main request parsing method.

:param argmap: Either a `marshmallow.Schema`, a `dict`
    of argname -> `marshmallow.fields.Field` pairs, or a callable
    which accepts a request and returns a `marshmallow.Schema`.
:param req: The request object to parse.
:param str location: Where on the request to load values.
    Can be any of the values in :py:attr:`~__location_map__`. By
    default, that means one of ``('json', 'query', 'querystring',
    'form', 'headers', 'cookies', 'files', 'json_or_form')``.
:param str unknown: A value to pass for ``unknown`` when calling the
    schema's ``load`` method. Defaults to EXCLUDE for non-body
    locations and RAISE for request bodies. Pass ``None`` to use the
    schema's setting instead.
:param callable validate: Validation function or list of validation functions
    that receives the dictionary of parsed arguments. Validator either returns a
    boolean or raises a :exc:`ValidationError`.
:param int error_status_code: Status code passed to error handler functions when
    a `ValidationError` is raised.
:param dict error_headers: Headers passed to error handler functions when a
    a `ValidationError` is raised.

 :return: A dictionary of parsed arguments

◆ pre_load()

Mapping pre_load (   self,
Mapping  location_data,
*ma.Schema  schema,
Request  req,
str   location 
)
A method of the parser which can transform data after location
loading is done. By default it does nothing, but users can subclass
parsers and override this method.

◆ use_args()

typing.Callable[..., typing.Callable] use_args (   self,
ArgMap  argmap,
typing.Optional[Request]   req = None,
*typing.Optional[str]   location = None,
typing.Optional[str]   unknown = _UNKNOWN_DEFAULT_PARAM,
bool   as_kwargs = False,
ValidateArg   validate = None,
typing.Optional[int]   error_status_code = None,
typing.Optional[typing.Mapping[str, str]]   error_headers = None 
)
Decorator that injects parsed arguments into a view function or method.

Example usage with Flask: ::

    @app.route('/echo', methods=['get', 'post'])
    @parser.use_args({'name': fields.Str()}, location="querystring")
    def greet(args):
return 'Hello ' + args['name']

:param argmap: Either a `marshmallow.Schema`, a `dict`
    of argname -> `marshmallow.fields.Field` pairs, or a callable
    which accepts a request and returns a `marshmallow.Schema`.
:param str location: Where on the request to load values.
:param str unknown: A value to pass for ``unknown`` when calling the
    schema's ``load`` method.
:param bool as_kwargs: Whether to insert arguments as keyword arguments.
:param callable validate: Validation function that receives the dictionary
    of parsed arguments. If the function returns ``False``, the parser
    will raise a :exc:`ValidationError`.
:param int error_status_code: Status code passed to error handler functions when
    a `ValidationError` is raised.
:param dict error_headers: Headers passed to error handler functions when a
    a `ValidationError` is raised.

◆ use_kwargs()

typing.Callable use_kwargs (   self,
args,
**  kwargs 
)
Decorator that injects parsed arguments into a view function or method
as keyword arguments.

This is a shortcut to :meth:`use_args` with ``as_kwargs=True``.

Example usage with Flask: ::

    @app.route('/echo', methods=['get', 'post'])
    @parser.use_kwargs({'name': fields.Str()})
    def greet(name):
return 'Hello ' + name

Receives the same ``args`` and ``kwargs`` as :meth:`use_args`.

Field Documentation

◆ error_callback

error_callback

◆ int

int
static

◆ location

location

◆ schema_class

schema_class

◆ str

str
static

◆ Type

Type
static

◆ unknown

unknown

The documentation for this class was generated from the following file: