Skip to content

Commit 82fad4e

Browse files
committed
Merge branch 'master' of github.com:rethinkdb/rethinkdb-python into sawdog
2 parents cf766c6 + e57884c commit 82fad4e

16 files changed

+719
-55
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ test-ci:
5555
@killall rethinkdb
5656

5757
test-remote:
58+
curl -qo ${REMOTE_TEST_SETUP_NAME} ${REMOTE_TEST_SETUP_URL}
5859
python ${REMOTE_TEST_SETUP_NAME} pytest -m integration
5960

6061
install-db:
@@ -63,7 +64,7 @@ install-db:
6364
upload-coverage:
6465
@sh scripts/upload-coverage.sh
6566

66-
upload-pypi:
67+
upload-pypi: prepare
6768
@sh scripts/upload-pypi.sh
6869

6970
clean:

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RethinkDB Python driver
2-
[![Build Status](https://travis-ci.org/rethinkdb/rethinkdb-python.svg?branch=master)](https://travis-ci.org/rethinkdb/rethinkdb-python) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2b5231a6f90a4a1ba2fc795f8466bbe4)](https://www.codacy.com/app/rethinkdb/rethinkdb-python?utm_source=github.com&utm_medium=referral&utm_content=rethinkdb/rethinkdb-python&utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/2b5231a6f90a4a1ba2fc795f8466bbe4)](https://www.codacy.com/app/rethinkdb/rethinkdb-python?utm_source=github.com&utm_medium=referral&utm_content=rethinkdb/rethinkdb-python&utm_campaign=Badge_Coverage)
2+
[![PyPI version](https://badge.fury.io/py/rethinkdb.svg)](https://badge.fury.io/py/rethinkdb) [![Build Status](https://travis-ci.org/rethinkdb/rethinkdb-python.svg?branch=master)](https://travis-ci.org/rethinkdb/rethinkdb-python) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2b5231a6f90a4a1ba2fc795f8466bbe4)](https://www.codacy.com/app/rethinkdb/rethinkdb-python?utm_source=github.com&utm_medium=referral&utm_content=rethinkdb/rethinkdb-python&utm_campaign=Badge_Grade) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/2b5231a6f90a4a1ba2fc795f8466bbe4)](https://www.codacy.com/app/rethinkdb/rethinkdb-python?utm_source=github.com&utm_medium=referral&utm_content=rethinkdb/rethinkdb-python&utm_campaign=Badge_Coverage)
33

44
## Overview
55

@@ -29,6 +29,7 @@ non-blocking I/O through multiple async frameworks:
2929
* [Asyncio](https://docs.python.org/3/library/asyncio.html)
3030
* [Gevent](http://www.gevent.org/)
3131
* [Tornado](https://www.tornadoweb.org/en/stable/)
32+
* [Trio](https://trio.readthedocs.io/en/latest/)
3233
* [Twisted](https://twistedmatrix.com/trac/)
3334

3435
The following examples demonstrate how to use the driver in each mode.
@@ -148,6 +149,45 @@ def main():
148149
IOLoop.current().run_sync(main)
149150
```
150151

152+
### Trio mode
153+
154+
```python
155+
from rethinkdb import RethinkDB
156+
import trio
157+
158+
async def main():
159+
r = RethinkDB()
160+
r.set_loop_type('trio')
161+
async with trio.open_nursery() as nursery:
162+
async with r.open(db='test', nursery=nursery) as conn:
163+
await r.table_create('marvel').run(conn)
164+
marvel_heroes = r.table('marvel')
165+
await marvel_heroes.insert({
166+
'id': 1,
167+
'name': 'Iron Man',
168+
'first_appearance': 'Tales of Suspense #39'
169+
}).run(conn)
170+
171+
# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
172+
# call "await cursor.next()" in a loop.
173+
cursor = await marvel_heroes.run(conn)
174+
async with cursor:
175+
async for hero in cursor:
176+
print(hero['name'])
177+
178+
trio.run(main)
179+
```
180+
181+
The Trio mode also supports a database connection pool. You can modify the example above
182+
as follows:
183+
184+
```python
185+
db_pool = r.ConnectionPool(db='test', nursery=nursery)
186+
async with db_pool.connection() as conn:
187+
...
188+
await db_pool.close()
189+
```
190+
151191
### Twisted mode
152192

153193
```python
@@ -212,6 +252,7 @@ Remote test will create a new temporary SSH key and a Droplet for you until the
212252
| DO_REGION | sfo2 |
213253

214254
```bash
255+
$ pip install paramiko python-digitalocean
215256
$ export DO_TOKEN=<YOUR_TOKEN>
216257
$ make test-remote
217258
```

requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
codacy-coverage==1.3.11
22
mock==2.0.0
33
pytest-cov==2.6.1
4-
pytest==4.1.1
5-
paramiko==2.4.2
6-
python-digitalocean==1.13.2
4+
pytest==4.3.0
75
six==1.12.0

rethinkdb/__init__.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import os
15+
import imp
16+
import pkg_resources
1417

1518
from rethinkdb import errors, version
1619

@@ -30,7 +33,16 @@ class RethinkDB(builtins.object):
3033
def __init__(self):
3134
super(RethinkDB, self).__init__()
3235

33-
from rethinkdb import _dump, _export, _import, _index_rebuild, _restore, ast, query, net
36+
from rethinkdb import (
37+
_dump,
38+
_export,
39+
_import,
40+
_index_rebuild,
41+
_restore,
42+
ast,
43+
query,
44+
net
45+
)
3446

3547
self._dump = _dump
3648
self._export = _export
@@ -43,3 +55,31 @@ def __init__(self):
4355
for module in (net, query, ast, errors):
4456
for function_name in module.__all__:
4557
setattr(self, function_name, getattr(module, function_name))
58+
59+
self.set_loop_type(None)
60+
61+
def set_loop_type(self, library=None):
62+
if library is None:
63+
self.connection_type = net.DefaultConnection
64+
return
65+
66+
# find module file
67+
manager = pkg_resources.ResourceManager()
68+
libPath = '%(library)s_net/net_%(library)s.py' % {'library': library}
69+
if not manager.resource_exists(__name__, libPath):
70+
raise ValueError('Unknown loop type: %r' % library)
71+
72+
# load the module
73+
modulePath = manager.resource_filename(__name__, libPath)
74+
moduleName = 'net_%s' % library
75+
moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)])
76+
module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc)
77+
78+
# set the connection type
79+
self.connection_type = module.Connection
80+
81+
# cleanup
82+
manager.cleanup_resources()
83+
84+
def connect(self, *args, **kwargs):
85+
return self.make_connection(self.connection_type, *args, **kwargs)

rethinkdb/asyncio_net/net_asyncio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def __init__(self, *args, **kwargs):
9292
Cursor.__init__(self, *args, **kwargs)
9393
self.new_response = asyncio.Future()
9494

95-
@asyncio.coroutine
9695
def __aiter__(self):
9796
return self
9897

rethinkdb/logger.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ def __init__(self, level=logging.INFO):
3535
"""
3636

3737
super(DriverLogger, self).__init__()
38-
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
39-
logging.basicConfig(format=log_format)
4038

41-
self.logger = logging.getLogger()
39+
self.logger = logging.getLogger(__name__)
4240
self.logger.setLevel(level)
4341

4442
self.write_to_console = False
@@ -65,7 +63,7 @@ def _print_message(self, level, message):
6563

6664
def _log(self, level, message, *args, **kwargs):
6765
self._print_message(level, message)
68-
self.logger.log(level, message, args, kwargs)
66+
self.logger.log(level, message, *args, **kwargs)
6967

7068
def debug(self, message):
7169
"""

rethinkdb/net.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
import collections
2020
import errno
21-
import imp
2221
import numbers
23-
import os
2422
import pprint
2523
import socket
2624
import ssl
@@ -48,7 +46,7 @@
4846
from rethinkdb.handshake import HandshakeV1_0
4947
from rethinkdb.logger import default_logger
5048

51-
__all__ = ['connect', 'set_loop_type', 'Connection', 'Cursor', 'DEFAULT_PORT']
49+
__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT', 'DefaultConnection', 'make_connection']
5250

5351

5452
DEFAULT_PORT = 28015
@@ -705,10 +703,11 @@ def __init__(self, *args, **kwargs):
705703
Connection.__init__(self, ConnectionInstance, *args, **kwargs)
706704

707705

708-
connection_type = DefaultConnection
709706

710707

711-
def connect(
708+
709+
def make_connection(
710+
connection_type,
712711
host=None,
713712
port=None,
714713
db=None,
@@ -734,26 +733,3 @@ def connect(
734733

735734
conn = connection_type(host, port, db, auth_key, user, password, timeout, ssl, _handshake_version, **kwargs)
736735
return conn.reconnect(timeout=timeout)
737-
738-
739-
def set_loop_type(library):
740-
global connection_type
741-
import pkg_resources
742-
743-
# find module file
744-
manager = pkg_resources.ResourceManager()
745-
libPath = '%(library)s_net/net_%(library)s.py' % {'library': library}
746-
if not manager.resource_exists(__name__, libPath):
747-
raise ValueError('Unknown loop type: %r' % library)
748-
749-
# load the module
750-
modulePath = manager.resource_filename(__name__, libPath)
751-
moduleName = 'net_%s' % library
752-
moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)])
753-
module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc)
754-
755-
# set the connection type
756-
connection_type = module.Connection
757-
758-
# cleanup
759-
manager.cleanup_resources()

rethinkdb/trio_net/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)