Skip to content

Commit 17d655d

Browse files
committed
Add support for creating gzipped JSON files with PathPlus
1 parent 044fed3 commit 17d655d

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

domdf_python_tools/paths.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
# stdlib
4343
import contextlib
44+
import gzip
4445
import json
4546
import os
4647
import pathlib
@@ -543,17 +544,20 @@ def dump_json(
543544
encoding: Optional[str] = "UTF-8",
544545
errors: Optional[str] = None,
545546
json_library: JsonLibrary = json, # type: ignore
547+
*,
548+
compress: bool = False,
546549
**kwargs,
547550
) -> None:
548-
"""
551+
r"""
549552
Dump ``data`` to the file as JSON.
550553
551554
:param data: The object to serialise to JSON.
552555
:param encoding: The encoding to write to the file in.
553556
:param errors:
554557
:param json_library: The JSON serialisation library to use.
555558
:default json_library: :mod:`json`
556-
:param kwargs: Keyword arguments to pass to the JSON serialisation function.
559+
:param compress: Whether to compress the JSON file using gzip.
560+
:param \*\*kwargs: Keyword arguments to pass to the JSON serialisation function.
557561
558562
.. versionadded:: 0.5.0
559563
@@ -562,8 +566,16 @@ def dump_json(
562566
Now uses :meth:`PathPlus.write_clean <domdf_python_tools.paths.PathPlus.write_clean>`
563567
rather than :meth:`PathPlus.write_text <domdf_python_tools.paths.PathPlus.write_text>`,
564568
and returns :py:obj:`None` rather than :class:`int`.
569+
570+
.. versionchanged:: 1.8.0
571+
572+
Added the ``compress`` keyword-only argument.
565573
"""
566574

575+
if compress:
576+
with gzip.open(self, mode="wt", encoding=encoding, errors=errors) as fp:
577+
return fp.write(json_library.dumps(data, **kwargs))
578+
567579
return self.write_clean(
568580
json_library.dumps(data, **kwargs),
569581
encoding=encoding,
@@ -575,24 +587,38 @@ def load_json(
575587
encoding: Optional[str] = "UTF-8",
576588
errors: Optional[str] = None,
577589
json_library: JsonLibrary = json, # type: ignore
590+
*,
591+
decompress: bool = False,
578592
**kwargs,
579593
) -> Any:
580-
"""
594+
r"""
581595
Load JSON data from the file.
582596
583597
:param encoding: The encoding to write to the file in.
584598
:param errors:
585599
:param json_library: The JSON serialisation library to use.
586600
:default json_library: :mod:`json`
587-
:param kwargs: Keyword arguments to pass to the JSON deserialisation function.
601+
:param decompress: Whether to decompress the JSON file using gzip.
602+
Will raise an exception if the file is not compressed.
603+
:param \*\*kwargs: Keyword arguments to pass to the JSON deserialisation function.
588604
589605
:return: The deserialised JSON data.
590606
591607
.. versionadded:: 0.5.0
608+
609+
.. versionchanged:: 1.8.0
610+
611+
Added the ``compress`` keyword-only argument.
592612
"""
593613

614+
if decompress:
615+
with gzip.open(self, mode="rt", encoding=encoding, errors=errors) as fp:
616+
content = fp.read()
617+
else:
618+
content = self.read_text(encoding=encoding, errors=errors)
619+
594620
return json_library.loads(
595-
self.read_text(encoding=encoding, errors=errors),
621+
content,
596622
**kwargs,
597623
)
598624

tests/test_paths.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ def test_dump_json(tmpdir):
580580
""")
581581

582582

583+
def test_dump_json_gzip(tmpdir):
584+
tmpdir_p = PathPlus(tmpdir)
585+
586+
tmp_file = tmpdir_p / "test.txt"
587+
588+
tmp_file.dump_json({"key": "value", "int": 1234, "float": 12.34}, compress=True)
589+
assert tmp_file.load_json(decompress=True) == {"key": "value", "int": 1234, "float": 12.34}
590+
591+
tmp_file.dump_json({"key": "value", "int": 1234, "float": 12.34}, indent=2, compress=True)
592+
assert tmp_file.load_json(decompress=True) == {"key": "value", "int": 1234, "float": 12.34}
593+
594+
583595
def test_load_json(tmpdir):
584596
tmpdir_p = PathPlus(tmpdir)
585597

0 commit comments

Comments
 (0)