1
+ """Utility command runner."""
2
+
3
+ import argparse
1
4
from distutils import log
2
5
import json
3
6
import os
4
7
import platform
8
+ import requests
5
9
import shutil
6
10
from subprocess import check_call
7
11
import sys
8
12
import time
9
13
10
- USAGE = "usage: python commands.py [updateplotlyjsdev | updateplotlyjs | codegen]"
14
+ from codegen import perform_codegen
15
+
16
+
11
17
PROJECT_ROOT = os .path .dirname (os .path .abspath (__file__ ))
12
18
NODE_ROOT = os .path .join (PROJECT_ROOT , "js" )
13
19
NODE_MODULES = os .path .join (NODE_ROOT , "node_modules" )
@@ -89,24 +95,12 @@ def install_js_deps(local):
89
95
raise ValueError (msg )
90
96
91
97
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
-
102
98
def overwrite_schema_local (uri ):
103
99
path = os .path .join (PROJECT_ROOT , "codegen" , "resources" , "plot-schema.json" )
104
100
shutil .copyfile (uri , path )
105
101
106
102
107
103
def overwrite_schema (url ):
108
- import requests
109
-
110
104
req = requests .get (url )
111
105
assert req .status_code == 200
112
106
path = os .path .join (PROJECT_ROOT , "codegen" , "resources" , "plot-schema.json" )
@@ -120,8 +114,6 @@ def overwrite_bundle_local(uri):
120
114
121
115
122
116
def overwrite_bundle (url ):
123
- import requests
124
-
125
117
req = requests .get (url )
126
118
print ("url:" , url )
127
119
assert req .status_code == 200
@@ -145,8 +137,6 @@ def overwrite_plotlyjs_version_file(plotlyjs_version):
145
137
146
138
147
139
def request_json (url ):
148
- import requests
149
-
150
140
req = requests .get (url )
151
141
return json .loads (req .content .decode ("utf-8" ))
152
142
@@ -228,7 +218,7 @@ def update_bundle(plotly_js_version):
228
218
def update_plotlyjs (plotly_js_version ):
229
219
update_bundle (plotly_js_version )
230
220
update_schema (plotly_js_version )
231
- run_codegen ()
221
+ perform_codegen ()
232
222
233
223
234
224
# Update the plotly.js schema and bundle from master
@@ -296,20 +286,43 @@ def update_schema_bundle_from_master():
296
286
# Update project to a new development version of plotly.js
297
287
def update_plotlyjs_dev ():
298
288
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 ()
300
305
301
306
302
307
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" :
309
316
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 )
313
326
314
327
315
328
if __name__ == "__main__" :
0 commit comments