Skip to content

Commit cca73ba

Browse files
committed
feat: switch to argparse and clean up imports in commands.py
- Also removes `requires-optional.txt` (which was just confusing things)
1 parent a76b656 commit cca73ba

File tree

3 files changed

+45
-88
lines changed

3 files changed

+45
-88
lines changed

codegen/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def preprocess_schema(plotly_schema):
8989
items["colorscale"] = items.pop("concentrationscales")
9090

9191

92-
def perform_codegen(reformat=True):
92+
def perform_codegen(noformat=False):
9393
# Set root codegen output directory
9494
# ---------------------------------
9595
# (relative to project root)
@@ -341,15 +341,15 @@ def __getattr__(import_name):
341341
f.write(graph_objects_init_source)
342342

343343
# ### Run black code formatter on output directories ###
344-
if reformat:
344+
if noformat:
345+
print("skipping reformatting")
346+
else:
345347
target_version = [
346348
f"--target-version={v}" for v in BLACK_TARGET_VERSIONS.split()
347349
]
348350
subprocess.call(["black", *target_version, validators_pkgdir])
349351
subprocess.call(["black", *target_version, graph_objs_pkgdir])
350352
subprocess.call(["black", *target_version, graph_objects_path])
351-
else:
352-
print("skipping reformatting")
353353

354354

355355
if __name__ == "__main__":

commands.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
"""Utility command runner."""
2+
3+
import argparse
14
from distutils import log
25
import json
36
import os
47
import platform
8+
import requests
59
import shutil
610
from subprocess import check_call
711
import sys
812
import time
913

10-
USAGE = "usage: python commands.py [updateplotlyjsdev | updateplotlyjs | codegen]"
14+
from codegen import perform_codegen
15+
16+
1117
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
1218
NODE_ROOT = os.path.join(PROJECT_ROOT, "js")
1319
NODE_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-
10298
def overwrite_schema_local(uri):
10399
path = os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
104100
shutil.copyfile(uri, path)
105101

106102

107103
def 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

122116
def 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

147139
def 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):
228218
def 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
297287
def 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

302307
def 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

315328
if __name__ == "__main__":

requires-optional.txt

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)