diff --git a/rethinkdb/__init__.py b/rethinkdb/__init__.py index ab6b356f..9f2d0a55 100644 --- a/rethinkdb/__init__.py +++ b/rethinkdb/__init__.py @@ -11,6 +11,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os + +import imp from rethinkdb import errors, version @@ -37,9 +40,41 @@ def __init__(self): self._import = _import self._index_rebuild = _index_rebuild self._restore = _restore + self._connect = net.connect net.Connection._r = self for module in (net, query, ast, errors): for function_name in module.__all__: setattr(self, function_name, getattr(module, function_name)) + + self.set_loop_type(None) + + def set_loop_type(self, library): + import pkg_resources + + if library is None: + from rethinkdb import net + self.connection_type = net.DefaultConnection + return + + # find module file + manager = pkg_resources.ResourceManager() + libPath = '%(library)s_net/net_%(library)s.py' % {'library': library} + if not manager.resource_exists(__name__, libPath): + raise ValueError('Unknown loop type: %r' % library) + + # load the module + modulePath = manager.resource_filename(__name__, libPath) + moduleName = 'net_%s' % library + moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)]) + module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc) + + # set the connection type + self.connection_type = module.Connection + + # cleanup + manager.cleanup_resources() + + def connect(self, *args, **kwargs): + return self._connect(self.connection_type, *args, **kwargs) diff --git a/rethinkdb/net.py b/rethinkdb/net.py index 4ffdaf7e..e9282b01 100644 --- a/rethinkdb/net.py +++ b/rethinkdb/net.py @@ -18,9 +18,7 @@ import collections import errno -import imp import numbers -import os import pprint import socket import ssl @@ -48,7 +46,7 @@ from rethinkdb.handshake import HandshakeV1_0 from rethinkdb.logger import default_logger -__all__ = ['connect', 'set_loop_type', 'Connection', 'Cursor', 'DEFAULT_PORT'] +__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT'] DEFAULT_PORT = 28015 @@ -705,10 +703,10 @@ def __init__(self, *args, **kwargs): Connection.__init__(self, ConnectionInstance, *args, **kwargs) -connection_type = DefaultConnection -def connect( + +def connect(connection_type, host=None, port=None, db=None, @@ -736,24 +734,3 @@ def connect( return conn.reconnect(timeout=timeout) -def set_loop_type(library): - global connection_type - import pkg_resources - - # find module file - manager = pkg_resources.ResourceManager() - libPath = '%(library)s_net/net_%(library)s.py' % {'library': library} - if not manager.resource_exists(__name__, libPath): - raise ValueError('Unknown loop type: %r' % library) - - # load the module - modulePath = manager.resource_filename(__name__, libPath) - moduleName = 'net_%s' % library - moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)]) - module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc) - - # set the connection type - connection_type = module.Connection - - # cleanup - manager.cleanup_resources()