Skip to content

Commit 4ce5622

Browse files
committed
feat: preparatory work for cleaning up code generator
1. Modify `codegen/__init__.py` and `./setup.py` so that `python setup.py --reformat=false` disables reformatting. 2. Add `bin/get_size.py` so that `python bin/get_size.py plotly build` reports the number of files and total size in bytes of the `plotly` directory (where generated code is put) and the `build` directory that is populated by `python setup.py build`. ``` $ python bin/get_size.py plotly build which num_files num_bytes src 15110 47664264 build 14982 45870886 ```
1 parent 3d36f14 commit 4ce5622

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Calculate total size and total number of files of package."""
2+
3+
from pathlib import Path
4+
import sys
5+
6+
7+
def main():
8+
"""Main driver."""
9+
assert len(sys.argv) == 3, "Usage: get_size.py src_dir build_dir"
10+
src_files, src_bytes = get_size(sys.argv[1])
11+
build_files, build_bytes = get_size(sys.argv[2])
12+
print(f"which\t{'num_files':8s}\t{'num_bytes':8s}")
13+
print(f"src\t{src_files:8d}\t{src_bytes:8d}")
14+
print(f"build\t{build_files:8d}\t{build_bytes:8d}")
15+
16+
17+
def get_size(root_dir):
18+
"""Count files and size in bytes."""
19+
num_files, num_bytes = 0, 0
20+
for f in Path(root_dir).glob("**/*.*"):
21+
num_files += 1
22+
num_bytes += f.stat().st_size
23+
return num_files, num_bytes
24+
25+
26+
if __name__ == "__main__":
27+
main()

packages/python/plotly/codegen/__init__.py

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

8787

88-
def perform_codegen():
88+
def perform_codegen(reformat=True):
8989
# Set root codegen output directory
9090
# ---------------------------------
9191
# (relative to project root)
@@ -337,9 +337,12 @@ def __getattr__(import_name):
337337
f.write(graph_objects_init_source)
338338

339339
# ### Run black code formatter on output directories ###
340-
subprocess.call(["black", "--target-version=py36", validators_pkgdir])
341-
subprocess.call(["black", "--target-version=py36", graph_objs_pkgdir])
342-
subprocess.call(["black", "--target-version=py36", graph_objects_path])
340+
if reformat:
341+
subprocess.call(["black", "--target-version=py36", validators_pkgdir])
342+
subprocess.call(["black", "--target-version=py36", graph_objs_pkgdir])
343+
subprocess.call(["black", "--target-version=py36", graph_objects_path])
344+
else:
345+
print("skipping reformatting")
343346

344347

345348
if __name__ == "__main__":

packages/python/plotly/setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,23 @@ def run(self):
176176

177177
class CodegenCommand(Command):
178178
description = "Generate class hierarchy from Plotly JSON schema"
179-
user_options = []
179+
user_options = [
180+
("reformat=", None, "reformat "),
181+
]
180182

181183
def initialize_options(self):
182-
pass
184+
self.reformat = "true"
183185

184186
def finalize_options(self):
185-
pass
187+
self.reformat = self.reformat.lower() in {"true", "t", "yes", "y", "1"}
186188

187189
def run(self):
188190
if sys.version_info < (3, 8):
189191
raise ImportError("Code generation must be executed with Python >= 3.8")
190192

191193
from codegen import perform_codegen
192194

193-
perform_codegen()
195+
perform_codegen(self.reformat)
194196

195197

196198
def overwrite_schema_local(uri):

0 commit comments

Comments
 (0)