Skip to content
Closed
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
35 changes: 35 additions & 0 deletions rethinkdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
29 changes: 3 additions & 26 deletions rethinkdb/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import collections
import errno
import imp
import numbers
import os
import pprint
import socket
import ssl
Expand Down Expand Up @@ -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']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is abusing __all__ because of the setattr happening in RethinkDB init. Otherwise this is going to override the connect method defined in the class on instance instantiations. Without connect here Python rules implicitly say it's a private method, then we expect it above. I'm not a fan of the confusion in all that. Renaming to make_connection or changing it to a class would solve this.

__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT']


DEFAULT_PORT = 28015
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()