commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5009 - in trunk/gnue-common/src: apps datasources datasources/drivers/D


From: jcater
Subject: r5009 - in trunk/gnue-common/src: apps datasources datasources/drivers/DBSIG2 datasources/drivers/gadfly/gadfly datasources/drivers/interbase/interbase datasources/drivers/mysql/mysql datasources/drivers/oracle/Base datasources/drivers/postgresql/Base datasources/drivers/sqlite/sqlite datasources/drivers/sqlrelay/sqlrelay
Date: Fri, 19 Dec 2003 17:00:00 -0600 (CST)

Author: jcater
Date: 2003-12-19 16:59:59 -0600 (Fri, 19 Dec 2003)
New Revision: 5009

Modified:
   trunk/gnue-common/src/apps/GBaseApp.py
   trunk/gnue-common/src/datasources/GConnections.py
   trunk/gnue-common/src/datasources/GLoginHandler.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
   trunk/gnue-common/src/datasources/drivers/gadfly/gadfly/Connection.py
   trunk/gnue-common/src/datasources/drivers/interbase/interbase/Connection.py
   trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py
   trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py
   trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py
   trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py
   trunk/gnue-common/src/datasources/drivers/sqlrelay/sqlrelay/Connection.py
Log:
added a basic login handler; moved the connection extension 'sql' into the 
DBSIG2 base class


Modified: trunk/gnue-common/src/apps/GBaseApp.py
===================================================================
--- trunk/gnue-common/src/apps/GBaseApp.py      2003-12-19 21:33:36 UTC (rev 
5008)
+++ trunk/gnue-common/src/apps/GBaseApp.py      2003-12-19 22:59:59 UTC (rev 
5009)
@@ -62,6 +62,8 @@
   SUMMARY = "A brief summary of the program goes here."
   COMMAND = "app"
   USAGE = "[options]"
+  USE_CONNECTIONS = 1       # Set to 1 if the program uses dbdrivers
+  USE_DATABASE_OPTIONS = 0  # Also implies USE_CONNECTIONS = 1
 
 
   # More options, but won't be changed unless
@@ -84,7 +86,6 @@
 
 
 
-
   def __init__(self, connections=None, application=None, defaults=None):
 
 
@@ -103,16 +104,6 @@
          [ 'debug_file',None,'debug-file',1,None, "file",
            _('Sends all debugging messages to a specified file ') + \
            _('(e.g., "--debug-file trace.log" sends all output to 
"trace.log")') ],
-         [ 'connections', None, 'connections', 1, None, "loc",
-           _('Specifies the location of the connection definition file. ') + \
-           _('<loc> may specify a file name ') + \
-           '(/usr/local/gnue/etc/connections.conf),'  + \
-           _('or a URL location ') + \
-           '(http://localhost/connections.conf).' + \
-           _('If this option is not specified, the environent variable ') + \
-           _('GNUE_CONNECTIONS is checked.') + \
-           _('If neither of them is set, "%s" is used as a default.') %
-             os.path.join (paths.config, "connections.conf")],
          [ 'help', None, 'help', 0, None, None,
            _('Displays this help screen.') ],
          [ 'configuration_options', None, 'configuration-options', 0, None, 
None,
@@ -126,6 +117,34 @@
            _("Run the app inside Python's built-in debugger ")],
     ]
 
+    if self.USE_DATABASE_OPTIONS:
+      self.USE_CONNECTIONS = 1
+      self._base_options += [
+        [ 'username', 'u', 'username', 1, '', 'name',
+           _('Username used to log into the database.  Note that if '
+             'specified, this will be used for all databases.  If not '
+             'supplied, the program will prompt for username.')],
+        [ 'password', None, 'password', 1, '', 'passwd',
+           _('Password used to log into the database.  Note that if '
+             'specified, this will be used for all databases.  If not '
+             'supplied, the program will prompt for password if needed.'
+             '\nNOTE: SUPPLYING A PASSWORD VIA THE COMMAND LINE MAY BE '
+             'CONSIDERED A SECURITY RISK AND IS NOT RECOMMENDED.)') ]
+      ]
+
+    if self.USE_CONNECTIONS:
+      self._base_options += [
+        [ 'connections', None, 'connections', 1, None, "loc",
+           _('Specifies the location of the connection definition file. ') + \
+           _('<loc> may specify a file name ') + \
+           '(/usr/local/gnue/etc/connections.conf),'  + \
+           _('or a URL location ') + \
+           '(http://localhost/connections.conf).' + \
+           _('If this option is not specified, the environent variable ') + \
+           _('GNUE_CONNECTIONS is checked.') + \
+           _('If neither of them is set, "%s" is used as a default.') %
+             os.path.join (paths.config, "connections.conf")] ]
+
     # Python version check
     try:
       if sys.hexversion < 0x02000000:
@@ -141,6 +160,7 @@
     shortoptions = ""
     longoptions = []
     lookup = {}
+
     for optionset in [self._base_options, self.COMMAND_OPTIONS]:
       for option in optionset:
         self.OPTIONS[option[0]] = option[4]
@@ -186,7 +206,7 @@
 
     # Setup debugging
     # Should we run in debugger?
-    if self.OPTIONS['debugger']:
+    elif self.OPTIONS['debugger']:
       self.run = self._debugger
 
     try:
@@ -236,9 +256,9 @@
       textEncoding = gConfig('textEncoding')
       if not catalog.charset():
         raise InvalidFormatError, "Translation file is badly formatted for 
unicode operation"
-      
+
       def translate(msg):
-        global catalog  
+        global catalog
         return catalog.ugettext(msg).encode(textEncoding)
     
       import __builtin__  
@@ -255,7 +275,16 @@
     if connections != None:
       GDebug.printMesg(1,"Reusing connections instance")
       self.connections = connections
-    else:
+    elif self.USE_CONNECTIONS:
+
+      # Check for default username/password
+      lhOptions = {}
+      if self.USE_DATABASE_OPTIONS:
+        if self.OPTIONS['username']:
+          lhOptions['_username'] = self.OPTIONS['username']
+        if self.OPTIONS['password']:
+          lhOptions['_password'] = self.OPTIONS['password']
+
       if self.OPTIONS['connections']:
         self.connections_file = self.OPTIONS['connections']
       elif os.environ.has_key('GNUE_CONNECTIONS'):
@@ -270,7 +299,7 @@
       GDebug.printMesg(1, 'Connection Definition: "%s"' % 
self.connections_file)
 
       try:
-        self.connections = GConnections.GConnections(self.connections_file)
+        self.connections = GConnections.GConnections(self.connections_file, 
loginOptions=lhOptions)
       except GConnections.InvalidFormatError, msg:
         self.handleStartupError(
             _('Unable to load the connections definition file.\n') \

Modified: trunk/gnue-common/src/datasources/GConnections.py
===================================================================
--- trunk/gnue-common/src/datasources/GConnections.py   2003-12-19 21:33:36 UTC 
(rev 5008)
+++ trunk/gnue-common/src/datasources/GConnections.py   2003-12-19 22:59:59 UTC 
(rev 5009)
@@ -37,6 +37,8 @@
 from gnue.common.datasources import GLoginHandler
 from gnue.common.datasources.drivers import DRIVERS as ALLDRIVERS
 from gnue.common.utils.FileUtils import openResource, dyn_import
+import GLoginHandler
+
 class Error(StandardError):
   # Base error
   pass
@@ -61,9 +63,10 @@
 
 class GConnections:
 
-  def __init__(self, location, loginHandler=None):
+  def __init__(self, location, loginHandler=None, loginOptions={}):
 
     self._loginHandler = loginHandler
+    self._loginOptions = loginOptions
     self._parser = ConfigParser()
     self._location = location
     self._authenticatedUsers = {}
@@ -119,6 +122,10 @@
 
   def setLoginHandler(self, loginHandler):
     self._loginHandler = loginHandler
+    try:
+      loginHandler.defaults.update(self._loginOptions)
+    except AttributeError:
+      print "WARNING: Login handler doesn't support 'default' login info."
 
 
   def hasConnectionParameters(self, connection_name):
@@ -294,6 +301,9 @@
     connection_name = connection.name
     connection_base = connection_name.split(':')[0]
 
+    if not self._loginHandler:
+      self.setLoginHandler(GLoginHandler.BasicLoginHandler())
+
     try:
       connected = connection.__connected
     except AttributeError:

Modified: trunk/gnue-common/src/datasources/GLoginHandler.py
===================================================================
--- trunk/gnue-common/src/datasources/GLoginHandler.py  2003-12-19 21:33:36 UTC 
(rev 5008)
+++ trunk/gnue-common/src/datasources/GLoginHandler.py  2003-12-19 22:59:59 UTC 
(rev 5009)
@@ -58,3 +58,41 @@
   # Called when the app no longer needs the login handler
   def destroyLoginDialog(self):
     pass
+
+
+#
+# This is a basic solution to getting a login
+#
+try:
+  import getpass
+except:
+  getpass = None
+
+class BasicLoginHandler(LoginHandler):
+
+  defaults = {'_username': None, '_password': None}
+
+  def getLogin(self, loginData, error):
+    try:
+      print "*"*60
+      if len(loginData[1]):
+        print 'Attempting to log into "%s" (%s):' % (loginData[1], 
loginData[0])
+      else:
+        print 'Attempting to log into %s:' % (loginData[0])
+      print
+      val = {}
+      for prompt in loginData[2]:
+        if not (self.defaults.has_key(prompt[0]) and 
self.defaults.get(prompt[0],None)):
+          if prompt[2] and getpass:
+            val[prompt[0]] = getpass.getpass("  %s: " % prompt[1])
+          else:
+            val[prompt[0]] = raw_input("  %s: " % prompt[1])
+      print
+      print "*"*60
+      return val
+    except KeyboardInterrupt:
+      raise GLoginHandler.UserCanceledLogin
+
+
+  def destroyLoginDialog(self):
+    pass

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2003-12-19 22:59:59 UTC (rev 5009)
@@ -71,3 +71,23 @@
   # this is usually not necessary (MySQL is one of few DBs that must force)
   def _beginTransaction(self):
     pass
+
+
+  #
+  # Extensions
+  #
+
+  # Run the SQL statement 'statement'
+  def sql(self, statement):
+    cursor = self.native.cursor()
+    try:
+      cursor.execute(statement)
+    except:
+      cursor.close()
+      raise
+    try:
+      rs = cursor.fetchall()
+      cursor.close()
+      return rs
+    except:
+      return []

Modified: trunk/gnue-common/src/datasources/drivers/gadfly/gadfly/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/gadfly/gadfly/Connection.py       
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/gadfly/gadfly/Connection.py       
2003-12-19 22:59:59 UTC (rev 5009)
@@ -109,7 +109,3 @@
 #  def getSequence(self, name):
 #    pass
 
-  # Run the SQL statement 'statement'
-#  def sql(self, statement):
-#    pass
-

Modified: 
trunk/gnue-common/src/datasources/drivers/interbase/interbase/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/interbase/interbase/Connection.py 
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/interbase/interbase/Connection.py 
2003-12-19 22:59:59 UTC (rev 5009)
@@ -94,16 +94,6 @@
   def getSequence(self, name):
     return self.__singleQuery("select gen_id(%s,1) from rdb$database" % name)
 
-  # Run the SQL statement 'statement'
-  def sql(self, statement):
-    cursor = self.native.cursor()
-    try:
-      cursor.execute(statement)
-      cursor.close()
-    except:
-      cursor.close()
-      raise
-
   # Used internally
   def __singleQuery(self, statement):
     cursor = self.native.cursor()

Modified: trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py 
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/mysql/mysql/Connection.py 
2003-12-19 22:59:59 UTC (rev 5009)
@@ -105,16 +105,6 @@
   #def getSequence(self, name):
   #  raise "Not supported"
 
-  # Run the SQL statement 'statement'
-  def sql(self, statement):
-    cursor = self.native.cursor()
-    try:
-      cursor.execute(statement)
-      cursor.close()
-    except:
-      cursor.close()
-      raise
-
   # Used internally
   def __singleQuery(self, statement):
     cursor = self.native.cursor()

Modified: trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py 
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/oracle/Base/Connection.py 
2003-12-19 22:59:59 UTC (rev 5009)
@@ -88,23 +88,9 @@
   def getSequence(self, name):
     return self.__singleQuery("select %s.nextval from dual" % name)
 
-  # Run the SQL statement 'statement'
-  def sql(self, statement):
-    cursor = self.__connection.cursor()
-    try:
-      cursor.execute(statement)
-      cursor.close()
-    except:
-      cursor.close()
-      raise
-    try:
-      return cursor.fetchall()
-    except:
-      return None
-
   # Used internally
   def __singleQuery(self, statement):
-    cursor = self.__connection.cursor()
+    cursor = self.native.cursor()
     try:
       cursor.execute(statement)
       rv = cursor.fetchone()

Modified: 
trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py     
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/postgresql/Base/Connection.py     
2003-12-19 22:59:59 UTC (rev 5009)
@@ -124,16 +124,6 @@
   def getSequence(self, name):
     return self.__singleQuery("select nextval('%s')" % name)
 
-  # Run the SQL statement 'statement'
-  def sql(self, statement):
-    cursor = self.native.cursor()
-    try:
-      cursor.execute(statement)
-      cursor.close()
-    except:
-      cursor.close()
-      raise
-
   # Used internally
   def __singleQuery(self, statement):
     cursor = self.native.cursor()

Modified: trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py       
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py       
2003-12-19 22:59:59 UTC (rev 5009)
@@ -76,5 +76,3 @@
   # Each list item is another list of ["field label", isPassword?]
   def getLoginFields(self):
     return []
-
-

Modified: 
trunk/gnue-common/src/datasources/drivers/sqlrelay/sqlrelay/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sqlrelay/sqlrelay/Connection.py   
2003-12-19 21:33:36 UTC (rev 5008)
+++ trunk/gnue-common/src/datasources/drivers/sqlrelay/sqlrelay/Connection.py   
2003-12-19 22:59:59 UTC (rev 5009)
@@ -87,4 +87,3 @@
     except self._DatabaseError, value:
       raise GDataObjects.LoginError, value
 
-





reply via email to

[Prev in Thread] Current Thread [Next in Thread]