Ejemplo n.º 1
0
def expr(val, nesting_depth=20):
    """
        Convert a Python primitive into a RQL primitive value
    """
    if not isinstance(nesting_depth, int):
        raise ReqlDriverCompileError(
            "Second argument to `r.expr` must be a number.")

    if nesting_depth <= 0:
        raise ReqlDriverCompileError("Nesting depth limit exceeded.")

    if isinstance(val, RqlQuery):
        return val
    elif isinstance(val, collections.Callable):
        return Func(val)
    elif isinstance(val, (datetime.datetime, datetime.date)):
        if not hasattr(val, "tzinfo") or not val.tzinfo:
            raise ReqlDriverCompileError(
                """Cannot convert %s to ReQL time object
            without timezone information. You can add timezone information with
            the third party module \"pytz\" or by constructing ReQL compatible
            timezone values with r.make_timezone(\"[+-]HH:MM\"). Alternatively,
            use one of ReQL's bultin time constructors, r.now, r.time,
            or r.iso8601.
            """ % (type(val).__name__))
        return ISO8601(val.isoformat())
    elif isinstance(val, RqlBinary):
        return Binary(val)
    elif isinstance(val, (str, unicode)):
        return Datum(val)
    elif isinstance(val, bytes):
        return Binary(val)
    elif isinstance(val, collections.Mapping):
        # MakeObj doesn't take the dict as a keyword args to avoid
        # conflicting with the `self` parameter.
        obj = {}
        for k, v in dict_items(val):
            obj[k] = expr(v, nesting_depth - 1)
        return MakeObj(obj)
    elif isinstance(val, collections.Iterable):
        val = [expr(v, nesting_depth - 1) for v in val]
        return MakeArray(*val)
    else:
        return Datum(val)
Ejemplo n.º 2
0
    def __init__(self, data):
        # We only allow 'bytes' objects to be serialized as binary
        # Python 2 - `bytes` is equivalent to `str`, either will be accepted
        # Python 3 - `unicode` is equivalent to `str`, neither will be accepted
        if isinstance(data, RqlQuery):
            RqlTopLevelQuery.__init__(self, data)
        elif isinstance(data, unicode):
            raise ReqlDriverCompileError(
                "Cannot convert a unicode string to binary, "
                "use `unicode.encode()` to specify the "
                "encoding.")
        elif not isinstance(data, bytes):
            raise ReqlDriverCompileError(
                ("Cannot convert %s to binary, convert the "
                 "object to a `bytes` object first.") % type(data).__name__)
        else:
            self.base64_data = base64.b64encode(data)

            # Kind of a hack to get around composing
            self._args = []
            self.optargs = {}
Ejemplo n.º 3
0
    def __init__(self, *args, **optargs):
        RqlBiOperQuery.__init__(self, *args, **optargs)

        for arg in args:
            try:
                if arg.infix:
                    err = (
                        "Calling '%s' on result of infix bitwise operator:\n"
                        "%s.\n"
                        "This is almost always a precedence error.\n"
                        "Note that `a < b | b < c` <==> `a < (b | b) < c`.\n"
                        "If you really want this behavior, use `.or_` or "
                        "`.and_` instead.")
                    raise ReqlDriverCompileError(
                        err %
                        (self.statement, QueryPrinter(self).print_query()))
            except AttributeError:
                pass  # No infix attribute, so not possible to be an infix bool operator
Ejemplo n.º 4
0
 def __init__(self, obj_dict):
     super(MakeObj, self).__init__()
     for key, value in dict_items(obj_dict):
         if not isinstance(key, (str, unicode)):
             raise ReqlDriverCompileError("Object keys must be strings.")
         self.optargs[key] = expr(value)
Ejemplo n.º 5
0
 def __init__(self, *args):
     if len(args) == 0:
         raise ReqlDriverCompileError(
             "Expected 1 or more arguments but found 0.")
     args = [func_wrap(args[-1])] + list(args[:-1])
     RqlQuery.__init__(self, *args)