Skip to content

Exposing the module version in loaded_modules #1648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ def loaded_modules(self):
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)
Expand Down
2 changes: 2 additions & 0 deletions redis/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class JSON(JSONCommands):
def __init__(
self,
client,
version=None,
decoder=JSONDecoder(),
encoder=JSONEncoder(),
):
Expand Down Expand Up @@ -62,6 +63,7 @@ def __init__(

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)
Expand Down
18 changes: 13 additions & 5 deletions redis/commands/redismodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion redis/commands/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ def commit(self):
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`

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
2 changes: 1 addition & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down