Skip to content

Commit c1a1fc8

Browse files
committed
cope with datetimes when dumping JSON
1 parent a9f213c commit c1a1fc8

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ diff-cover.html: coverage.xml
148148

149149
## test : run the ${MODULE} test suite
150150
test: $(PYSOURCES)
151-
python setup.py test
151+
python setup.py test ${PYTEST_EXTRA}
152152

153153
## testcov : run the ${MODULE} test suite and collect coverage
154154
testcov: $(PYSOURCES)
155-
python setup.py test --addopts "--cov"
155+
python setup.py test --addopts "--cov" ${PYTEST_EXTRA}
156156

157157
sloccount.sc: $(PYSOURCES) Makefile
158158
sloccount --duplicates --wide --details $^ > $@

schema_salad/jsonld_context.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,16 @@ def makerdf(
263263
if isinstance(wf, MutableSequence):
264264
for w in wf:
265265
w["@context"] = ctx
266-
g.parse(data=json_dumps(w), format="json-ld", publicID=str(workflow))
266+
g.parse(
267+
data=json_dumps(w, default=str),
268+
format="json-ld",
269+
publicID=str(workflow),
270+
)
267271
elif isinstance(wf, MutableMapping):
268272
wf["@context"] = ctx
269-
g.parse(data=json_dumps(wf), format="json-ld", publicID=str(workflow))
273+
g.parse(
274+
data=json_dumps(wf, default=str), format="json-ld", publicID=str(workflow)
275+
)
270276
else:
271277
raise SchemaException(f"{wf} is not a workflow")
272278

schema_salad/main.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@ def main(argsl: Optional[List[str]] = None) -> int:
265265

266266
# Optionally print the schema after ref resolution
267267
if not args.document and args.print_pre:
268-
json_dump(schema_doc, fp=sys.stdout, indent=4)
268+
json_dump(schema_doc, fp=sys.stdout, indent=4, default=str)
269269
return 0
270270

271271
if not args.document and args.print_index:
272-
json_dump(list(metaschema_loader.idx.keys()), fp=sys.stdout, indent=4)
272+
json_dump(
273+
list(metaschema_loader.idx.keys()), fp=sys.stdout, indent=4, default=str
274+
)
273275
return 0
274276

275277
# Validate the schema document against the metaschema
@@ -322,21 +324,21 @@ def main(argsl: Optional[List[str]] = None) -> int:
322324
exc_info=((type(err), err, None) if args.debug else None),
323325
)
324326
if args.print_avro:
325-
json_dump(avsc_obj, fp=sys.stdout, indent=4)
327+
json_dump(avsc_obj, fp=sys.stdout, indent=4, default=str)
326328
return 1
327329
else:
328330
_logger.error("Schema `%s` must be a list.", args.schema)
329331
return 1
330332

331333
# Optionally print Avro-compatible schema from schema
332334
if args.print_avro:
333-
json_dump(avsc_obj, fp=sys.stdout, indent=4)
335+
json_dump(avsc_obj, fp=sys.stdout, indent=4, default=str)
334336
return 0
335337

336338
# Optionally print the json-ld context from the schema
337339
if args.print_jsonld_context:
338340
j = {"@context": schema_ctx}
339-
json_dump(j, fp=sys.stdout, indent=4, sort_keys=True)
341+
json_dump(j, fp=sys.stdout, indent=4, sort_keys=True, default=str)
340342
return 0
341343

342344
# Optionally print the RDFS graph from the schema
@@ -345,7 +347,7 @@ def main(argsl: Optional[List[str]] = None) -> int:
345347
return 0
346348

347349
if args.print_metadata and not args.document:
348-
json_dump(schema_metadata, fp=sys.stdout, indent=4)
350+
json_dump(schema_metadata, fp=sys.stdout, indent=4, default=str)
349351
return 0
350352

351353
if args.print_inheritance_dot:
@@ -379,11 +381,13 @@ def main(argsl: Optional[List[str]] = None) -> int:
379381

380382
# Optionally print the document after ref resolution
381383
if args.print_pre:
382-
json_dump(document, fp=sys.stdout, indent=4)
384+
json_dump(document, fp=sys.stdout, indent=4, default=str)
383385
return 0
384386

385387
if args.print_index:
386-
json_dump(list(document_loader.idx.keys()), fp=sys.stdout, indent=4)
388+
json_dump(
389+
list(document_loader.idx.keys()), fp=sys.stdout, indent=4, default=str
390+
)
387391
return 0
388392

389393
# Validate the user document against the schema
@@ -410,7 +414,7 @@ def main(argsl: Optional[List[str]] = None) -> int:
410414
return 1
411415

412416
if args.print_metadata:
413-
json_dump(doc_metadata, fp=sys.stdout, indent=4)
417+
json_dump(doc_metadata, fp=sys.stdout, indent=4, default=str)
414418
return 0
415419

416420
print(f"Document `{args.document}` is valid")

setup.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919
needs_pytest = {"pytest", "test", "ptr"}.intersection(sys.argv)
2020
pytest_runner = ["pytest < 7", "pytest-runner"] if needs_pytest else []
2121

22-
if os.path.exists("requirements.txt"):
23-
requirements = [
24-
r for r in open("requirements.txt").read().split("\n") if ";" not in r
25-
]
26-
else:
27-
# In tox, it will cover them anyway.
28-
requirements = []
29-
3022
USE_MYPYC = False
3123
# To compile with mypyc, a mypyc checkout must be present on the PYTHONPATH
3224
if len(sys.argv) > 1 and sys.argv[1] == "--use-mypyc":

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ description =
2626
py{36,37,38,39}-lint: Lint the Python code
2727
py{36,37,38,39}-bandit: Search for common security issues
2828
py{36,37,38,39}-mypy: Check for type safety
29-
py38-pydocstyle: docstring style checker
29+
py39-pydocstyle: docstring style checker
30+
py39-lint-readme: Lint the README.rst->.md conversion
3031

3132
passenv =
3233
CI
@@ -44,7 +45,7 @@ setenv =
4445

4546
commands =
4647
py{36,37,38,39}-unit: python -m pip install -U pip setuptools wheel
47-
py{36,37,38,39}-unit: make coverage-report coverage.xml
48+
py{36,37,38,39}-unit: make coverage-report coverage.xml PYTEST_EXTRA={posargs}
4849
py{36,37,38,39}-bandit: bandit --recursive --exclude schema_salad/tests/ schema_salad
4950
py{36,37,38,39}-lint: make flake8
5051
py{36,37,38,39}-lint: make format-check
@@ -64,7 +65,6 @@ deps =
6465
diff-cover
6566

6667
[testenv:py39-lint-readme]
67-
description = Lint the README.rst->.md conversion
6868
commands =
6969
python setup.py sdist
7070
python setup.py bdist_wheel

0 commit comments

Comments
 (0)