flask_shortcut package¶
-
class
flask_shortcut.
Shortcut
(app: flask.app.Flask)[source]¶ Bases:
object
Object that handles the shortcut rerouting.
Calling an instance of this class on a view function gives the view an option to behave differently in non-production environments. The way in which the behavior may differ is constrained in three possible ways.
In the first one, only the arguments for a response are passed to the shortcut definition, meaning that the original view is effectively disabled and the route will instead just return the shortcut’s arguments as its only response.
In the second one, any number of shortcut response arguments are mapped to condition-keys. The condition is a json-like string that is used to assert a substructure in request bodies that reach that route, and will only apply its respective shortcut-response iff that substructure can be matched.
In the third one, a function represents the shortcut and can run arbitrary code to ensure whatever the user deems necessary on the request body, header, etc. Such a shortcut function may not accept arguments and needs to either return None, to signal that the shortcut condition failed and the original logic should be run, or valid response arguments in the form of a tuple.
If none of the condition can be satisfied, the route will run its original view.
There are two different ways to register shortcuts, one using decorators on the target functions before the are decorated as routes, and the other by running the a single wire call after all routes were added.
Example
Basic app setup:
>>> app = Flask(__name__) >>> short = Shortcut(app)
With shortcut decorator:
>>> @app.route('/my_route', methods=['GET']) ... @short.cut(('short_ok', 200)) ... def my_func(): ... return 'ok', 200
Equivalent post route-definition wiring:
>>> @app.route('/my_route', methods=['GET']) ... def my_func(): ... return 'ok', 200 >>> short.wire({'/my_route': ('short_ok', 200)})
-
cut
(mapping: Union[Tuple[Any, int], Dict[str, Tuple[Any, int]], Callable[Optional[Tuple[Any, int]]]])[source]¶ Returns view function wrappers.
Depending on the input argument, a different wrapper will be returned. This function can only run in applications that are not listed in the _EXCLUDE list.
- Parameters
mapping – The mapping that decides which types of shortcuts can be offered under which type of condition.
-
wire
(shortcuts: Dict[str, Union[Tuple[Any, int], Dict[str, Tuple[Any, int]], Callable[Optional[Tuple[Any, int]]]]])[source]¶ Manual wiring function.
If you don’t want to have the shortcut definitions in your routing file for some reason (e.g. there are lots of shortcuts and it would make the whole thing hard to read), you can use this function at some point after all view functions were registered and before the server is started.
- Parameters
shortcuts – A dictionary that maps routes to the mappings that would have been used as arguments in the shortcut decorator.
Submodules¶
flask_shortcut.shortcut module¶
-
flask_shortcut.shortcut.
RESPONSE_ARGS
¶ alias of
Tuple
[Any
,int
]
-
class
flask_shortcut.shortcut.
Shortcut
(app: flask.app.Flask)[source]¶ Bases:
object
Object that handles the shortcut rerouting.
Calling an instance of this class on a view function gives the view an option to behave differently in non-production environments. The way in which the behavior may differ is constrained in three possible ways.
In the first one, only the arguments for a response are passed to the shortcut definition, meaning that the original view is effectively disabled and the route will instead just return the shortcut’s arguments as its only response.
In the second one, any number of shortcut response arguments are mapped to condition-keys. The condition is a json-like string that is used to assert a substructure in request bodies that reach that route, and will only apply its respective shortcut-response iff that substructure can be matched.
In the third one, a function represents the shortcut and can run arbitrary code to ensure whatever the user deems necessary on the request body, header, etc. Such a shortcut function may not accept arguments and needs to either return None, to signal that the shortcut condition failed and the original logic should be run, or valid response arguments in the form of a tuple.
If none of the condition can be satisfied, the route will run its original view.
There are two different ways to register shortcuts, one using decorators on the target functions before the are decorated as routes, and the other by running the a single wire call after all routes were added.
Example
Basic app setup:
>>> app = Flask(__name__) >>> short = Shortcut(app)
With shortcut decorator:
>>> @app.route('/my_route', methods=['GET']) ... @short.cut(('short_ok', 200)) ... def my_func(): ... return 'ok', 200
Equivalent post route-definition wiring:
>>> @app.route('/my_route', methods=['GET']) ... def my_func(): ... return 'ok', 200 >>> short.wire({'/my_route': ('short_ok', 200)})
-
cut
(mapping: Union[Tuple[Any, int], Dict[str, Tuple[Any, int]], Callable[Optional[Tuple[Any, int]]]])[source]¶ Returns view function wrappers.
Depending on the input argument, a different wrapper will be returned. This function can only run in applications that are not listed in the _EXCLUDE list.
- Parameters
mapping – The mapping that decides which types of shortcuts can be offered under which type of condition.
-
wire
(shortcuts: Dict[str, Union[Tuple[Any, int], Dict[str, Tuple[Any, int]], Callable[Optional[Tuple[Any, int]]]]])[source]¶ Manual wiring function.
If you don’t want to have the shortcut definitions in your routing file for some reason (e.g. there are lots of shortcuts and it would make the whole thing hard to read), you can use this function at some point after all view functions were registered and before the server is started.
- Parameters
shortcuts – A dictionary that maps routes to the mappings that would have been used as arguments in the shortcut decorator.
flask_shortcut.util module¶
-
flask_shortcut.util.
diff
(target, sub, *, path_=None) → bool[source]¶ Simple recursive asymmetric diff function on json-like data structures.
- Parameters
target – The data container in which we want to find a subtree.
sub – The subtree that needs to be matched.
path – A private object used to produce meaningful error messages.
- Returns
True if the subtree could be cleanly matched, else False.
- Raises
TypeError – If the parse paths don’t match, e.g. the target is a list at a level where the subtree expected it to be a dictionary.
-
flask_shortcut.util.
get_request_data
() → Any[source]¶ Request data fetcher.
This function inspects the mimetype in order to figure out how the data can be represented in a standard python-dictionary form.
- Returns
- The data in a form that sticks as close a parsed json object as possible, since
that is the format in which the mappings expect to be passed by.
- Raises
ValueError – In case of unexpected request data formats.
Exception – In case of junk data, hard to list what each parser decides is best to raise.