|
1 | 1 | import re
|
| 2 | +import warnings |
| 3 | +import typing |
2 | 4 |
|
3 | 5 | from collections import OrderedDict
|
4 | 6 | from copy import deepcopy
|
|
20 | 22 | )
|
21 | 23 |
|
22 | 24 |
|
| 25 | +class FlaskCompatibilityWarning(DeprecationWarning): |
| 26 | + pass |
| 27 | + |
| 28 | + |
23 | 29 | def merge(first, second):
|
24 | 30 | """
|
25 | 31 | Recursively merges two dictionaries.
|
@@ -118,3 +124,43 @@ def unpack(response, default_code=HTTPStatus.OK):
|
118 | 124 | return data, code or default_code, headers
|
119 | 125 | else:
|
120 | 126 | raise ValueError("Too many response values")
|
| 127 | + |
| 128 | + |
| 129 | +def to_view_name(view_func: typing.Callable) -> str: |
| 130 | + """Helper that returns the default endpoint for a given |
| 131 | + function. This always is the function name. |
| 132 | +
|
| 133 | + Note: copy of simple flask internal helper |
| 134 | + """ |
| 135 | + assert view_func is not None, "expected view func if endpoint is not provided." |
| 136 | + return view_func.__name__ |
| 137 | + |
| 138 | + |
| 139 | +def import_check_view_func(): |
| 140 | + """ |
| 141 | + Resolve import flask _endpoint_from_view_func. |
| 142 | +
|
| 143 | + Show warning if function cannot be found and provide copy of last known implementation. |
| 144 | +
|
| 145 | + Note: This helper method exists because reoccurring problem with flask function, but |
| 146 | + actual method body remaining the same in each flask version. |
| 147 | + """ |
| 148 | + import importlib.metadata |
| 149 | + |
| 150 | + flask_version = importlib.metadata.version("flask").split(".") |
| 151 | + try: |
| 152 | + if flask_version[0] == "1": |
| 153 | + from flask.helpers import _endpoint_from_view_func |
| 154 | + elif flask_version[0] == "2": |
| 155 | + from flask.scaffold import _endpoint_from_view_func |
| 156 | + elif flask_version[0] == "3": |
| 157 | + from flask.sansio.scaffold import _endpoint_from_view_func |
| 158 | + else: |
| 159 | + warnings.simplefilter("once", FlaskCompatibilityWarning) |
| 160 | + _endpoint_from_view_func = None |
| 161 | + except ImportError: |
| 162 | + warnings.simplefilter("once", FlaskCompatibilityWarning) |
| 163 | + _endpoint_from_view_func = None |
| 164 | + if _endpoint_from_view_func is None: |
| 165 | + _endpoint_from_view_func = to_view_name |
| 166 | + return _endpoint_from_view_func |
0 commit comments