|
3 | 3 | from os.path import isfile, join
|
4 | 4 | import collections
|
5 | 5 | from urllib.parse import parse_qs
|
6 |
| -from fnmatch import fnmatch |
7 | 6 | import re
|
8 | 7 | import flask
|
9 | 8 |
|
@@ -101,23 +100,18 @@ def _parse_path_variables(pathname, path_template):
|
101 | 100 | returns **{"asset_id": "a100"}
|
102 | 101 | """
|
103 | 102 |
|
104 |
| - # parse variable definitions e.g. <var_name> from template |
105 |
| - # and create pattern to match |
106 |
| - wildcard_pattern = re.sub("<.*?>", "*", path_template) |
107 |
| - var_pattern = re.sub("<.*?>", "(.*)", path_template) |
| 103 | + # Copy of re._special_chars_map from Python >= 3.7 for backwards compatibility with 3.6 |
| 104 | + _special_chars_map = {i: '\\' + chr(i) for i in b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f'} |
| 105 | + escaped_template = path_template.translate(_special_chars_map) |
108 | 106 |
|
109 |
| - # check that static sections of the pathname match the template |
110 |
| - if not fnmatch(pathname, wildcard_pattern): |
111 |
| - return None |
112 |
| - |
113 |
| - # parse variable names e.g. var_name from template |
114 |
| - var_names = re.findall("<(.*?)>", path_template) |
115 |
| - |
116 |
| - # parse variables from path |
117 |
| - variables = re.findall(var_pattern, pathname) |
118 |
| - variables = variables[0] if isinstance(variables[0], tuple) else variables |
119 |
| - |
120 |
| - return dict(zip(var_names, variables)) |
| 107 | + pattern = re.compile( |
| 108 | + re.sub( |
| 109 | + "<(?P<var>.*?)>", r"(?P<\g<var>>.*)", |
| 110 | + escaped_template |
| 111 | + ) |
| 112 | + ) |
| 113 | + match = pattern.match(pathname) |
| 114 | + return match.groupdict() if match else None |
121 | 115 |
|
122 | 116 |
|
123 | 117 | def _create_redirect_function(redirect_to):
|
|
0 commit comments