From e87c1d16bd46c779947aa9095ad48bc569fa3cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Wed, 21 Apr 2021 16:06:11 +0200 Subject: [PATCH] write_to_file_json has kwargs --- CHANGELOG.md | 1 + satella/__init__.py | 2 +- satella/json.py | 14 ++++++++------ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c1d57c2..32688963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,4 @@ * deprecated `IteratorListAdapter` * fixed a bug that disabled json if ujson was not installed +* write_to_file_json has kwargs diff --git a/satella/__init__.py b/satella/__init__.py index 8258b360..e797a16a 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.15.7a3' +__version__ = '2.15.7a4' diff --git a/satella/json.py b/satella/json.py index d29328a5..1a96cdcf 100644 --- a/satella/json.py +++ b/satella/json.py @@ -65,30 +65,32 @@ def json_encode(x: tp.Any) -> str: return JSONEncoder().encode(x) -def write_json_to_file(path: str, value: JSONAble) -> None: +def write_json_to_file(path: str, value: JSONAble, **kwargs) -> None: """ Write out a JSON to a file as UTF-8 encoded plain text. :param path: path to the file :param value: JSON-able content + :param kwargs: will be passed to ujson/json's dump """ if isinstance(value, JSONAble): value = value.to_json() with open(path, 'w') as f_out: try: import ujson - ujson.dump(value, f_out) + ujson.dump(value, f_out, **kwargs) except ImportError: - json.dump(value, f_out) + json.dump(value, f_out, **kwargs) -def write_json_to_file_if_different(path: str, value: JSONAble) -> bool: +def write_json_to_file_if_different(path: str, value: JSONAble, **kwargs) -> bool: """ Read JSON from a file. Write out a JSON to a file if it's value is different, as UTF-8 encoded plain text. :param path: path to the file :param value: JSON-able content + :param kwargs: will be passed to ujson/json dumps :return: whether the write actually happened """ if isinstance(value, JSONAble): @@ -96,11 +98,11 @@ def write_json_to_file_if_different(path: str, value: JSONAble) -> bool: try: val = read_json_from_file(path) if val != value: - write_json_to_file(path, value) + write_json_to_file(path, value, **kwargs) return True return False except (OSError, ValueError): - write_json_to_file(path, value) + write_json_to_file(path, value, **kwargs) return True -- GitLab