Skip to content

Commit 717488d

Browse files
committed
feat: merging code generator work with latest changes on master
1. Modify `commands.py` to run code generation. 1. Remove comments from generated code. 1. Replaced named arguments in constructors with positional arguments. 1. Regenerate all code. Notes: The generated code is reformatted once again: this slightly increases size but makes it more readable. There is one flaky test: `tests/test_plotly_utils/validators/test_colorscale_validator.py::test_acceptance_named[Inferno_r]` It fails when the entire test suite is run but does *not* fail when only `test_colorscale_validator.py` is run (using Python 3.11.9). fix
1 parent 556dd53 commit 717488d

File tree

14,844 files changed

+244506
-227154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

14,844 files changed

+244506
-227154
lines changed

codegen/datatypes.py

+8-44
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ def build_datatype_py(node):
6969
"""
7070

7171
# Validate inputs
72-
# ---------------
7372
assert node.is_compound
7473

7574
# Handle template traces
76-
# ----------------------
75+
#
7776
# We want template trace/layout classes like
7877
# plotly.graph_objs.layout.template.data.Scatter to map to the
7978
# corresponding trace/layout class (e.g. plotly.graph_objs.Scatter).
@@ -85,17 +84,14 @@ def build_datatype_py(node):
8584
return "from plotly.graph_objs import Layout"
8685

8786
# Extract node properties
88-
# -----------------------
8987
undercase = node.name_undercase
9088
datatype_class = node.name_datatype_class
9189
literal_nodes = [n for n in node.child_literals if n.plotly_name in ["type"]]
9290

9391
# Initialze source code buffer
94-
# ----------------------------
9592
buffer = StringIO()
9693

9794
# Imports
98-
# -------
9995
buffer.write(
10096
f"from plotly.basedatatypes "
10197
f"import {node.name_base_datatype} as _{node.name_base_datatype}\n"
@@ -106,7 +102,6 @@ def build_datatype_py(node):
106102
buffer.write(f"from warnings import warn\n")
107103

108104
# Write class definition
109-
# ----------------------
110105
buffer.write(
111106
f"""
112107
@@ -168,10 +163,9 @@ def _subplot_re_match(self, prop):
168163
valid_props_list = sorted(
169164
[node.name_property for node in subtype_nodes + literal_nodes]
170165
)
166+
# class properties
171167
buffer.write(
172168
f"""
173-
# class properties
174-
# --------------------
175169
_parent_path_str = '{node.parent_path_str}'
176170
_path_str = '{node.path_str}'
177171
_valid_props = {{"{'", "'.join(valid_props_list)}"}}
@@ -268,8 +262,6 @@ def {literal_node.name_property}(self):
268262
valid_props = {node.name_property for node in subtype_nodes}
269263
buffer.write(
270264
f"""
271-
# Self properties description
272-
# ---------------------------
273265
@property
274266
def _prop_descriptions(self):
275267
return \"\"\"\\"""
@@ -331,18 +323,17 @@ def __init__(self"""
331323
)
332324

333325
if datatype_class == "Layout":
334-
buffer.write(
335-
f"""
336326
# Override _valid_props for instance so that instance can mutate set
337327
# to support subplot properties (e.g. xaxis2)
328+
buffer.write(
329+
f"""
338330
self._valid_props = {{"{'", "'.join(valid_props_list)}"}}
339331
"""
340332
)
341333

334+
# Validate arg
342335
buffer.write(
343336
f"""
344-
# Validate arg
345-
# ------------
346337
if arg is None:
347338
arg = {{}}
348339
elif isinstance(arg, self.__class__):
@@ -355,19 +346,12 @@ def __init__(self"""
355346
constructor must be a dict or
356347
an instance of :class:`{class_name}`\"\"\")
357348
358-
# Handle skip_invalid
359-
# -------------------
360349
self._skip_invalid = kwargs.pop('skip_invalid', False)
361350
self._validate = kwargs.pop('_validate', True)
362351
"""
363352
)
364353

365-
buffer.write(
366-
f"""
367-
368-
# Populate data dict with properties
369-
# ----------------------------------"""
370-
)
354+
buffer.write("\n\n")
371355
for subtype_node in subtype_nodes:
372356
name_prop = subtype_node.name_property
373357
buffer.write(
@@ -377,13 +361,7 @@ def __init__(self"""
377361

378362
# ### Literals ###
379363
if literal_nodes:
380-
buffer.write(
381-
f"""
382-
383-
# Read-only literals
384-
# ------------------
385-
"""
386-
)
364+
buffer.write("\n\n")
387365
for literal_node in literal_nodes:
388366
lit_name = literal_node.name_property
389367
lit_val = repr(literal_node.node_data)
@@ -395,13 +373,7 @@ def __init__(self"""
395373

396374
buffer.write(
397375
f"""
398-
399-
# Process unknown kwargs
400-
# ----------------------
401376
self._process_kwargs(**dict(arg, **kwargs))
402-
403-
# Reset skip_invalid
404-
# ------------------
405377
self._skip_invalid = False
406378
"""
407379
)
@@ -420,7 +392,6 @@ def __init__(self"""
420392
)
421393

422394
# Return source string
423-
# --------------------
424395
return buffer.getvalue()
425396

426397

@@ -527,11 +498,9 @@ def add_docstring(
527498
528499
"""
529500
# Validate inputs
530-
# ---------------
531501
assert node.is_compound
532502

533503
# Build wrapped description
534-
# -------------------------
535504
node_description = node.description
536505
if node_description:
537506
description_lines = textwrap.wrap(
@@ -544,7 +513,6 @@ def add_docstring(
544513
node_description = "\n".join(description_lines) + "\n\n"
545514

546515
# Write header and description
547-
# ----------------------------
548516
buffer.write(
549517
f"""
550518
\"\"\"
@@ -555,7 +523,7 @@ def add_docstring(
555523
)
556524

557525
# Write parameter descriptions
558-
# ----------------------------
526+
559527
# Write any prepend extras
560528
for p, v in prepend_extras:
561529
v_wrapped = "\n".join(
@@ -594,7 +562,6 @@ def add_docstring(
594562
)
595563

596564
# Write return block and close docstring
597-
# --------------------------------------
598565
buffer.write(
599566
f"""
600567
@@ -623,16 +590,13 @@ def write_datatype_py(outdir, node):
623590
"""
624591

625592
# Build file path
626-
# ---------------
627593
# filepath = opath.join(outdir, "graph_objs", *node.parent_path_parts, "__init__.py")
628594
filepath = opath.join(
629595
outdir, "graph_objs", *node.parent_path_parts, "_" + node.name_undercase + ".py"
630596
)
631597

632598
# Generate source code
633-
# --------------------
634599
datatype_source = build_datatype_py(node)
635600

636601
# Write file
637-
# ----------
638602
write_source_py(datatype_source, filepath, leading_newlines=2)

codegen/validators.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def __init__(self, plotly_name={params['plotly_name']},
5454
# ### Write constructor ###
5555
buffer.write(
5656
f"""
57-
super().__init__(plotly_name=plotly_name,
58-
parent_name=parent_name"""
57+
super().__init__(plotly_name, parent_name"""
5958
)
6059

6160
# Write out remaining constructor parameters
@@ -198,10 +197,7 @@ def __init__(self, plotly_name={params['plotly_name']},
198197
parent_name={params['parent_name']},
199198
**kwargs):
200199
201-
super().__init__(class_strs_map={params['class_strs_map']},
202-
plotly_name=plotly_name,
203-
parent_name=parent_name,
204-
**kwargs)"""
200+
super().__init__({params['class_strs_map']}, plotly_name, parent_name, **kwargs)"""
205201
)
206202

207203
return buffer.getvalue()

commands.py

+37-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1+
from distutils import log
2+
import json
13
import os
2-
import sys
3-
import time
44
import platform
5-
import json
65
import shutil
7-
86
from subprocess import check_call
9-
from distutils import log
7+
import sys
8+
import time
109

11-
project_root = os.path.dirname(os.path.abspath(__file__))
12-
node_root = os.path.join(project_root, "js")
13-
is_repo = os.path.exists(os.path.join(project_root, ".git"))
14-
node_modules = os.path.join(node_root, "node_modules")
15-
targets = [
16-
os.path.join(project_root, "plotly", "package_data", "widgetbundle.js"),
10+
USAGE = "usage: python commands.py [updateplotlyjsdev | updateplotlyjs | codegen]"
11+
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
12+
NODE_ROOT = os.path.join(PROJECT_ROOT, "js")
13+
NODE_MODULES = os.path.join(NODE_ROOT, "node_modules")
14+
TARGETS = [
15+
os.path.join(PROJECT_ROOT, "plotly", "package_data", "widgetbundle.js"),
1716
]
1817

19-
npm_path = os.pathsep.join(
18+
NPM_PATH = os.pathsep.join(
2019
[
21-
os.path.join(node_root, "node_modules", ".bin"),
20+
os.path.join(NODE_ROOT, "node_modules", ".bin"),
2221
os.environ.get("PATH", os.defpath),
2322
]
2423
)
2524

2625
# Load plotly.js version from js/package.json
2726
def plotly_js_version():
28-
path = os.path.join(project_root, "js", "package.json")
27+
path = os.path.join(PROJECT_ROOT, "js", "package.json")
2928
with open(path, "rt") as f:
3029
package_json = json.load(f)
3130
version = package_json["dependencies"]["plotly.js"]
@@ -57,33 +56,33 @@ def install_js_deps(local):
5756
)
5857

5958
env = os.environ.copy()
60-
env["PATH"] = npm_path
59+
env["PATH"] = NPM_PATH
6160

6261
if has_npm:
6362
log.info("Installing build dependencies with npm. This may take a while...")
6463
check_call(
6564
[npmName, "install"],
66-
cwd=node_root,
65+
cwd=NODE_ROOT,
6766
stdout=sys.stdout,
6867
stderr=sys.stderr,
6968
)
7069
if local is not None:
7170
plotly_archive = os.path.join(local, "plotly.js.tgz")
7271
check_call(
7372
[npmName, "install", plotly_archive],
74-
cwd=node_root,
73+
cwd=NODE_ROOT,
7574
stdout=sys.stdout,
7675
stderr=sys.stderr,
7776
)
7877
check_call(
7978
[npmName, "run", "build"],
80-
cwd=node_root,
79+
cwd=NODE_ROOT,
8180
stdout=sys.stdout,
8281
stderr=sys.stderr,
8382
)
84-
os.utime(node_modules, None)
83+
os.utime(NODE_MODULES, None)
8584

86-
for t in targets:
85+
for t in TARGETS:
8786
if not os.path.exists(t):
8887
msg = "Missing file: %s" % t
8988
raise ValueError(msg)
@@ -100,7 +99,7 @@ def run_codegen():
10099

101100

102101
def overwrite_schema_local(uri):
103-
path = os.path.join(project_root, "codegen", "resources", "plot-schema.json")
102+
path = os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
104103
shutil.copyfile(uri, path)
105104

106105

@@ -109,13 +108,13 @@ def overwrite_schema(url):
109108

110109
req = requests.get(url)
111110
assert req.status_code == 200
112-
path = os.path.join(project_root, "codegen", "resources", "plot-schema.json")
111+
path = os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
113112
with open(path, "wb") as f:
114113
f.write(req.content)
115114

116115

117116
def overwrite_bundle_local(uri):
118-
path = os.path.join(project_root, "plotly", "package_data", "plotly.min.js")
117+
path = os.path.join(PROJECT_ROOT, "plotly", "package_data", "plotly.min.js")
119118
shutil.copyfile(uri, path)
120119

121120

@@ -125,13 +124,13 @@ def overwrite_bundle(url):
125124
req = requests.get(url)
126125
print("url:", url)
127126
assert req.status_code == 200
128-
path = os.path.join(project_root, "plotly", "package_data", "plotly.min.js")
127+
path = os.path.join(PROJECT_ROOT, "plotly", "package_data", "plotly.min.js")
129128
with open(path, "wb") as f:
130129
f.write(req.content)
131130

132131

133132
def overwrite_plotlyjs_version_file(plotlyjs_version):
134-
path = os.path.join(project_root, "plotly", "offline", "_plotlyjs_version.py")
133+
path = os.path.join(PROJECT_ROOT, "plotly", "offline", "_plotlyjs_version.py")
135134
with open(path, "w") as f:
136135
f.write(
137136
"""\
@@ -274,7 +273,7 @@ def update_schema_bundle_from_master():
274273
overwrite_schema_local(schema_uri)
275274

276275
# Update plotly.js url in package.json
277-
package_json_path = os.path.join(node_root, "package.json")
276+
package_json_path = os.path.join(NODE_ROOT, "package.json")
278277
with open(package_json_path, "r") as f:
279278
package_json = json.load(f)
280279

@@ -299,9 +298,18 @@ def update_plotlyjs_dev():
299298
run_codegen()
300299

301300

302-
if __name__ == "__main__":
303-
if "updateplotlyjsdev" in sys.argv:
301+
def main():
302+
if len(sys.argv) != 2:
303+
print(USAGE, file=sys.stderr)
304+
sys.exit(1)
305+
elif sys.argv[1] == "codegen":
306+
run_codegen()
307+
elif sys.argv[1] == "updateplotlyjsdev":
304308
update_plotlyjs_dev()
305-
elif "updateplotlyjs" in sys.argv:
309+
elif sys.argv[1] == "updateplotlyjs":
306310
print(plotly_js_version())
307311
update_plotlyjs(plotly_js_version())
312+
313+
314+
if __name__ == "__main__":
315+
main()

0 commit comments

Comments
 (0)