Esempio n. 1
0
 def encode(self, value):
     "Return a bytestring or bytes-like representation of the value"
     if isinstance(value, (bytes, memoryview)):
         return value
     elif isinstance(value, bool):
         # special case bool since it is a subclass of int
         raise DataError(
             "Invalid input of type: 'bool'. Convert to a "
             "bytes, string, int or float first."
         )
     elif isinstance(value, float):
         value = repr(value).encode()
     elif isinstance(value, (int, long)):
         # python 2 repr() on longs is '123L', so use str() instead
         value = str(value).encode()
     elif isinstance(value, (list, dict, tuple)):
         value = unicode(value)
     elif not isinstance(value, basestring):
         # a value we don't know how to deal with. throw an error
         typename = type(value).__name__
         raise DataError(
             "Invalid input of type: '%s'. Convert to a "
             "bytes, string, int or float first." % typename
         )
     if isinstance(value, unicode):
         value = value.encode(self.encoding, self.encoding_errors)
     return value
Esempio n. 2
0
 def debug(self, subcommand, key=None, path=Path.rootPath()):
     """Return the memory usage in bytes of a value under ``path`` from
     key ``name``.
     """
     valid_subcommands = ["MEMORY", "HELP"]
     if subcommand not in valid_subcommands:
         raise DataError("The only valid subcommands are ",
                         str(valid_subcommands))
     pieces = [subcommand]
     if subcommand == "MEMORY":
         if key is None:
             raise DataError("No key specified")
         pieces.append(key)
         pieces.append(str(path))
     return self.execute_command("JSON.DEBUG", *pieces)
Esempio n. 3
0
    def _appendWithLabels(params, with_labels, select_labels=None):
        """Append labels behavior to params."""
        if with_labels and select_labels:
            raise DataError(
                "with_labels and select_labels cannot be provided together.")

        if with_labels:
            params.extend(["WITHLABELS"])
        if select_labels:
            params.extend(["SELECTED_LABELS", *select_labels])
Esempio n. 4
0
 def hmset(self, name, mapping):
     """
     Sets each key in the ``mapping`` dict to its corresponding value
     in the hash ``name``
     """
     if not mapping:
         raise DataError("'hmset' with 'mapping' of length 0")
     items = []
     for pair in mapping.iteritems():
         items.extend(pair)
     return self.execute_command('HMSET', name, *items)
Esempio n. 5
0
    def debug(
        self,
        subcommand: str,
        key: Optional[str] = None,
        path: Optional[str] = Path.root_path(),
    ) -> Union[int, List[str]]:
        """Return the memory usage in bytes of a value under ``path`` from
        key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsondebug
        """  # noqa
        valid_subcommands = ["MEMORY", "HELP"]
        if subcommand not in valid_subcommands:
            raise DataError("The only valid subcommands are ", str(valid_subcommands))
        pieces = [subcommand]
        if subcommand == "MEMORY":
            if key is None:
                raise DataError("No key specified")
            pieces.append(key)
            pieces.append(str(path))
        return self.execute_command("JSON.DEBUG", *pieces)
Esempio n. 6
0
    def commit(self):
        # TODO: If fails check for user:sid:<sid> but accepts user:<user_id> update user:sid:<sid>
        # Set user sid
        if self._R.exists(f"user:sid:{self.sid}") or self._R.hget(
                f"user:{self.user_id}", "sid"):

            if (self._R.hget(f"user:{self.user_id}", "sid").decode("utf-8") !=
                    self.NO_SID):
                raise DataError("User SID already set")

        self._R.set(f"user:sid:{self.sid}", self.user_id)
        self._R.hset(f"user:{self.user_id}", "sid", self.sid)

        #! VV Not true VV. Must catch exception
        # Everything commited, so session is no longer dirty
        self.dirty = False
Esempio n. 7
0
    def test_ratelimit_exceptions(self, mock_get, mock_pipeline, mock_error):

        with self.app.test_request_context():
            g.db = database.get_session()

            self.assertFalse(RateLimit().call(uid=None),
                             msg='Anonymous requests should not be limited')

            mock_get.side_effect = ConnectionError('testlimit')
            self.assertFalse(RateLimit().call(uid=self.test_uid))
            mock_error.assert_called_with(
                dict(action='ratelimit.call', message='testlimit'))
            mock_get.side_effect = None

            mock_pipeline.side_effect = DataError('key not set')
            self.assertFalse(RateLimit().call(uid=self.test_uid))
            mock_error.assert_called_with(
                dict(action='ratelimit.call', message='key not set'))
            g.db.remove()
Esempio n. 8
0
    def sort(self,
             name,
             start=None,
             num=None,
             by=None,
             get=None,
             desc=False,
             alpha=False,
             store=None,
             groups=None):
        """Sort and return the list, set or sorted set at ``name``.

        ``start`` and ``num`` allow for paging through the sorted data

        ``by`` allows using an external key to weight and sort the items.
            Use an "*" to indicate where in the key the item value is located

        ``get`` allows for returning items from external keys rather than the
            sorted data itself.  Use an "*" to indicate where int he key
            the item value is located

        ``desc`` allows for reversing the sort

        ``alpha`` allows for sorting lexicographically rather than numerically

        ``store`` allows for storing the result of the sort into
            the key ``store``

        ClusterImpl: A full implementation of the server side sort mechanics because many of the
                     options work on multiple keys that can exist on multiple servers.
        """
        if (start is None and num is not None) or \
           (start is not None and num is None):
            raise RedisError(
                "RedisError: ``start`` and ``num`` must both be specified")
        try:
            data_type = b(self.type(name))

            if data_type == b("none"):
                return []
            elif data_type == b("set"):
                data = list(self.smembers(name))[:]
            elif data_type == b("list"):
                data = self.lrange(name, 0, -1)
            else:
                raise RedisClusterException(
                    "Unable to sort data type : {}".format(data_type))
            if by is not None:
                # _sort_using_by_arg mutates data so we don't
                # need need a return value.
                self._sort_using_by_arg(data, by, alpha)
            elif not alpha:
                data.sort(key=self._strtod_key_func)
            else:
                data.sort()
            if desc:
                data = data[::-1]
            if not (start is None and num is None):
                data = data[start:start + num]

            if get:
                data = self._retrive_data_from_sort(data, get)

            if store is not None:
                if data_type == b("set"):
                    self.delete(store)
                    self.rpush(store, *data)
                elif data_type == b("list"):
                    self.delete(store)
                    self.rpush(store, *data)
                else:
                    raise RedisClusterException(
                        "Unable to store sorted data for data type : {}".
                        format(data_type))

                return len(data)

            if groups:
                if not get or isinstance(get, basestring) or len(get) < 2:
                    raise DataError('when using "groups" the "get" argument '
                                    'must be specified and contain at least '
                                    'two keys')
                n = len(get)
                return list(izip(*[data[i::n] for i in range(n)]))
            else:
                return data
        except KeyError:
            return []