From aa0c80906a58ce4f79b3251e095d1fe0b816cf15 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Sun, 19 Dec 2021 17:38:57 +0200 Subject: add set_file and set_path --- redis/commands/json/commands.py | 39 +++++++++++++++++++++++++++++++++++++++ tests/test_json.py | 12 ++++++++++++ 2 files changed, 51 insertions(+) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index e7f07b6..92b018c 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -4,6 +4,8 @@ from redis.exceptions import DataError from .decoders import decode_dict_keys from .path import Path +from json import loads, JSONDecodeError +import os class JSONCommands: @@ -213,6 +215,43 @@ class JSONCommands: pieces.append("XX") return self.execute_command("JSON.SET", *pieces) + def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False): + """ + Set the JSON value at key ``name`` under the ``path`` to the contents of the json file ``file_name``. + + ``nx`` if set to True, set ``value`` only if it does not exist. + ``xx`` if set to True, set ``value`` only if it exists. + ``decode_keys`` If set to True, the keys of ``obj`` will be decoded + with utf-8. + + """ + try: + file_content = loads(file_name) + except JSONDecodeError: + raise JSONDecodeError("Inappropriate file type, set_file() requires json file") + + return self.set(name, path, file_content, nx, xx, decode_keys) + + + def set_path(self, json_path, root_directory , nx=False, xx=False, decode_keys=False): + """ + + """ + set_files_result = {} + for root, dirs, files in os.walk(root_directory): + for file in files: + try: + file_name = file.rsplit('.')[0] + file_path = os.path.join(root, file) + self.set_file(file_name, json_path, file_path, nx, xx, decode_keys) + set_files_result[os.path.join(root, file)] = True + except JSONDecodeError: + set_files_result[os.path.join(root, file)] = False + + return set_files_result + + + def strlen(self, name, path=None): """Return the length of the string JSON value under ``path`` at key ``name``. diff --git a/tests/test_json.py b/tests/test_json.py index a99547d..74b00ea 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1392,3 +1392,15 @@ def test_custom_decoder(client): assert client.exists("foo") == 0 assert not isinstance(cj.__encoder__, json.JSONEncoder) assert not isinstance(cj.__decoder__, json.JSONDecoder) + + +@pytest.mark.redismod +def test_set_file(client): + import tempfile + import json + + jsonfile = tempfile.NamedTemporaryFile(suffix=".json") + with open(jsonfile.name, 'w+') as fp: + fp.write(json.dumps({"hello": "world"})) + + assert client.json().set("test", Path.rootPath(), jsonfile.name) \ No newline at end of file -- cgit v1.2.1 From 58d383f01c0859c3ff76a2a9695cf7f1730d6075 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Mon, 20 Dec 2021 12:16:04 +0200 Subject: fixing tests and lint --- redis/commands/json/commands.py | 33 +++++++++++++++++++-------------- tests/test_json.py | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 92b018c..626f2c1 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -1,11 +1,12 @@ +import os +from json import JSONDecodeError, loads + from deprecated import deprecated from redis.exceptions import DataError from .decoders import decode_dict_keys from .path import Path -from json import loads, JSONDecodeError -import os class JSONCommands: @@ -217,7 +218,8 @@ class JSONCommands: def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False): """ - Set the JSON value at key ``name`` under the ``path`` to the contents of the json file ``file_name``. + Set the JSON value at key ``name`` under the ``path`` to the content + of the json file ``file_name``. ``nx`` if set to True, set ``value`` only if it does not exist. ``xx`` if set to True, set ``value`` only if it exists. @@ -225,23 +227,28 @@ class JSONCommands: with utf-8. """ - try: - file_content = loads(file_name) - except JSONDecodeError: - raise JSONDecodeError("Inappropriate file type, set_file() requires json file") - + + with open(file_name, "r") as fp: + file_content = loads(fp.read()) + return self.set(name, path, file_content, nx, xx, decode_keys) + def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False): + """ + Iterate over ``root_folder`` and set each JSON file to a value + under ``json_path`` with the file name as the key. - def set_path(self, json_path, root_directory , nx=False, xx=False, decode_keys=False): - """ + ``nx`` if set to True, set ``value`` only if it does not exist. + ``xx`` if set to True, set ``value`` only if it exists. + ``decode_keys`` If set to True, the keys of ``obj`` will be decoded + with utf-8. """ set_files_result = {} - for root, dirs, files in os.walk(root_directory): + for root, dirs, files in os.walk(root_folder): for file in files: try: - file_name = file.rsplit('.')[0] + file_name = os.path.join(root, file).rsplit(".")[0] file_path = os.path.join(root, file) self.set_file(file_name, json_path, file_path, nx, xx, decode_keys) set_files_result[os.path.join(root, file)] = True @@ -250,8 +257,6 @@ class JSONCommands: return set_files_result - - def strlen(self, name, path=None): """Return the length of the string JSON value under ``path`` at key ``name``. diff --git a/tests/test_json.py b/tests/test_json.py index 74b00ea..9faf0ae 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1396,11 +1396,38 @@ def test_custom_decoder(client): @pytest.mark.redismod def test_set_file(client): - import tempfile import json + import tempfile + obj = {"hello": "world"} jsonfile = tempfile.NamedTemporaryFile(suffix=".json") - with open(jsonfile.name, 'w+') as fp: + with open(jsonfile.name, "w+") as fp: + fp.write(json.dumps(obj)) + + nojsonfile = tempfile.NamedTemporaryFile() + nojsonfile.write(b"Hello World") + + assert client.json().set_file("test", Path.rootPath(), jsonfile.name) + assert client.json().get("test") == obj + with pytest.raises(json.JSONDecodeError): + client.json().set_file("test2", Path.rootPath(), nojsonfile.name) + + +@pytest.mark.redismod +def test_set_path(client): + import json + import os + import tempfile + + root = tempfile.mkdtemp() + sub = tempfile.mkdtemp(dir=root) + ospointer, jsonfile = tempfile.mkstemp(suffix=".json", dir=sub) + ospointer2, nojsonfile = tempfile.mkstemp(dir=root) + + with open(jsonfile, "w+") as fp: fp.write(json.dumps({"hello": "world"})) + open(nojsonfile, "a+").write("hello") - assert client.json().set("test", Path.rootPath(), jsonfile.name) \ No newline at end of file + result = {"/private" + jsonfile: True, "/private" + nojsonfile: False} + assert client.json().set_path(Path.rootPath(), os.path.realpath(root)) == result + assert client.json().get("/private" + jsonfile.rsplit(".")[0]) == {"hello": "world"} -- cgit v1.2.1 From b58cd9015a33aa39f04eb417e9e4496e2961efad Mon Sep 17 00:00:00 2001 From: dvora-h Date: Mon, 20 Dec 2021 16:56:48 +0200 Subject: fix test_set_path --- tests/test_json.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_json.py b/tests/test_json.py index 9faf0ae..8bd1d77 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1416,18 +1416,18 @@ def test_set_file(client): @pytest.mark.redismod def test_set_path(client): import json - import os import tempfile root = tempfile.mkdtemp() sub = tempfile.mkdtemp(dir=root) - ospointer, jsonfile = tempfile.mkstemp(suffix=".json", dir=sub) - ospointer2, nojsonfile = tempfile.mkstemp(dir=root) + jsonfile = tempfile.mktemp(suffix=".json", dir=sub) + nojsonfile = tempfile.mktemp(dir=root) with open(jsonfile, "w+") as fp: fp.write(json.dumps({"hello": "world"})) open(nojsonfile, "a+").write("hello") - result = {"/private" + jsonfile: True, "/private" + nojsonfile: False} - assert client.json().set_path(Path.rootPath(), os.path.realpath(root)) == result - assert client.json().get("/private" + jsonfile.rsplit(".")[0]) == {"hello": "world"} + result = {jsonfile: True, nojsonfile: False} + print(result) + assert client.json().set_path(Path.rootPath(), root) == result + assert client.json().get(jsonfile.rsplit(".")[0]) == {"hello": "world"} -- cgit v1.2.1 From 1757d97e0483bd27917124508ce52105b57d2994 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Tue, 21 Dec 2021 10:38:58 +0200 Subject: fixing PR comments --- redis/commands/json/commands.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 626f2c1..a132b8e 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -231,7 +231,7 @@ class JSONCommands: with open(file_name, "r") as fp: file_content = loads(fp.read()) - return self.set(name, path, file_content, nx, xx, decode_keys) + return self.set(name, path, file_content, nx=nx, xx=xx, decode_keys=decode_keys) def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False): """ @@ -247,13 +247,20 @@ class JSONCommands: set_files_result = {} for root, dirs, files in os.walk(root_folder): for file in files: + file_path = os.path.join(root, file) try: - file_name = os.path.join(root, file).rsplit(".")[0] - file_path = os.path.join(root, file) - self.set_file(file_name, json_path, file_path, nx, xx, decode_keys) - set_files_result[os.path.join(root, file)] = True + file_name = file_path.rsplit(".")[0] + self.set_file( + file_name, + json_path, + file_path, + nx=nx, + xx=xx, + decode_keys=decode_keys, + ) + set_files_result[file_path] = True except JSONDecodeError: - set_files_result[os.path.join(root, file)] = False + set_files_result[file_path] = False return set_files_result -- cgit v1.2.1