Exemplo n.º 1
0
 def lset(self, key, index, value):
     """Emulate lset."""
     redis_list = self._get_list(key, 'LSET')
     if redis_list is None:
         raise ResponseError("no such key")
     try:
         redis_list[index] = self._encode(value)
     except IndexError:
         raise ResponseError("index out of range")
Exemplo n.º 2
0
    def set(self, key, value, ex=None, px=None, nx=False, xx=False):
        """
        Set the ``value`` for the ``key`` in the context of the provided kwargs.

        As per the behavior of the redis-py lib:
        If nx and xx are both set, the function does nothing and None is returned.
        If px and ex are both set, the preference is given to px.
        If the key is not set for some reason, the lib function returns None.
        """
        key = self._encode(key)
        value = self._encode(value)

        if nx and xx:
            return None
        mode = "nx" if nx else "xx" if xx else None
        if self._should_set(key, mode):
            expire = None
            if ex is not None:
                expire = ex if isinstance(ex, timedelta) else timedelta(seconds=ex)
            if px is not None:
                expire = px if isinstance(px, timedelta) else timedelta(milliseconds=px)

            if expire is not None and expire.total_seconds() <= 0:
                raise ResponseError("invalid expire time in SETEX")

            result = self._set(key, value)
            if expire:
                self._expire(key, expire)

            return result
Exemplo n.º 3
0
 def sadd(self, key, *values):
     """Emulate sadd."""
     if len(values) == 0:
         raise ResponseError("wrong number of arguments for 'sadd' command")
     redis_set = self._get_set(key, 'SADD', create=True)
     before_count = len(redis_set)
     redis_set.update(map(self._encode, values))
     after_count = len(redis_set)
     return after_count - before_count
Exemplo n.º 4
0
    def debug_object(self, key):
        if key not in self.redis:
            # as of redis 2.8, -2 returned if key does not exist
            raise ResponseError("ERR no such key")

        return {
            'encoding': 'raw',
            'refcount': 1,
            'lru_seconds_idle': 0,
            'lru': 0,
            'at': '0x7f76cb6c3cd0',
            'serializedlength': total_size(self.redis[key]),
            'type': 'Value'
        }
Exemplo n.º 5
0
def _lua_to_python(lval, return_status=False):
    """
    Patch MockRedis+Lua for Python 3 compatibility
    """
    # noinspection PyUnresolvedReferences
    import lua
    lua_globals = lua.globals()
    if lval is None:
        # Lua None --> Python None
        return None
    if lua_globals.type(lval) == 'table':
        # Lua table --> Python list
        pval = []
        for i in lval:
            if return_status:
                if i == 'ok':
                    return lval[i]
                if i == 'err':
                    raise ResponseError(lval[i])
            pval.append(Script._lua_to_python(lval[i]))
        return pval
    elif isinstance(lval, six.integer_types):
        # Lua number --> Python long
        return six.integer_types[-1](lval)
    elif isinstance(lval, float):
        # Lua number --> Python float
        return float(lval)
    elif lua_globals.type(lval) == 'userdata':
        # Lua userdata --> Python string
        return str(lval)
    elif lua_globals.type(lval) == 'string':
        # Lua string --> Python string
        return lval
    elif lua_globals.type(lval) == 'boolean':
        # Lua boolean --> Python bool
        return bool(lval)
    raise RuntimeError('Invalid Lua type: ' + str(lua_globals.type(lval)))
Exemplo n.º 6
0
 def _lua_to_python(lval, return_status=False):
     """
     Convert Lua object(s) into Python object(s), as at times Lua object(s)
     are not compatible with Python functions
     """
     import lua
     lua_globals = lua.globals()
     if lval is None:
         # Lua None --> Python None
         return None
     if lua_globals.type(lval) == "table":
         # Lua table --> Python list
         pval = []
         for i in lval:
             if return_status:
                 if i == 'ok':
                     return lval[i]
                 if i == 'err':
                     raise ResponseError(lval[i])
             pval.append(Script._lua_to_python(lval[i]))
         return pval
     elif isinstance(lval, long):
         # Lua number --> Python long
         return long(lval)
     elif isinstance(lval, float):
         # Lua number --> Python float
         return float(lval)
     elif lua_globals.type(lval) == "userdata":
         # Lua userdata --> Python string
         return str(lval)
     elif lua_globals.type(lval) == "string":
         # Lua string --> Python string
         return lval
     elif lua_globals.type(lval) == "boolean":
         # Lua boolean --> Python bool
         return bool(lval)
     raise RuntimeError("Invalid Lua type: " + str(lua_globals.type(lval)))