66from contextlib import contextmanager
77import pymysql as client
88import logging
9+ from getpass import getpass
910
1011from . import config
1112from . import DataJointError
1617logger = logging .getLogger (__name__ )
1718
1819
19- def conn (host = None , user = None , passwd = None , init_fun = None , reset = False ):
20+ def conn (host = None , user = None , password = None , init_fun = None , reset = False ):
2021 """
2122 Returns a persistent connection object to be shared by multiple modules.
2223 If the connection is not yet established or reset=True, a new connection is set up.
@@ -26,18 +27,20 @@ def conn(host=None, user=None, passwd=None, init_fun=None, reset=False):
2627
2728 :param host: hostname
2829 :param user: mysql user
29- :param passwd : mysql password
30+ :param password : mysql password
3031 :param init_fun: initialization function
31- :param reset: whether the connection should be reseted or not
32+ :param reset: whether the connection should be reset or not
3233 """
3334 if not hasattr (conn , 'connection' ) or reset :
3435 host = host if host is not None else config ['database.host' ]
3536 user = user if user is not None else config ['database.user' ]
36- passwd = passwd if passwd is not None else config ['database.password' ]
37- if passwd is None : # pragma: no cover
38- passwd = input ("Please enter database password: " )
37+ password = password if password is not None else config ['database.password' ]
38+ if user is None : # pragma: no cover
39+ user = input ("Please enter DataJoint username: " )
40+ if password is None : # pragma: no cover
41+ password = getpass (prompt = "Please enter DataJoint password: " )
3942 init_fun = init_fun if init_fun is not None else config ['connection.init_function' ]
40- conn .connection = Connection (host , user , passwd , init_fun )
43+ conn .connection = Connection (host , user , password , init_fun )
4144 return conn .connection
4245
4346
@@ -50,19 +53,20 @@ class Connection:
5053
5154 :param host: host name
5255 :param user: user name
53- :param passwd : password
54- :param init_fun: initialization function
56+ :param password : password
57+ :param init_fun: connection initialization function (SQL)
5558 """
5659
57- def __init__ (self , host , user , passwd , init_fun = None ):
60+ def __init__ (self , host , user , password , init_fun = None ):
5861 if ':' in host :
5962 host , port = host .split (':' )
6063 port = int (port )
6164 else :
6265 port = config ['database.port' ]
63- self .conn_info = dict (host = host , port = port , user = user , passwd = passwd )
66+ self .conn_info = dict (host = host , port = port , user = user , passwd = password )
6467 self .init_fun = init_fun
6568 print ("Connecting {user}@{host}:{port}" .format (** self .conn_info ))
69+ self ._conn = None
6670 self .connect ()
6771 if self .is_connected :
6872 logger .info ("Connected {user}@{host}:{port}" .format (** self .conn_info ))
@@ -82,11 +86,9 @@ def __repr__(self):
8286 return "DataJoint connection ({connected}) {user}@{host}:{port}" .format (
8387 connected = connected , ** self .conn_info )
8488
85- def connect (self , init_fun = None ):
89+ def connect (self ):
8690 """
8791 Connects to the database server.
88-
89- :param init_fun: initialization function passed to pymysql
9092 """
9193 self ._conn = client .connect (init_command = self .init_fun , ** self .conn_info )
9294
@@ -198,6 +200,3 @@ def transaction(self):
198200 raise
199201 else :
200202 self .commit_transaction ()
201-
202-
203-
0 commit comments