Skip to content

Commit cd54e8e

Browse files
committed
Allow generate-spec to take optional output file
1 parent 130c0e2 commit cd54e8e

File tree

5 files changed

+7591
-13
lines changed

5 files changed

+7591
-13
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ jobs:
141141
pip install file://$PWD#egg=ipywidgets
142142
- name: Compare spec with latest version
143143
run: |
144-
python ./packages/schema/generate-spec.py -f markdown > spec.md
144+
python ./packages/schema/generate-spec.py -f markdown spec.md
145145
diff -u ./packages/schema/jupyterwidgetmodels.latest.md ./spec.md
146146
ui-test:
147147
name: Visual Regression

docs/source/dev_install.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Updating widget model specification
7575
7676
To update the widget model specification with changes, do something like this in the repo root:
7777
```
78-
python ./packages/schema/generate-spec.py -f json-pretty > packages/schema/jupyterwidgetmodels.latest.json
79-
python ./packages/schema/generate-spec.py -f markdown > packages/schema/jupyterwidgetmodels.latest.md
78+
python ./packages/schema/generate-spec.py -f json-pretty packages/schema/jupyterwidgetmodels.latest.json
79+
python ./packages/schema/generate-spec.py -f markdown packages/schema/jupyterwidgetmodels.latest.md
8080
```
8181
8282
Releasing new versions

docs/source/dev_release.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ First, update the relevant model specification versions. For example, the commit
2828

2929
Next, regenerate the model spec with the new version numbers by doing something like this in the repository root directory:
3030
```
31-
python ./packages/schema/generate-spec.py -f json-pretty > packages/schema/jupyterwidgetmodels.latest.json
32-
python ./packages/schema/generate-spec.py -f markdown > packages/schema/jupyterwidgetmodels.latest.md
31+
python ./packages/schema/generate-spec.py -f json-pretty packages/schema/jupyterwidgetmodels.latest.json
32+
python ./packages/schema/generate-spec.py -f markdown packages/schema/jupyterwidgetmodels.latest.md
3333
```
3434

3535
Copy `packages/schema/jupyterwidgetmodels.latest.md` to an appropriately-named

packages/schema/generate-spec.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import argparse
55
import json
66
from operator import itemgetter
7+
import pathlib
8+
import sys
79

810
from traitlets import (
911
CaselessStrEnum,
@@ -225,16 +227,28 @@ def create_markdown(spec):
225227
parser = argparse.ArgumentParser(description='Description of your program')
226228
parser.add_argument('-f', '--format', choices=['json', 'json-pretty', 'markdown'],
227229
help='Format to generate', default='json')
230+
parser.add_argument('output', nargs='?', type=pathlib.Path)
228231
args = parser.parse_args()
229232
format = args.format
230233

234+
if args.output:
235+
args.output.parent.mkdir(exist_ok=True)
236+
output = open(args.output, mode='w', encoding='utf8')
237+
else:
238+
output = sys.stdout
239+
231240
widgets_to_document = sorted(widgets.Widget._widget_types.items())
232241
spec = create_spec(widgets_to_document)
233-
if format == 'json':
234-
print(json.dumps(spec, sort_keys=True))
235-
elif format == 'json-pretty':
236-
print(json.dumps(spec, sort_keys=True,
237-
indent=2, separators=(',', ': ')))
238-
elif format == 'markdown':
239-
# We go through the json engine to convert tuples to lists, etc.
240-
print(create_markdown(json.loads(json.dumps(spec))))
242+
try:
243+
if format == 'json':
244+
json.dump(spec, output, sort_keys=True)
245+
elif format == 'json-pretty':
246+
json.dump(spec, output, sort_keys=True,
247+
indent=2, separators=(',', ': '))
248+
elif format == 'markdown':
249+
# We go through the json engine to convert tuples to lists, etc.
250+
output.write(create_markdown(json.loads(json.dumps(spec))))
251+
output.write('\n')
252+
finally:
253+
if args.output:
254+
output.close()

0 commit comments

Comments
 (0)