-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy path_validate.py
360 lines (309 loc) · 12.2 KB
/
_validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import collections
import re
from textwrap import dedent
from .development.base_component import Component
from . import exceptions
from ._utils import patch_collections_abc, _strings, stringify_id
def validate_callback(output, inputs, state, extra_args, types):
is_multi = isinstance(output, (list, tuple))
outputs = output if is_multi else [output]
Input, Output, State = types
if extra_args:
if not isinstance(extra_args[0], (Output, Input, State)):
raise exceptions.IncorrectTypeException(
dedent(
"""
Callback arguments must be `Output`, `Input`, or `State` objects,
optionally wrapped in a list or tuple. We found (possibly after
unwrapping a list or tuple):
{}
"""
).format(repr(extra_args[0]))
)
raise exceptions.IncorrectTypeException(
dedent(
"""
In a callback definition, you must provide all Outputs first,
then all Inputs, then all States. After this item:
{}
we found this item next:
{}
"""
).format(repr((outputs + inputs + state)[-1]), repr(extra_args[0]))
)
for args in [outputs, inputs, state]:
for arg in args:
validate_callback_arg(arg)
def validate_callback_arg(arg):
if not isinstance(getattr(arg, "component_property", None), _strings):
raise exceptions.IncorrectTypeException(
dedent(
"""
component_property must be a string, found {!r}
"""
).format(arg.component_property)
)
if hasattr(arg, "component_event"):
raise exceptions.NonExistentEventException(
"""
Events have been removed.
Use the associated property instead.
"""
)
if isinstance(arg.component_id, dict):
validate_id_dict(arg)
elif isinstance(arg.component_id, _strings):
validate_id_string(arg)
else:
raise exceptions.IncorrectTypeException(
dedent(
"""
component_id must be a string or dict, found {!r}
"""
).format(arg.component_id)
)
def validate_id_dict(arg):
arg_id = arg.component_id
for k in arg_id:
# Need to keep key type validation on the Python side, since
# non-string keys will be converted to strings in json.dumps and may
# cause unwanted collisions
if not isinstance(k, _strings):
raise exceptions.IncorrectTypeException(
dedent(
"""
Wildcard ID keys must be non-empty strings,
found {!r} in id {!r}
"""
).format(k, arg_id)
)
def validate_id_string(arg):
arg_id = arg.component_id
invalid_chars = ".{"
invalid_found = [x for x in invalid_chars if x in arg_id]
if invalid_found:
raise exceptions.InvalidComponentIdError(
"""
The element `{}` contains `{}` in its ID.
Characters `{}` are not allowed in IDs.
""".format(
arg_id, "`, `".join(invalid_found), "`, `".join(invalid_chars)
)
)
def validate_multi_return(outputs_list, output_value, callback_id):
if not isinstance(output_value, (list, tuple)):
raise exceptions.InvalidCallbackReturnValue(
dedent(
"""
The callback {} is a multi-output.
Expected the output type to be a list or tuple but got:
{}.
"""
).format(callback_id, repr(output_value))
)
if len(output_value) != len(outputs_list):
raise exceptions.InvalidCallbackReturnValue(
"""
Invalid number of output values for {}.
Expected {}, got {}
""".format(
callback_id, len(outputs_list), len(output_value)
)
)
for i, outi in enumerate(outputs_list):
if isinstance(outi, list):
vi = output_value[i]
if not isinstance(vi, (list, tuple)):
raise exceptions.InvalidCallbackReturnValue(
dedent(
"""
The callback {} output {} is a wildcard multi-output.
Expected the output type to be a list or tuple but got:
{}.
output spec: {}
"""
).format(callback_id, i, repr(vi), repr(outi))
)
if len(vi) != len(outi):
raise exceptions.InvalidCallbackReturnValue(
dedent(
"""
Invalid number of output values for {} item {}.
Expected {}, got {}
output spec: {}
output value: {}
"""
).format(callback_id, i, len(vi), len(outi), repr(outi), repr(vi))
)
def fail_callback_output(output_value, output):
valid = _strings + (dict, int, float, type(None), Component)
def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
bad_type = type(bad_val).__name__
outer_id = (
"(id={:s})".format(outer_val.id) if getattr(outer_val, "id", False) else ""
)
outer_type = type(outer_val).__name__
if toplevel:
location = dedent(
"""
The value in question is either the only value returned,
or is in the top level of the returned list,
"""
)
else:
index_string = "[*]" if index is None else "[{:d}]".format(index)
location = dedent(
"""
The value in question is located at
{} {} {}
{},
"""
).format(index_string, outer_type, outer_id, path)
raise exceptions.InvalidCallbackReturnValue(
dedent(
"""
The callback for `{output}`
returned a {object:s} having type `{type}`
which is not JSON serializable.
{location}
and has string representation
`{bad_val}`
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
"""
).format(
output=repr(output),
object="tree with one value" if not toplevel else "value",
type=bad_type,
location=location,
bad_val=bad_val,
)
)
def _value_is_valid(val):
return isinstance(val, valid)
def _validate_value(val, index=None):
# val is a Component
if isinstance(val, Component):
# pylint: disable=protected-access
for p, j in val._traverse_with_paths():
# check each component value in the tree
if not _value_is_valid(j):
_raise_invalid(bad_val=j, outer_val=val, path=p, index=index)
# Children that are not of type Component or
# list/tuple not returned by traverse
child = getattr(j, "children", None)
if not isinstance(child, (tuple, collections.MutableSequence)):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
path=p + "\n" + "[*] " + type(child).__name__,
index=index,
)
# Also check the child of val, as it will not be returned
child = getattr(val, "children", None)
if not isinstance(child, (tuple, collections.MutableSequence)):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
path=type(child).__name__,
index=index,
)
# val is not a Component, but is at the top level of tree
elif not _value_is_valid(val):
_raise_invalid(
bad_val=val,
outer_val=type(val).__name__,
path="",
index=index,
toplevel=True,
)
if isinstance(output_value, list):
for i, val in enumerate(output_value):
_validate_value(val, index=i)
else:
_validate_value(output_value)
# if we got this far, raise a generic JSON error
raise exceptions.InvalidCallbackReturnValue(
"""
The callback for property `{property:s}` of component `{id:s}`
returned a value which is not JSON serializable.
In general, Dash properties can only be dash components, strings,
dictionaries, numbers, None, or lists of those.
""".format(
property=output.component_property, id=output.component_id
)
)
def check_obsolete(kwargs):
for key in kwargs:
if key in ["components_cache_max_age", "static_folder"]:
raise exceptions.ObsoleteKwargException(
"""
{} is no longer a valid keyword argument in Dash since v1.0.
See https://dash.plotly.com for details.
""".format(
key
)
)
# any other kwarg mimic the built-in exception
raise TypeError("Dash() got an unexpected keyword argument '" + key + "'")
def validate_js_path(registered_paths, package_name, path_in_package_dist):
if package_name not in registered_paths:
raise exceptions.DependencyException(
"""
Error loading dependency. "{}" is not a registered library.
Registered libraries are:
{}
""".format(
package_name, list(registered_paths.keys())
)
)
if path_in_package_dist not in registered_paths[package_name]:
raise exceptions.DependencyException(
"""
"{}" is registered but the path requested is not valid.
The path requested: "{}"
List of registered paths: {}
""".format(
package_name, path_in_package_dist, registered_paths
)
)
def validate_index(name, checks, index):
missing = [i for check, i in checks if not re.compile(check).search(index)]
if missing:
plural = "s" if len(missing) > 1 else ""
raise exceptions.InvalidIndexException(
"Missing item{pl} {items} in {name}.".format(
items=", ".join(missing), pl=plural, name=name
)
)
def validate_layout_type(value):
if not isinstance(value, (Component, patch_collections_abc("Callable"))):
raise exceptions.NoLayoutException(
"Layout must be a dash component "
"or a function that returns a dash component."
)
def validate_layout(layout, layout_value):
if layout is None:
raise exceptions.NoLayoutException(
"""
The layout was `None` at the time that `run_server` was called.
Make sure to set the `layout` attribute of your application
before running the server.
"""
)
layout_id = stringify_id(getattr(layout_value, "id", None))
component_ids = {layout_id} if layout_id else set()
for component in layout_value._traverse(): # pylint: disable=protected-access
component_id = stringify_id(getattr(component, "id", None))
if component_id and component_id in component_ids:
raise exceptions.DuplicateIdError(
"""
Duplicate component id found in the initial layout: `{}`
""".format(
component_id
)
)
component_ids.add(component_id)