From fd67828ff76f94dd6486a87702472798b0d5aead 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 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'redis/client.py') diff --git a/redis/client.py b/redis/client.py index 1d73d32..e1c6846 100755 --- a/redis/client.py +++ b/redis/client.py @@ -67,6 +67,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 @@ -662,7 +689,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)) -- cgit v1.2.1