Skip to content

Commit 930509f

Browse files
bmaranvillegvwilson
authored andcommitted
generate validator class instances from json data
1 parent 1f911b6 commit 930509f

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

plotly/validator_cache.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
import importlib
21
from _plotly_utils.basevalidators import LiteralValidator
3-
2+
from plotly.validators._data import DataValidator
3+
import _plotly_utils.basevalidators as basevalidators
4+
import json
5+
import os.path as opath
46

57
class ValidatorCache(object):
68
_cache = {}
9+
_json_cache = None
710

811
@staticmethod
912
def get_validator(parent_path, prop_name):
13+
if ValidatorCache._json_cache is None:
14+
# Load the JSON validator params from the file
15+
validator_json_path = opath.join(
16+
opath.dirname(__file__), "validators", "_validators.json"
17+
)
18+
if not opath.exists(validator_json_path):
19+
raise FileNotFoundError(
20+
f"Validator JSON file not found: {validator_json_path}"
21+
)
22+
with open(validator_json_path, "r") as f:
23+
ValidatorCache._json_cache = json.load(f)
1024

1125
key = (parent_path, prop_name)
1226
if key not in ValidatorCache._cache:
1327

1428
if "." not in parent_path and prop_name == "type":
1529
# Special case for .type property of traces
1630
validator = LiteralValidator("type", parent_path, parent_path)
31+
elif parent_path == "" and prop_name == "data":
32+
# Special case for .data property of Figure
33+
validator = DataValidator
1734
else:
1835
lookup_name = None
1936
if parent_path == "layout":
@@ -24,11 +41,14 @@ def get_validator(parent_path, prop_name):
2441
lookup_name = match.group(1)
2542

2643
lookup_name = lookup_name or prop_name
27-
class_name = lookup_name.title() + "Validator"
28-
validator = getattr(
29-
importlib.import_module("plotly.validators." + parent_path),
30-
class_name,
31-
)(plotly_name=prop_name)
44+
lookup = f"{parent_path}.{lookup_name}" if parent_path else lookup_name
45+
validator_item = ValidatorCache._json_cache.get(lookup)
46+
print("validator_item", key, validator_item)
47+
validator = generate_validator(validator_item["params"], validator_item["superclass"])
3248
ValidatorCache._cache[key] = validator
3349

3450
return ValidatorCache._cache[key]
51+
52+
def generate_validator(params: dict, superclass_name: str):
53+
superclass = getattr(basevalidators, superclass_name)
54+
return superclass(**params)

0 commit comments

Comments
 (0)