Skip to content

Commit 7abb2cf

Browse files
committed
Allow generate-spec to take optional output file
1 parent 24628f0 commit 7abb2cf

File tree

5 files changed

+532
-13
lines changed

5 files changed

+532
-13
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ 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

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
@@ -29,8 +29,8 @@ First, update the relevant model specification versions. For example, the commit
2929

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

3636
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 (CaselessStrEnum, Unicode, Tuple, List, Bool, CFloat,
911
Float, CInt, Int, Instance, Dict, Bytes, Any)
@@ -196,16 +198,28 @@ def create_markdown(spec):
196198
parser = argparse.ArgumentParser(description='Description of your program')
197199
parser.add_argument('-f', '--format', choices=['json', 'json-pretty', 'markdown'],
198200
help='Format to generate', default='json')
201+
parser.add_argument('output', nargs='?', type=pathlib.Path)
199202
args = parser.parse_args()
200203
format = args.format
201204

205+
if args.output:
206+
args.output.parent.mkdir(exist_ok=True)
207+
output = open(args.output, mode='w', encoding='utf8')
208+
else:
209+
output = sys.stdout
210+
202211
widgets_to_document = sorted(widgets.Widget._widget_types.items())
203212
spec = create_spec(widgets_to_document)
204-
if format == 'json':
205-
print(json.dumps(spec, sort_keys=True))
206-
elif format == 'json-pretty':
207-
print(json.dumps(spec, sort_keys=True,
208-
indent=2, separators=(',', ': ')))
209-
elif format == 'markdown':
210-
# We go through the json engine to convert tuples to lists, etc.
211-
print(create_markdown(json.loads(json.dumps(spec))))
213+
try:
214+
if format == 'json':
215+
json.dump(spec, output, sort_keys=True)
216+
elif format == 'json-pretty':
217+
json.dump(spec, output, sort_keys=True,
218+
indent=2, separators=(',', ': '))
219+
elif format == 'markdown':
220+
# We go through the json engine to convert tuples to lists, etc.
221+
output.write(create_markdown(json.loads(json.dumps(spec))))
222+
output.write('\n')
223+
finally:
224+
if args.output:
225+
output.close()

0 commit comments

Comments
 (0)