-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathvalidators.py
More file actions
132 lines (108 loc) · 3.71 KB
/
validators.py
File metadata and controls
132 lines (108 loc) · 3.71 KB
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
import os.path as opath
import json
import _plotly_utils.basevalidators
from codegen.utils import PlotlyNode, TraceNode
def get_validator_params(node: PlotlyNode, store: dict):
"""
Get params for the validator instance for the supplied node
and add them to the store.
Parameters
----------
node : PlotlyNode
The datatype node (node.is_datatype must evaluate to true) for which
to build a validator class
store : dict
Dictionary to store the JSON data for the validator
Returns
-------
None
"""
assert isinstance(store, dict)
assert node.is_datatype
raw_params = node.get_validator_params()
params = dict([(k, eval(v)) for k, v in raw_params.items()])
superclass_name = node.name_base_validator.split(".")[-1]
key = ".".join(node.parent_path_parts + (node.name_property,))
store[key] = {"params": params, "superclass": superclass_name}
def get_data_validator_params(base_trace_node: TraceNode, store: dict):
"""
Add a dict of constructor params for the DataValidator to the store.
Parameters
----------
base_trace_node : TraceNode
PlotlyNode that is the parent of all of the individual trace nodes
store : dict
Dictionary to store the JSON data for the validator
Returns
-------
None"""
assert isinstance(store, dict)
params = build_data_validator_params(base_trace_node)
store["data"] = {
"params": params,
"superclass": "BaseDataValidator",
}
def write_validator_json(outdir, params: dict):
"""
Write out a JSON serialization of the validator arguments
for all validators (keyed by f"{parent_name}.{plotly_name})
Each validator has a "params": {kwargs} entry and
a "superclass": str to indicate the class to be instantiated
Parameters
----------
outdir : str
Root outdir in which the validators package should reside
params : dict
Dictionary to store the JSON data for the validator
Returns
-------
None
"""
# Validate inputs
if not isinstance(params, dict):
raise ValueError("Expected params to be a dictionary")
# Write file
filepath = opath.join(outdir, "validators", "_validators.json")
with open(filepath, "w") as f:
f.write(json.dumps(params, indent=4))
def build_data_validator_params(base_trace_node: TraceNode):
"""
Build a dict of constructor params for the DataValidator.
(This is the validator that inputs a list of traces)
Parameters
----------
base_trace_node : PlotlyNode
PlotlyNode that is the parent of all of the individual trace nodes
Returns
-------
dict
Mapping from property name to repr-string of property value.
"""
# Get list of trace nodes
tracetype_nodes = base_trace_node.child_compound_datatypes
class_strs_map = dict(
[(node.name_property, node.name_datatype_class) for node in tracetype_nodes]
)
return {
"class_strs_map": class_strs_map,
"plotly_name": "data",
"parent_name": "",
}
def get_data_validator_instance(base_trace_node: TraceNode):
"""
Construct an instance of the DataValidator
(this is the validator that inputs a list of traces)
Parameters
----------
base_trace_node :
PlotlyNode that is the parent of all of the individual trace nodes
Returns
-------
BaseDataValidator
"""
# Build constructor params
# We need to eval the values to convert out of the repr-form of the
# params. e.g. '3' -> 3
params = build_data_validator_params(base_trace_node)
# Build and return BaseDataValidator instance
return _plotly_utils.basevalidators.BaseDataValidator(**params)