Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asyncheck #223

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .bandit
100644 → 100755
Empty file.
Empty file modified .coveragerc
100644 → 100755
Empty file.
Empty file modified .editorconfig
100644 → 100755
Empty file.
Empty file modified .github/FUNDING.yml
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/bug_report.md
100644 → 100755
Empty file.
Empty file modified .github/ISSUE_TEMPLATE/feature_request.md
100644 → 100755
Empty file.
Empty file modified .github/PULL_REQUEST_TEMPLATE.md
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CODE_OF_CONDUCT.md
100644 → 100755
Empty file.
Empty file modified CONTRIBUTING.md
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified MANIFEST.in
100644 → 100755
Empty file.
Empty file modified Makefile
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified pytest.ini
100644 → 100755
Empty file.
Empty file modified requirements.txt
100644 → 100755
Empty file.
Empty file modified rethinkdb/__init__.py
100644 → 100755
Empty file.
Empty file modified rethinkdb/__main__.py
100644 → 100755
Empty file.
167 changes: 79 additions & 88 deletions rethinkdb/ast.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

__all__ = ["expr", "RqlQuery", "ReQLEncoder", "ReQLDecoder", "Repl"]


import base64
import binascii
import collections
Expand All @@ -26,7 +25,8 @@
import threading

from rethinkdb import ql2_pb2
from rethinkdb.errors import QueryPrinter, ReqlDriverCompileError, ReqlDriverError, T
from rethinkdb.errors import (QueryPrinter, ReqlDriverCompileError,
ReqlDriverError, T)

P_TERM = ql2_pb2.Term.TermType

Expand Down Expand Up @@ -77,7 +77,8 @@ 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.")
raise ReqlDriverCompileError(
"Second argument to `r.expr` must be a number.")

if nesting_depth <= 0:
raise ReqlDriverCompileError("Nesting depth limit exceeded.")
Expand All @@ -95,9 +96,7 @@ def expr(val, nesting_depth=20):
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__)
)
""" % (type(val).__name__))
return ISO8601(val.isoformat())
elif isinstance(val, RqlBinary):
return Binary(val)
Expand Down Expand Up @@ -136,12 +135,10 @@ def run(self, c=None, **global_optargs):
if Repl.repl_active:
raise ReqlDriverError(
"RqlQuery.run must be given a connection to run on. A default connection has been set with "
"`repl()` on another thread, but not this one."
)
"`repl()` on another thread, but not this one.")
else:
raise ReqlDriverError(
"RqlQuery.run must be given a connection to run on."
)
"RqlQuery.run must be given a connection to run on.")

return c._start(self, **global_optargs)

Expand All @@ -155,7 +152,7 @@ def __repr__(self):
# Compile this query to a json-serializable object
def build(self):
res = [self.term_type, self._args]
if len(self.optargs) > 0:
if self.optargs:
res.append(self.optargs)
return res

Expand Down Expand Up @@ -392,7 +389,10 @@ def set_difference(self, *args):
def __getitem__(self, index):
if isinstance(index, slice):
if index.stop:
return Slice(self, index.start or 0, index.stop, bracket_operator=True)
return Slice(self,
index.start or 0,
index.stop,
bracket_operator=True)
else:
return Slice(
self,
Expand All @@ -408,8 +408,7 @@ def __iter__(*args, **kwargs):
raise ReqlDriverError(
"__iter__ called on an RqlQuery object.\n"
"To iterate over the results of a query, call run first.\n"
"To iterate inside a query, use map or for_each."
)
"To iterate inside a query, use map or for_each.")

def get_field(self, *args):
return GetField(self, *args)
Expand Down Expand Up @@ -468,7 +467,7 @@ def max(self, *args, **kwargs):
def map(self, *args):
if len(args) > 0:
# `func_wrap` only the last argument
return Map(self, *(args[:-1] + (func_wrap(args[-1]),)))
return Map(self, *(args[:-1] + (func_wrap(args[-1]), )))
else:
return Map(self)

Expand All @@ -481,7 +480,8 @@ def fold(self, *args, **kwargs):
kwfuncargs = {}
for arg_name in kwargs:
kwfuncargs[arg_name] = func_wrap(kwargs[arg_name])
return Fold(self, *(args[:-1] + (func_wrap(args[-1]),)), **kwfuncargs)
return Fold(self, *(args[:-1] + (func_wrap(args[-1]), )),
**kwfuncargs)
else:
return Fold(self)

Expand All @@ -492,7 +492,10 @@ def concat_map(self, *args):
return ConcatMap(self, *[func_wrap(arg) for arg in args])

def order_by(self, *args, **kwargs):
args = [arg if isinstance(arg, (Asc, Desc)) else func_wrap(arg) for arg in args]
args = [
arg if isinstance(arg, (Asc, Desc)) else func_wrap(arg)
for arg in args
]
return OrderBy(self, *args, **kwargs)

def between(self, *args, **kwargs):
Expand Down Expand Up @@ -625,29 +628,34 @@ def needs_wrap(arg):


class RqlBoolOperQuery(RqlQuery):
statement_infix = None

def __init__(self, *args, **optargs):
super().__init__(*args, **optargs)
self.infix = False
RqlQuery.__init__(self, *args, **optargs)

def set_infix(self):
self.infix = True

def compose(self, args, optargs):
t_args = [
T("r.expr(", args[i], ")") if needs_wrap(self._args[i]) else args[i]
T("r.expr(", args[i], ")")
if needs_wrap(self._args[i]) else args[i]
for i in xrange(len(args))
]

if self.infix:
return T("(", T(*t_args, intsp=[" ", self.statement_infix, " "]), ")")
t_args = T(*t_args, intsp=[" ", self.statement_infix, " "])
return T("(", t_args, ")")
else:
return T("r.", self.statement, "(", T(*t_args, intsp=", "), ")")


class RqlBiOperQuery(RqlQuery):
def compose(self, args, optargs):
t_args = [
T("r.expr(", args[i], ")") if needs_wrap(self._args[i]) else args[i]
T("r.expr(", args[i], ")")
if needs_wrap(self._args[i]) else args[i]
for i in xrange(len(args))
]
return T("(", T(*t_args, intsp=[" ", self.statement, " "]), ")")
Expand All @@ -666,11 +674,10 @@ def __init__(self, *args, **optargs):
"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."
)
"`.and_` instead.")
raise ReqlDriverCompileError(
err % (self.statement, QueryPrinter(self).print_query())
)
err %
(self.statement, QueryPrinter(self).print_query()))
except AttributeError:
pass # No infix attribute, so not possible to be an infix bool operator

Expand Down Expand Up @@ -723,7 +730,7 @@ def __init__(self, offsetstr):
self.delta = datetime.timedelta(hours=hours, minutes=minutes)

def __getinitargs__(self):
return (self.offsetstr,)
return (self.offsetstr, )

def __copy__(self):
return RqlTzinfo(self.offsetstr)
Expand Down Expand Up @@ -751,9 +758,8 @@ def recursively_make_hashable(obj):
if isinstance(obj, list):
return tuple([recursively_make_hashable(i) for i in obj])
elif isinstance(obj, dict):
return frozenset(
[(k, recursively_make_hashable(v)) for k, v in dict_items(obj)]
)
return frozenset([(k, recursively_make_hashable(v))
for k, v in dict_items(obj)])
return obj


Expand Down Expand Up @@ -789,42 +795,31 @@ def __init__(self, reql_format_opts=None):
def convert_time(self, obj):
if "epoch_time" not in obj:
raise ReqlDriverError(
(
"pseudo-type TIME object %s does not "
+ 'have expected field "epoch_time".'
)
% json.dumps(obj)
)
("pseudo-type TIME object %s does not " +
'have expected field "epoch_time".') % json.dumps(obj))

if "timezone" in obj:
return datetime.datetime.fromtimestamp(
obj["epoch_time"], RqlTzinfo(obj["timezone"])
)
return datetime.datetime.fromtimestamp(obj["epoch_time"],
RqlTzinfo(obj["timezone"]))
else:
return datetime.datetime.utcfromtimestamp(obj["epoch_time"])

@staticmethod
def convert_grouped_data(obj):
if "data" not in obj:
raise ReqlDriverError(
(
"pseudo-type GROUPED_DATA object"
+ ' %s does not have the expected field "data".'
)
% json.dumps(obj)
)
return dict([(recursively_make_hashable(k), v) for k, v in obj["data"]])
("pseudo-type GROUPED_DATA object" +
' %s does not have the expected field "data".') %
json.dumps(obj))
return dict([(recursively_make_hashable(k), v)
for k, v in obj["data"]])

@staticmethod
def convert_binary(obj):
if "data" not in obj:
raise ReqlDriverError(
(
"pseudo-type BINARY object %s does not have "
+ 'the expected field "data".'
)
% json.dumps(obj)
)
("pseudo-type BINARY object %s does not have " +
'the expected field "data".') % json.dumps(obj))
return RqlBinary(base64.b64decode(obj["data"].encode("utf-8")))

def convert_pseudotype(self, obj):
Expand All @@ -837,16 +832,14 @@ def convert_pseudotype(self, obj):
return self.convert_time(obj)
elif time_format != "raw":
raise ReqlDriverError(
'Unknown time_format run option "%s".' % time_format
)
'Unknown time_format run option "%s".' % time_format)
elif reql_type == "GROUPED_DATA":
group_format = self.reql_format_opts.get("group_format")
if group_format is None or group_format == "native":
return self.convert_grouped_data(obj)
elif group_format != "raw":
raise ReqlDriverError(
'Unknown group_format run option "%s".' % group_format
)
'Unknown group_format run option "%s".' % group_format)
elif reql_type == "GEOMETRY":
# No special support for this. Just return the raw object
return obj
Expand All @@ -856,8 +849,8 @@ def convert_pseudotype(self, obj):
return self.convert_binary(obj)
elif binary_format != "raw":
raise ReqlDriverError(
'Unknown binary_format run option "%s".' % binary_format
)
'Unknown binary_format run option "%s".' %
binary_format)
else:
raise ReqlDriverError("Unknown pseudo-type %s" % reql_type)
# If there was no pseudotype, or the relevant format is raw, return
Expand Down Expand Up @@ -909,12 +902,13 @@ def build(self):
return self.optargs

def compose(self, args, optargs):
list_comp = [
T(repr(key), ": ", value) for key, value in dict_items(optargs)
]
t_value = T(*list_comp, intsp=", ")
return T(
"r.expr({",
T(
*[T(repr(key), ": ", value) for key, value in dict_items(optargs)],
intsp=", "
),
t_value,
"})",
)

Expand Down Expand Up @@ -1236,13 +1230,16 @@ class FunCall(RqlQuery):
# before passing it down to the base class constructor.
def __init__(self, *args):
if len(args) == 0:
raise ReqlDriverCompileError("Expected 1 or more arguments but found 0.")
raise ReqlDriverCompileError(
"Expected 1 or more arguments but found 0.")
args = [func_wrap(args[-1])] + list(args[:-1])
RqlQuery.__init__(self, *args)

def compose(self, args, optargs):
if len(args) != 2:
return T("r.do(", T(T(*(args[1:]), intsp=", "), args[0], intsp=", "), ")")
return T("r.do(", T(T(*(args[1:]), intsp=", "),
args[0],
intsp=", "), ")")

if isinstance(self._args[1], Datum):
args[1] = T("r.expr(", args[1], ")")
Expand Down Expand Up @@ -1712,12 +1709,10 @@ def __new__(cls, *args, **kwargs):

def __repr__(self):
excerpt = binascii.hexlify(self[0:6]).decode("utf-8")
excerpt = " ".join([excerpt[i : i + 2] for i in xrange(0, len(excerpt), 2)])
excerpt = (
", '%s%s'" % (excerpt, "..." if len(self) > 6 else "")
if len(self) > 0
else ""
)
excerpt = " ".join(
[excerpt[i:i + 2] for i in xrange(0, len(excerpt), 2)])
excerpt = (", '%s%s'" %
(excerpt, "..." if len(self) > 6 else "") if self else "")
return "<binary, %d byte%s%s>" % (
len(self),
"s" if len(self) != 1 else "",
Expand All @@ -1741,16 +1736,11 @@ def __init__(self, data):
raise ReqlDriverCompileError(
"Cannot convert a unicode string to binary, "
"use `unicode.encode()` to specify the "
"encoding."
)
"encoding.")
elif not isinstance(data, bytes):
raise ReqlDriverCompileError(
(
"Cannot convert %s to binary, convert the "
"object to a `bytes` object first."
)
% type(data).__name__
)
("Cannot convert %s to binary, convert the "
"object to a `bytes` object first.") % type(data).__name__)
else:
self.base64_data = base64.b64encode(data)

Expand All @@ -1759,16 +1749,17 @@ def __init__(self, data):
self.optargs = {}

def compose(self, args, optargs):
if len(self._args) == 0:
if self._args:
return T("r.", self.statement, "(bytes(<data>))")
else:
return RqlTopLevelQuery.compose(self, args, optargs)
return RqlTopLevelQuery.compose(self, args, optargs)

def build(self):
if len(self._args) == 0:
return {"$reql_type$": "BINARY", "data": self.base64_data.decode("utf-8")}
else:
return RqlTopLevelQuery.build(self)
if self._args:
return {
"$reql_type$": "BINARY",
"data": self.base64_data.decode("utf-8")
}
return RqlTopLevelQuery.build(self)


class Range(RqlTopLevelQuery):
Expand Down Expand Up @@ -1972,12 +1963,12 @@ def __init__(self, lmbd):
self._args.extend([MakeArray(*vrids), expr(lmbd(*vrs))])

def compose(self, args, optargs):
list_comp = [
v.compose([v._args[0].compose(None, None)], []) for v in self.vrs
]
return T(
"lambda ",
T(
*[v.compose([v._args[0].compose(None, None)], []) for v in self.vrs],
intsp=", "
),
T(*list_comp, intsp=", "),
": ",
args[1],
)
Expand Down
Empty file modified rethinkdb/asyncio_net/__init__.py
100644 → 100755
Empty file.
Loading