Skip to content

Commit 3307ddf

Browse files
committed
refactor: fix typing issues and cleanup code
Signed-off-by: Gabor Boros <gabor.brs@gmail.com>
1 parent c6a28be commit 3307ddf

15 files changed

+88
-413
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ maintainers = ["RethinkDB <bugs@rethinkdb.com>"]
3838
# RethinkDB is aliasing the commands to `rethinkdb <command>`, therefore it
3939
# can be executed as `rethinkdb dump` or `rethinkdb-dump`.
4040
[tool.poetry.scripts]
41-
rethinkdb = "rethinkdb.cli.main:cmd_main"
4241
rethinkdb-dump = "rethinkdb.cli._dump:cmd_dump"
4342
rethinkdb-export = "rethinkdb.cli._export:cmd_export"
4443
rethinkdb-import = "rethinkdb.cli._import:cmd_import"

rethinkdb/__init__.py

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,62 @@
1616
# Copyright 2010-2016 RethinkDB, all rights reserved.
1717

1818
import warnings
19+
from types import SimpleNamespace
20+
21+
from rethinkdb import net
22+
# pylint: disable=redefined-builtin
23+
from rethinkdb.query import (
24+
add, and_, april, args, asc, august, avg, binary, bit_and, bit_not, bit_or,
25+
bit_sal, bit_sar, bit_xor, branch, ceil, circle, contains, count, db,
26+
db_create, db_drop, db_list, december, desc, distance, distinct, div, do,
27+
epoch_time, eq, error, february, floor, format, friday, ge, geojson, grant,
28+
group, gt, http, info, intersects, iso8601, january, json, july, june, le,
29+
line, literal, lt, make_timezone, map, march, max, maxval, may, min, minval,
30+
mod, monday, mul, ne, not_, november, now, object, october, or_, point,
31+
polygon, random, range, reduce, round, row, saturday, september, sub, sum,
32+
sunday, table, table_create, table_drop, table_list, thursday, time, tuesday,
33+
type_of, union, uuid, wednesday, js
34+
)
35+
# pylint: enable=redefined-builtin
1936

20-
from rethinkdb import errors # , version
21-
22-
__all__ = ["r", "RethinkDB"]
2337
__version__ = "2.5.0"
2438

25-
26-
class RethinkDB:
39+
# Create the r namespace object containing all query functions
40+
r = SimpleNamespace()
41+
42+
query_functions = {
43+
'add': add, 'and_': and_, 'april': april, 'args': args, 'asc': asc,
44+
'august': august, 'avg': avg, 'binary': binary, 'bit_and': bit_and,
45+
'bit_not': bit_not, 'bit_or': bit_or, 'bit_sal': bit_sal, 'bit_sar': bit_sar,
46+
'bit_xor': bit_xor, 'branch': branch, 'ceil': ceil, 'circle': circle,
47+
'contains': contains, 'count': count, 'db': db, 'db_create': db_create,
48+
'db_drop': db_drop, 'db_list': db_list, 'december': december, 'desc': desc,
49+
'distance': distance, 'distinct': distinct, 'div': div, 'do': do,
50+
'epoch_time': epoch_time, 'eq': eq, 'error': error, 'february': february,
51+
'floor': floor, 'format': format, 'friday': friday, 'ge': ge, 'geojson': geojson,
52+
'grant': grant, 'group': group, 'gt': gt, 'http': http, 'info': info,
53+
'intersects': intersects, 'iso8601': iso8601, 'january': january, 'json': json,
54+
'july': july, 'june': june, 'le': le, 'line': line, 'literal': literal,
55+
'lt': lt, 'make_timezone': make_timezone, 'map': map, 'march': march,
56+
'max': max, 'maxval': maxval, 'may': may, 'min': min, 'minval': minval,
57+
'mod': mod, 'monday': monday, 'mul': mul, 'ne': ne, 'not_': not_,
58+
'november': november, 'now': now, 'object': object, 'october': october,
59+
'or_': or_, 'point': point, 'polygon': polygon, 'random': random,
60+
'range': range, 'reduce': reduce, 'round': round, 'row': row,
61+
'saturday': saturday, 'september': september, 'sub': sub, 'sum': sum,
62+
'sunday': sunday, 'table': table, 'table_create': table_create,
63+
'table_drop': table_drop, 'table_list': table_list, 'thursday': thursday,
64+
'time': time, 'tuesday': tuesday, 'type_of': type_of, 'union': union,
65+
'uuid': uuid, 'wednesday': wednesday, 'js': js
66+
}
67+
68+
for name, func in query_functions.items():
69+
setattr(r, name, func)
70+
71+
72+
class Client:
2773
"""
28-
RethinkDB serves as an entrypoint for queries.
74+
Client is a wrapper around RethinkDB connection handling.
2975
3076
It constructs the connection handlers and event loops, re-exports internal modules for easier
3177
use, and sets the event loop.
@@ -34,23 +80,11 @@ class RethinkDB:
3480
def __init__(self):
3581
super().__init__()
3682

37-
# pylint: disable=import-outside-toplevel
38-
from rethinkdb import ast, net, query
39-
40-
# Re-export internal modules for backward compatibility
41-
self.ast = ast
42-
self.errors = errors
4383
self.net = net
44-
self.query = query
4584

4685
net.Connection._r = self
4786
self.connection_type = None
4887

49-
# Dynamically assign every re-exported internal module's function to self
50-
for module in (self.net, self.query, self.ast, self.errors):
51-
for function_name in module.__all__:
52-
setattr(self, function_name, getattr(module, function_name))
53-
5488
# Ensure the `make_connection` function is not overridden accidentally
5589
self.make_connection = self.net.make_connection
5690
self.set_loop_type(None)
@@ -83,12 +117,9 @@ def set_loop_type(self, library=None) -> None:
83117
if library is None or self.connection_type is None:
84118
self.connection_type = self.net.DefaultConnection
85119

86-
def connect(self, *args, **kwargs):
120+
def connect(self, *connect_args, **kwargs):
87121
"""
88122
Make a connection to the database.
89123
"""
90124

91-
return self.make_connection(self.connection_type, *args, **kwargs)
92-
93-
94-
r = RethinkDB()
125+
return self.make_connection(self.connection_type, *connect_args, **kwargs)

rethinkdb/__init__.pyi

Lines changed: 0 additions & 185 deletions
This file was deleted.

rethinkdb/cli/_dump.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
import click
3535

36-
from rethinkdb import errors, r
36+
import rethinkdb as r
37+
from rethinkdb import errors
3738
from rethinkdb.cli.utils import (
3839
common_options,
3940
get_connection,

rethinkdb/cli/_export.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
from datetime import datetime
2525
import gzip
2626
import json
27+
import logging
2728
import os
29+
import sys
2830
import threading
2931
from typing import Any, Dict, List, Optional
3032

3133
import click
3234

33-
from rethinkdb import errors, r
35+
import rethinkdb as r
36+
from rethinkdb import errors
3437
from rethinkdb.cli.utils import (
3538
common_options,
3639
get_connection,
@@ -162,16 +165,17 @@ def export_worker(
162165
if export_format == "ndjson":
163166
f.write("\n")
164167
else: # csv
165-
csv_row = []
166-
for field in fields:
167-
val = row_to_write.get(field)
168-
if isinstance(val, (dict, list, bool)):
169-
csv_row.append(
170-
json.dumps(val, default=json_default)
171-
)
172-
else:
173-
csv_row.append(val)
174-
csv_writer.writerow(csv_row)
168+
if fields and csv_writer is not None:
169+
csv_row = []
170+
for field in fields:
171+
val = row_to_write.get(field)
172+
if isinstance(val, (dict, list, bool)):
173+
csv_row.append(
174+
json.dumps(val, default=json_default)
175+
)
176+
else:
177+
csv_row.append(val)
178+
csv_writer.writerow(csv_row)
175179

176180
rows_written += 1
177181
last_pk = doc[primary_key]

0 commit comments

Comments
 (0)