Skip to content

Commit d9b8cdf

Browse files
Adam Grandquistgabor-boros
authored andcommitted
Refactor set loop type: closes #84 (#86)
* set_loop_type supports reverting back to synchronous code * Refactor for clarity on imports and method aliasing.
1 parent d86786f commit d9b8cdf

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

rethinkdb/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
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+
16+
import imp
1417

1518
from rethinkdb import errors, version
19+
from rethinkdb import net
20+
import pkg_resources
1621

1722

1823
# The builtins here defends against re-importing something obscuring `object`.
@@ -43,3 +48,31 @@ def __init__(self):
4348
for module in (net, query, ast, errors):
4449
for function_name in module.__all__:
4550
setattr(self, function_name, getattr(module, function_name))
51+
52+
self.set_loop_type(None)
53+
54+
def set_loop_type(self, library=None):
55+
if library is None:
56+
self.connection_type = net.DefaultConnection
57+
return
58+
59+
# find module file
60+
manager = pkg_resources.ResourceManager()
61+
libPath = '%(library)s_net/net_%(library)s.py' % {'library': library}
62+
if not manager.resource_exists(__name__, libPath):
63+
raise ValueError('Unknown loop type: %r' % library)
64+
65+
# load the module
66+
modulePath = manager.resource_filename(__name__, libPath)
67+
moduleName = 'net_%s' % library
68+
moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)])
69+
module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc)
70+
71+
# set the connection type
72+
self.connection_type = module.Connection
73+
74+
# cleanup
75+
manager.cleanup_resources()
76+
77+
def connect(self, *args, **kwargs):
78+
return self.make_connection(self.connection_type, *args, **kwargs)

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/utils_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def conn(self, test_connection=True):
6767

6868
# cache a new connection
6969
if not os.getpid() in self.__local.connCache:
70-
self.__local.connCache[os.getpid()] = net.connect(**self.__connectOptions)
70+
self.__local.connCache[os.getpid()] = net.make_connection(net.DefaultConnection, **self.__connectOptions)
7171

7272
# return the connection
7373
return self.__local.connCache[os.getpid()]

0 commit comments

Comments
 (0)