From 21f9476deccf590e18296d8cf00a70c45bc17038 Mon Sep 17 00:00:00 2001 From: Pavel Zinin Date: Thu, 24 Jan 2019 16:42:04 +0300 Subject: [PATCH 1/2] set_loop_type supports reverting back to synchronous code --- rethinkdb/__init__.py | 35 +++++++++++++++++++++++++++++++++++ rethinkdb/net.py | 29 +++-------------------------- 2 files changed, 38 insertions(+), 26 deletions(-) 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() From 787bdc6efde41feaa5f6e10f9b6d7bfad55adf1a Mon Sep 17 00:00:00 2001 From: Adam Grandquist Date: Sun, 10 Feb 2019 19:52:26 -0800 Subject: [PATCH 2/2] Refactor for clarity on imports and method aliasing. --- rethinkdb/__init__.py | 10 ++++------ rethinkdb/net.py | 7 +++---- rethinkdb/utils_common.py | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/rethinkdb/__init__.py b/rethinkdb/__init__.py index 9f2d0a55..1764b91c 100644 --- a/rethinkdb/__init__.py +++ b/rethinkdb/__init__.py @@ -16,6 +16,8 @@ import imp from rethinkdb import errors, version +from rethinkdb import net +import pkg_resources # The builtins here defends against re-importing something obscuring `object`. @@ -40,7 +42,6 @@ def __init__(self): self._import = _import self._index_rebuild = _index_rebuild self._restore = _restore - self._connect = net.connect net.Connection._r = self @@ -50,11 +51,8 @@ def __init__(self): self.set_loop_type(None) - def set_loop_type(self, library): - import pkg_resources - + def set_loop_type(self, library=None): if library is None: - from rethinkdb import net self.connection_type = net.DefaultConnection return @@ -77,4 +75,4 @@ def set_loop_type(self, library): manager.cleanup_resources() def connect(self, *args, **kwargs): - return self._connect(self.connection_type, *args, **kwargs) + return self.make_connection(self.connection_type, *args, **kwargs) diff --git a/rethinkdb/net.py b/rethinkdb/net.py index e9282b01..5a4c8ddc 100644 --- a/rethinkdb/net.py +++ b/rethinkdb/net.py @@ -46,7 +46,7 @@ from rethinkdb.handshake import HandshakeV1_0 from rethinkdb.logger import default_logger -__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT'] +__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT', 'DefaultConnection', 'make_connection'] DEFAULT_PORT = 28015 @@ -706,7 +706,8 @@ def __init__(self, *args, **kwargs): -def connect(connection_type, +def make_connection( + connection_type, host=None, port=None, db=None, @@ -732,5 +733,3 @@ def connect(connection_type, conn = connection_type(host, port, db, auth_key, user, password, timeout, ssl, _handshake_version, **kwargs) return conn.reconnect(timeout=timeout) - - diff --git a/rethinkdb/utils_common.py b/rethinkdb/utils_common.py index 59b2583f..4db6e194 100644 --- a/rethinkdb/utils_common.py +++ b/rethinkdb/utils_common.py @@ -67,7 +67,7 @@ def conn(self, test_connection=True): # cache a new connection if not os.getpid() in self.__local.connCache: - self.__local.connCache[os.getpid()] = net.connect(**self.__connectOptions) + self.__local.connCache[os.getpid()] = net.make_connection(net.DefaultConnection, **self.__connectOptions) # return the connection return self.__local.connCache[os.getpid()]