Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rm_list_item_error(self, mock_redis):
with self.assertRaises(RequiredArgument):
self.db.remove_list_item('1', None)
def test_set_ctx_with_error(self, mock_redis):
with self.assertRaises(RequiredArgument):
self.db.set_redisctx(None)
def select_kb(self, ctx, kbindex, set_global=False):
""" Use an existent redis connection and select a redis kb.
If needed, set the ctx as global.
Arguments:
ctx (redis obj): Redis context to use.
kbindex (str): The new kb to select
set_global (bool, optional): If should be the global context.
"""
if not ctx:
raise RequiredArgument('select_kb', 'ctx')
if not kbindex:
raise RequiredArgument('select_kb', 'kbindex')
ctx.execute_command('SELECT ' + str(kbindex))
if set_global:
self.set_redisctx(ctx)
self.db_index = str(kbindex)
def select_kb(self, ctx, kbindex, set_global=False):
""" Use an existent redis connection and select a redis kb.
If needed, set the ctx as global.
Arguments:
ctx (redis obj): Redis context to use.
kbindex (str): The new kb to select
set_global (bool, optional): If should be the global context.
"""
if not ctx:
raise RequiredArgument('select_kb', 'ctx')
if not kbindex:
raise RequiredArgument('select_kb', 'kbindex')
ctx.execute_command('SELECT ' + str(kbindex))
if set_global:
self.set_redisctx(ctx)
self.db_index = str(kbindex)
def get_elem_pattern_by_index(self, pattern, index=1, ctx=None):
""" Get all items with index 'index', stored under
a given pattern.
Arguments:
pattern (str): key pattern to match.
index (int, optional): Index of the element to get from the list.
ctx (redis obj, optional): Redis context to use.
Return a list with the elements under the matched key and given index.
"""
if not pattern:
raise RequiredArgument('get_elem_pattern_by_index', 'pattern')
if not ctx:
ctx = self.get_kb_context()
items = ctx.keys(pattern)
elem_list = []
for item in items:
elem_list.append([item, ctx.lindex(item, index)])
return elem_list
def set_single_item(self, name, value, ctx=None):
""" Set (replace) a single KB element.
Arguments:
name (str): key name of a list.
value (list): New elements to add to the key.
ctx (redis obj, optional): Redis context to use.
"""
if not name:
raise RequiredArgument('set_single_item', 'name')
if not value:
raise RequiredArgument('set_single_item', 'value')
if not ctx:
ctx = self.get_kb_context()
pipe = ctx.pipeline()
pipe.delete(name)
pipe.rpush(name, *set(value))
pipe.execute()
def set_redisctx(self, ctx):
""" Set the current rediscontext.
Arguments:
ctx (object): Redis context to be set as default.
"""
if not ctx:
raise RequiredArgument('set_redisctx', 'ctx')
self.rediscontext = ctx
def remove_list_item(self, key, value, ctx=None):
""" Remove item from the key list.
Arguments:
key (str): key name of a list.
value (str): Value to be removed from the key.
ctx (redis obj, optional): Redis context to use.
"""
if not key:
raise RequiredArgument('remove_list_item', 'key')
if not value:
raise RequiredArgument('remove_list_item ', 'value')
if not ctx:
ctx = self.get_kb_context()
ctx.lrem(key, count=LIST_ALL, value=value)
def add_single_item(self, name, values, ctx=None):
""" Add a single KB element with one or more values.
Arguments:
name (str): key name of a list.
value (list): Elements to add to the key.
ctx (redis obj, optional): Redis context to use.
"""
if not name:
raise RequiredArgument('add_list_item', 'name')
if not values:
raise RequiredArgument('add_list_item', 'value')
if not ctx:
ctx = self.get_kb_context()
ctx.rpush(name, *set(values))
def set_single_item(self, name, value, ctx=None):
""" Set (replace) a single KB element.
Arguments:
name (str): key name of a list.
value (list): New elements to add to the key.
ctx (redis obj, optional): Redis context to use.
"""
if not name:
raise RequiredArgument('set_single_item', 'name')
if not value:
raise RequiredArgument('set_single_item', 'value')
if not ctx:
ctx = self.get_kb_context()
pipe = ctx.pipeline()
pipe.delete(name)
pipe.rpush(name, *set(value))
pipe.execute()