summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChayim <chayim@users.noreply.github.com>2021-10-26 14:24:54 +0300
committerGitHub <noreply@github.com>2021-10-26 14:24:54 +0300
commit396e7323959b7a79d72602afee3ef648dc23adf9 (patch)
treec50ad4bdfe0760b7334fde8fc027bb72ef444a72
parent866ac00b45a144753c40bb466e784a8917212172 (diff)
downloadredis-py-396e7323959b7a79d72602afee3ef648dc23adf9.tar.gz
Exposing the module version in loaded_modules (#1648)
This is useful for the case where one wants to instantiate a module, knowing the back end version. The reason: behaviour may differ based on redis module versions.
-rwxr-xr-xredis/client.py3
-rw-r--r--redis/commands/json/__init__.py2
-rw-r--r--redis/commands/redismodules.py18
-rw-r--r--redis/commands/search/__init__.py3
-rw-r--r--tests/test_connection.py2
5 files changed, 20 insertions, 8 deletions
diff --git a/redis/client.py b/redis/client.py
index 4db9887..986af7c 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -930,7 +930,8 @@ class Redis(RedisModuleCommands, CoreCommands, object):
return mods
try:
- mods = [f.get('name').lower() for f in self.info().get('modules')]
+ mods = {f.get('name').lower(): f.get('ver')
+ for f in self.info().get('modules')}
except TypeError:
mods = []
setattr(self, key, mods)
diff --git a/redis/commands/json/__init__.py b/redis/commands/json/__init__.py
index 92f1199..2e26de3 100644
--- a/redis/commands/json/__init__.py
+++ b/redis/commands/json/__init__.py
@@ -23,6 +23,7 @@ class JSON(JSONCommands):
def __init__(
self,
client,
+ version=None,
decoder=JSONDecoder(),
encoder=JSONEncoder(),
):
@@ -62,6 +63,7 @@ class JSON(JSONCommands):
self.client = client
self.execute_command = client.execute_command
+ self.MODULE_VERSION = version
for key, value in self.MODULE_CALLBACKS.items():
self.client.set_response_callback(key, value)
diff --git a/redis/commands/redismodules.py b/redis/commands/redismodules.py
index 2c9066a..3ecce29 100644
--- a/redis/commands/redismodules.py
+++ b/redis/commands/redismodules.py
@@ -9,18 +9,26 @@ class RedisModuleCommands:
def json(self, encoder=JSONEncoder(), decoder=JSONDecoder()):
"""Access the json namespace, providing support for redis json."""
- if 'rejson' not in self.loaded_modules:
+ try:
+ modversion = self.loaded_modules['rejson']
+ except IndexError:
raise ModuleError("rejson is not a loaded in the redis instance.")
from .json import JSON
- jj = JSON(client=self, encoder=encoder, decoder=decoder)
+ jj = JSON(
+ client=self,
+ version=modversion,
+ encoder=encoder,
+ decoder=decoder)
return jj
def ft(self, index_name="idx"):
"""Access the search namespace, providing support for redis search."""
- if 'search' not in self.loaded_modules:
- raise ModuleError("search is not a loaded in the redis instance.")
+ try:
+ modversion = self.loaded_modules['search']
+ except IndexError:
+ raise ModuleError("rejson is not a loaded in the redis instance.")
from .search import Search
- s = Search(client=self, index_name=index_name)
+ s = Search(client=self, version=modversion, index_name=index_name)
return s
diff --git a/redis/commands/search/__init__.py b/redis/commands/search/__init__.py
index 8320ad4..425578e 100644
--- a/redis/commands/search/__init__.py
+++ b/redis/commands/search/__init__.py
@@ -83,7 +83,7 @@ class Search(SearchCommands):
self.pipeline.execute()
self.current_chunk = 0
- def __init__(self, client, index_name="idx"):
+ def __init__(self, client, version=None, index_name="idx"):
"""
Create a new Client for the given index_name.
The default name is `idx`
@@ -91,6 +91,7 @@ class Search(SearchCommands):
If conn is not None, we employ an already existing redis connection
"""
self.client = client
+ self.MODULE_VERSION = version
self.index_name = index_name
self.execute_command = client.execute_command
self.pipeline = client.pipeline
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 6728e0a..fa9a2b0 100644
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -20,7 +20,7 @@ def test_invalid_response(r):
@skip_if_server_version_lt('4.0.0')
def test_loaded_modules(r, modclient):
assert r.loaded_modules == []
- assert 'rejson' in modclient.loaded_modules
+ assert 'rejson' in modclient.loaded_modules.keys()
@skip_if_server_version_lt('4.0.0')