Skip to content

Commit a336502

Browse files
author
Jon M. Mease
committed
Integrate graph_objs codegen and improve codegen reliability
1 parent a9789d9 commit a336502

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

codegen/__init__.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import json
22
import os.path as opath
3-
import os
43
import shutil
54

6-
import time
7-
8-
from codegen.datatypes import build_datatypes_py, write_datatypes_py, append_figure_class
9-
from codegen.utils import TraceNode, PlotlyNode, LayoutNode, FrameNode
10-
from codegen.validators import write_validator_py, append_traces_validator_py
11-
5+
from codegen.graph_objs import (delete_graph_objs_non_generated_lines,
6+
append_graph_objs_generated_lines)
127

138
def perform_codegen():
149
outdir = 'plotly/'
15-
# outdir = 'codegen/output'
10+
11+
# Delete non-generated lines from graph_objs.py
12+
# ---------------------------------------------
13+
delete_graph_objs_non_generated_lines()
14+
15+
# Perform codegen imports
16+
# -----------------------
17+
# this must happend after deleting non-generated lines above
18+
from codegen.datatypes import (build_datatypes_py, write_datatypes_py,
19+
append_figure_class)
20+
from codegen.utils import TraceNode, PlotlyNode, LayoutNode, FrameNode
21+
from codegen.validators import (write_validator_py,
22+
append_traces_validator_py)
23+
1624
# Load plotly schema
1725
# ------------------
1826
with open('plotly/package_data/default-schema.json', 'r') as f:
@@ -73,6 +81,10 @@ def perform_codegen():
7381
# --------------------------------
7482
append_figure_class(datatypes_pkgdir, base_traces_node)
7583

84+
# Append generated lines to graph_objs.py
85+
# ------------------------------------
86+
append_graph_objs_generated_lines()
87+
7688

7789
if __name__ == '__main__':
7890
perform_codegen()

update_graph_objs.py renamed to codegen/graph_objs.py

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from __future__ import print_function
22

3-
from plotly.graph_objs import graph_objs_tools
4-
from plotly.graph_reference import ARRAYS, CLASSES, TRACE_NAMES
5-
63
FLAG = '# AUTO-GENERATED BELOW. DO NOT EDIT! See makefile.'
74

85

@@ -30,6 +27,8 @@ def get_non_generated_file_lines():
3027

3128

3229
def print_class(name, f):
30+
from plotly.graph_reference import ARRAYS, CLASSES, TRACE_NAMES
31+
3332
if name == 'Figure':
3433
print('from plotly.datatypes import Figure', file=f, end='\n\n\n')
3534
elif name == 'Layout':
@@ -48,20 +47,32 @@ def print_class(name, f):
4847
print(' pass', file=f, end='\n\n\n')
4948

5049

51-
copied_lines = get_non_generated_file_lines()
52-
with open('./plotly/graph_objs/graph_objs.py', 'w') as graph_objs_file:
50+
def delete_graph_objs_non_generated_lines():
51+
copied_lines = get_non_generated_file_lines()
52+
53+
with open('./plotly/graph_objs/graph_objs.py', 'w') as graph_objs_file:
54+
# Keep things *exactly* as they were above our special FLAG.
55+
for line in copied_lines:
56+
print(line, file=graph_objs_file, end='')
57+
print(FLAG, file=graph_objs_file)
58+
59+
60+
def append_graph_objs_generated_lines():
61+
from plotly.graph_reference import CLASSES
62+
63+
with open('./plotly/graph_objs/graph_objs.py', 'a') as graph_objs_file:
5364

54-
# Keep things *exactly* as they were above our special FLAG.
55-
for line in copied_lines:
56-
print(line, file=graph_objs_file, end='')
57-
print(FLAG, file=graph_objs_file)
65+
# For each object in the plot schema, generate a class in the file.
66+
class_names = list(CLASSES.keys())
67+
class_names.sort()
68+
for class_name in class_names:
69+
print_class(class_name, graph_objs_file)
5870

59-
# For each object in the plot schema, generate a class in the file.
60-
class_names = list(CLASSES.keys())
61-
class_names.sort()
62-
for class_name in class_names:
63-
print_class(class_name, graph_objs_file)
71+
# Print FigureWidget import
72+
print('from plotly.datatypes import FigureWidget\n\n',
73+
file=graph_objs_file)
6474

65-
# Finish off the file by only exporting plot-schema names.
66-
print('__all__ = [cls for cls in graph_reference.CLASSES.keys() '
67-
'if cls in globals()] + ["FigureWidget"]', file=graph_objs_file)
75+
# Finish off the file by only exporting plot-schema names.
76+
print("""\
77+
__all__ = [cls for cls in graph_reference.CLASSES.keys()
78+
if cls in globals()] + ['FigureWidget']""", file=graph_objs_file)

plotly/basedatatypes.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
from plotly import animation
1818
from plotly.basevalidators import CompoundValidator, CompoundArrayValidator, BaseDataValidator
1919
from plotly.callbacks import Points, BoxSelector, LassoSelector, InputState
20-
from plotly.validators.layout import (XAxisValidator, YAxisValidator, GeoValidator,
21-
TernaryValidator, SceneValidator)
20+
21+
# from plotly.validators.layout import (XAxisValidator, YAxisValidator, GeoValidator,
22+
# TernaryValidator, SceneValidator)
2223

2324

2425
class BaseFigure:
@@ -1806,15 +1807,24 @@ def _send_update(self, prop, val):
18061807

18071808

18081809
class BaseLayoutType(BaseLayoutHierarchyType):
1810+
1811+
18091812
_subplotid_prop_names = ['xaxis', 'yaxis', 'geo', 'ternary', 'scene']
1810-
_subplotid_validators = {'xaxis': XAxisValidator,
1811-
'yaxis': YAxisValidator,
1812-
'geo': GeoValidator,
1813-
'ternary': TernaryValidator,
1814-
'scene': SceneValidator}
18151813

18161814
_subplotid_prop_re = re.compile('(' + '|'.join(_subplotid_prop_names) + ')(\d+)')
18171815

1816+
@property
1817+
def _subplotid_validators(self):
1818+
from plotly.validators.layout import (XAxisValidator, YAxisValidator,
1819+
GeoValidator,
1820+
TernaryValidator, SceneValidator)
1821+
1822+
return {'xaxis': XAxisValidator,
1823+
'yaxis': YAxisValidator,
1824+
'geo': GeoValidator,
1825+
'ternary': TernaryValidator,
1826+
'scene': SceneValidator}
1827+
18181828
def __init__(self, plotly_name, **kwargs):
18191829
# Compute invalid kwargs. Pass to parent for error message
18201830
invalid_kwargs = {k: v for k, v in kwargs.items()

plotly/graph_objs/graph_objs.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,6 @@ def create(object_name, *args, **kwargs):
799799
return PlotlyDict(*args, **kwargs)
800800

801801

802-
from plotly.datatypes import FigureWidget
803-
804-
805802
# AUTO-GENERATED BELOW. DO NOT EDIT! See makefile.
806803
class AngularAxis(dict):
807804
pass
@@ -998,4 +995,8 @@ class ZAxis(dict):
998995
pass
999996

1000997

1001-
__all__ = [cls for cls in graph_reference.CLASSES.keys() if cls in globals()] + ["FigureWidget"]
998+
from plotly.datatypes import FigureWidget
999+
1000+
1001+
__all__ = [cls for cls in graph_reference.CLASSES.keys()
1002+
if cls in globals()] + ['FigureWidget']

0 commit comments

Comments
 (0)