From e5eaf56f0775f19594087aec9045e609414e20bf Mon Sep 17 00:00:00 2001 From: Andy McCurdy Date: Thu, 23 May 2019 15:26:12 -0700 Subject: case insensitive response callbacks. this change allows users to call client.execute_command('info') or client.execute_command('INFO') and get the same parsed result. Fixes #1168 --- redis/client.py | 30 +++++++++++++++++++++++++++++- tests/test_commands.py | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 6ae868b..b87fcf6 100755 --- a/redis/client.py +++ b/redis/client.py @@ -68,6 +68,33 @@ def dict_merge(*dicts): return merged +class CaseInsensitiveDict(dict): + "Case insensitive dict implementation. Assumes string keys only." + + def __init__(self, data): + for k, v in iteritems(data): + self[k.upper()] = v + + def __contains__(self, k): + return super(CaseInsensitiveDict, self).__contains__(k.upper()) + + def __delitem__(self, k): + super(CaseInsensitiveDict, self).__delitem__(k.upper()) + + def __getitem__(self, k): + return super(CaseInsensitiveDict, self).__getitem__(k.upper()) + + def get(self, k, default=None): + return super(CaseInsensitiveDict, self).get(k.upper(), default) + + def __setitem__(self, k, v): + super(CaseInsensitiveDict, self).__setitem__(k.upper(), v) + + def update(self, data): + data = CaseInsensitiveDict(data) + super(CaseInsensitiveDict, self).update(data) + + def parse_debug_object(response): "Parse the results of Redis's DEBUG OBJECT command into a Python dict" # The 'type' of the object is the first item in the response, but isn't @@ -663,7 +690,8 @@ class Redis(object): connection_pool = ConnectionPool(**kwargs) self.connection_pool = connection_pool - self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy() + self.response_callbacks = CaseInsensitiveDict( + self.__class__.RESPONSE_CALLBACKS) def __repr__(self): return "%s<%s>" % (type(self).__name__, repr(self.connection_pool)) diff --git a/tests/test_commands.py b/tests/test_commands.py index 33f78d5..0815427 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -54,6 +54,9 @@ class TestResponseCallbacks(object): r['a'] = 'foo' assert r['a'] == 'static' + def test_case_insensitive_command_names(self, r): + assert r.response_callbacks['del'] == r.response_callbacks['DEL'] + class TestRedisCommands(object): -- cgit v1.2.1