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
1518from 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 )
0 commit comments