1+ """Utility command runner."""
2+
3+ import argparse
14from distutils import log
25import json
36import os
47import platform
8+ import requests
59import shutil
610from subprocess import check_call
711import sys
812import time
913
10- USAGE = "usage: python commands.py [updateplotlyjsdev | updateplotlyjs | codegen]"
14+ from codegen import perform_codegen
15+
16+
1117PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
1218NODE_ROOT = os.path.join(PROJECT_ROOT, "js")
1319NODE_MODULES = os.path.join(NODE_ROOT, "node_modules")
@@ -89,24 +95,12 @@ def install_js_deps(local):
8995 raise ValueError(msg)
9096
9197
92- # Generate class hierarchy from Plotly JSON schema
93- def run_codegen():
94- if sys.version_info < (3, 8):
95- raise ImportError("Code generation must be executed with Python >= 3.8")
96-
97- from codegen import perform_codegen
98-
99- perform_codegen()
100-
101-
10298def overwrite_schema_local(uri):
10399 path = os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
104100 shutil.copyfile(uri, path)
105101
106102
107103def overwrite_schema(url):
108- import requests
109-
110104 req = requests.get(url)
111105 assert req.status_code == 200
112106 path = os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
@@ -120,8 +114,6 @@ def overwrite_bundle_local(uri):
120114
121115
122116def overwrite_bundle(url):
123- import requests
124-
125117 req = requests.get(url)
126118 print("url:", url)
127119 assert req.status_code == 200
@@ -145,8 +137,6 @@ def overwrite_plotlyjs_version_file(plotlyjs_version):
145137
146138
147139def request_json(url):
148- import requests
149-
150140 req = requests.get(url)
151141 return json.loads(req.content.decode("utf-8"))
152142
@@ -228,7 +218,7 @@ def update_bundle(plotly_js_version):
228218def update_plotlyjs(plotly_js_version):
229219 update_bundle(plotly_js_version)
230220 update_schema(plotly_js_version)
231- run_codegen ()
221+ perform_codegen ()
232222
233223
234224# Update the plotly.js schema and bundle from master
@@ -296,20 +286,43 @@ def update_schema_bundle_from_master():
296286# Update project to a new development version of plotly.js
297287def update_plotlyjs_dev():
298288 update_schema_bundle_from_master()
299- run_codegen()
289+ perform_codegen()
290+
291+
292+ def parse_args():
293+ """Parse command-line arguments."""
294+ parser = argparse.ArgumentParser()
295+ subparsers = parser.add_subparsers(dest="cmd", help="Available subcommands")
296+
297+ p_codegen = subparsers.add_parser("codegen", help="generate code")
298+ p_codegen.add_argument("--noformat", action="store_true", help="prevent reformatting")
299+
300+ p_updateplotlyjsdev = subparsers.add_parser("updateplotlyjsdev", help="update plotly.js for development")
301+
302+ p_updateplotlyjs = subparsers.add_parser("updateplotlyjs", help="update plotly.js")
303+
304+ return parser.parse_args()
300305
301306
302307def main():
303- if len(sys.argv) != 2:
304- print(USAGE, file=sys.stderr)
305- sys.exit(1)
306- elif sys.argv[1] == "codegen":
307- run_codegen()
308- elif sys.argv[1] == "updateplotlyjsdev":
308+ """Main driver."""
309+
310+ args = parse_args()
311+
312+ if args.cmd == "codegen":
313+ perform_codegen(noformat=args.noformat)
314+
315+ elif args.cmd == "updateplotlyjsdev":
309316 update_plotlyjs_dev()
310- elif sys.argv[1] == "updateplotlyjs":
311- print(plotly_js_version())
312- update_plotlyjs(plotly_js_version())
317+
318+ elif args.cmd == "updateplotlyjs":
319+ version = plotly_js_version()
320+ print(version)
321+ update_plotlyjs(version)
322+
323+ else:
324+ print(f"unknown command {args.cmd}", file=sys.stderr)
325+ sys.exit(1)
313326
314327
315328if __name__ == "__main__":
0 commit comments