Index: GConnections.py =================================================================== RCS file: /cvsroot/gnue/gnue/common/src/GConnections.py,v retrieving revision 1.35 diff -u -r1.35 GConnections.py --- GConnections.py 3 May 2002 16:46:32 -0000 1.35 +++ GConnections.py 28 May 2002 21:15:05 -0000 @@ -31,7 +31,7 @@ # from ConfigParser import * -import sys, string, copy +import sys, string, copy, netrc import GDebug import GDataObjects import GLoginHandler @@ -209,51 +209,88 @@ GDebug.printMesg(5,'Reusing data connection to %s' % connection_name) else: - attempts = 4 - - GDebug.printMesg(5,'Getting new data connection to %s' % connection_name) - - errortext = None - while attempts: - - try: - - # Ask the UI to prompt for our login data - loginData = self._loginHandler.getLogin( - [connection_name, self.getConnectionParameter(connection_name,'comment',''), - dataObject.getLoginFields()], errortext) - - # Add the parameters from the Connections Definition File - loginData.update(self.getConnectionParameters(connection_name)) - - # Ask the data object to connect to the database - dataObject.connect(loginData) + + # Get the parameters from the Connections Definition File + loginData = self.getConnectionParameters(connection_name) + try: + # load the user's netrc file + netrcData = netrc.netrc().authenticators("'gnue://%s/%s/%s'" % + ( loginData['host'], + loginData['provider'], + loginData['dbname'], + ) + ) + GDebug.printMesg(5, 'Read the user\'s .netrc file') + except IOError: + pass + + if (netrcData): + loginData['username'] = netrcData[0][1:-1] + loginData['password'] = netrcData[2][1:-1] + GDebug.printMesg(5, 'Found useful stuff for connection %s in the user\'s .netrc file' % connection_name) + + if (loginData.has_key('username')): + loginData['_username'] = loginData['username'] + del loginData['username'] + + if (loginData.has_key('password')): + loginData['_password'] = loginData['password'] + del loginData['password'] + + loginData.setdefault('_username') + loginData.setdefault('_password') + + try: - # Save the newly opened connection for future datasources - self._openConnections[connection_name] = \ - dataObject.getDataConnection() + dataObject.connect(loginData) + GDebug.printMesg(5, 'I had enough information to connect to %s without asking the user' % connection_name) + # Save the newly opened connection for future datasources + self._openConnections[connection_name] = dataObject.getDataConnection() + + except GDataObjects.LoginError: + attempts = 4 + + GDebug.printMesg(5,'Getting new data connection to %s' % connection_name) + + errortext = None + while attempts: + + try: + + # Ask the UI to prompt for our login data + loginData.update(self._loginHandler.getLogin( + [connection_name, + self.getConnectionParameter(connection_name,'comment',''), + dataObject.getLoginFields()], errortext)) + + # Ask the data object to connect to the database + dataObject.connect(loginData) + + # Save the newly opened connection for future datasources + self._openConnections[connection_name] = \ + dataObject.getDataConnection() - # We're done! - attempts = 0 - self._loginHandler.destroyLoginDialog() + # We're done! + attempts = 0 + self._loginHandler.destroyLoginDialog() - except GDataObjects.LoginError, error: - # Oops, they must have entered an invalid user/password. - # Those silly users. - # user: Hey! Who are you calling silly?!!! - attempts = attempts - 1 - errortext = string.replace("%s" % error,'\n','') + except GDataObjects.LoginError, error: + # Oops, they must have entered an invalid user/password. + # Those silly users. + # user: Hey! Who are you calling silly?!!! + attempts = attempts - 1 + errortext = string.replace("%s" % error,'\n','') + + if not attempts: + # Four times is plenty... + self._loginHandler.destroyLoginDialog() + raise GDataObjects.LoginError, \ + _("Unable to log in after 4 attempts.\n\nError: %s") % error - if not attempts: - # Four times is plenty... + except GLoginHandler.UserCanceledLogin: + # Guess they changed their minds. Treat as a login error. self._loginHandler.destroyLoginDialog() - raise GDataObjects.LoginError, \ - _("Unable to log in after 4 attempts.\n\nError: %s") % error - - except GLoginHandler.UserCanceledLogin: - # Guess they changed their minds. Treat as a login error. - self._loginHandler.destroyLoginDialog() - raise GDataObjects.LoginError, _("User canceled the login request.") + raise GDataObjects.LoginError, _("User canceled the login request.")