[GRASS-SVN] r51930 - in grass/trunk/lib: gis python

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jun 2 03:16:38 PDT 2012


Author: martinl
Date: 2012-06-02 03:16:38 -0700 (Sat, 02 Jun 2012)
New Revision: 51930

Modified:
   grass/trunk/lib/gis/parser.c
   grass/trunk/lib/python/db.py
Log:
libgis: fix parser when doing overwrite check for new files


Modified: grass/trunk/lib/gis/parser.c
===================================================================
--- grass/trunk/lib/gis/parser.c	2012-06-02 09:15:40 UTC (rev 51929)
+++ grass/trunk/lib/gis/parser.c	2012-06-02 10:16:38 UTC (rev 51930)
@@ -1317,14 +1317,14 @@
 		
 		for (i = 0; opt->answers[i]; i++) {
 		    found = FALSE;
-		    if (strcmp(element, "file") == 0 &&
-			access(opt->answers[i], F_OK) == 0) {
-			found = TRUE;
+		    if (strcmp(element, "file") == 0) {
+                        if (access(opt->answers[i], F_OK) == 0)
+                            found = TRUE;
 		    }
-		    if (G_find_file(element, opt->answers[i], G_mapset())) {
+                    else if (G_find_file(element, opt->answers[i], G_mapset())) {
 			found = TRUE;
 		    }
-		    
+                    
 		    if (found && strcmp(element, "vector") == 0 &&
 			(G_find_file("", "OGR", G_mapset()) ||
 			 G_find_file("", "PG", G_mapset())))

Modified: grass/trunk/lib/python/db.py
===================================================================
--- grass/trunk/lib/python/db.py	2012-06-02 09:15:40 UTC (rev 51929)
+++ grass/trunk/lib/python/db.py	2012-06-02 10:16:38 UTC (rev 51930)
@@ -13,7 +13,7 @@
 ...
 @endcode
 
-(C) 2008-2009 by the GRASS Development Team
+(C) 2008-2009, 2012 by the GRASS Development Team
 This program is free software under the GNU General Public
 License (>=v2). Read the file COPYING that comes with GRASS
 for details.
@@ -45,21 +45,21 @@
     """
     s = read_command('db.describe', flags = 'c', table = table, **args)
     if not s:
-	fatal(_("Unable to describe table <%s>") % table)
+        fatal(_("Unable to describe table <%s>") % table)
     
     cols = []
     result = {}
     for l in s.splitlines():
-	f = l.split(':')
-	key = f[0]
-	f[1] = f[1].lstrip(' ')
-	if key.startswith('Column '):
-	    n = int(key.split(' ')[1])
-	    cols.insert(n, f[1:])
-	elif key in ['ncols', 'nrows']:
-	    result[key] = int(f[1])
-	else:
-	    result[key] = f[1:]
+        f = l.split(':')
+        key = f[0]
+        f[1] = f[1].lstrip(' ')
+        if key.startswith('Column '):
+            n = int(key.split(' ')[1])
+            cols.insert(n, f[1:])
+        elif key in ['ncols', 'nrows']:
+            result[key] = int(f[1])
+        else:
+            result[key] = f[1:]
     result['cols'] = cols
     
     return result
@@ -76,9 +76,9 @@
     """
     result = run_command('db.describe', flags = 'c', table = table, **args)
     if result == 0:
-	    return True
+            return True
     else:
-	    return False
+            return False
 
 def db_connection():
     """!Return the current database connection parameters
@@ -94,33 +94,41 @@
     s = read_command('db.connect', flags = 'p')
     return parse_key_val(s, sep = ':')
 
-def db_select(table, sql, file = False, **args):
+def db_select(sql = None, filename = None, table = None, **args):
     """!Perform SQL select statement
 
-    @param table table name
-    @param sql   SQL select statement (string or file)
-    @param file  True if sql is filename
+    Note: one of <em>sql</em>, <em>filename</em>, or <em>table</em>
+    must be provided.
+
+    SQL statements:
+
+    \code
+    \endcode
+    
+    @param sql SQL statement to perform (or None)
+    @param filename name of file with SQL statements (or None)
+    @param table name of table to query (or None)
     @param args  see db.select arguments
     """
     fname = tempfile(create = False)
-    if not file:
-        ret = run_command('db.select', quiet = True,
-                          flags = 'c',
-                          table = table,
-                          sql = sql,
-                          output = fname,
-			  **args)
-    else: # -> sql is file
-        ret = run_command('db.select', quiet = True,
-                          flags = 'c',
-                          table = table,
-                          input = sql,
-                          output = fname,
-			  **args)
+    if sql:
+        args['sql'] = sql
+    elif filename:
+        args['input'] = filename
+    elif table:
+        args['table'] = table
+    else:
+        fatal(_("Programmer error: '%s', '%s', or '%s' must be provided") %
+              'sql', 'filename', 'table')
+        
+    ret = run_command('db.select', quiet = True,
+                      flags = 'c',
+                      output = fname,
+                      **args)
     
     if ret != 0:
-        fatal(_("Fetching data from table <%s> failed") % table)
-
+        fatal(_("Fetching data failed"))
+    
     ofile = open(fname)
     result = map(lambda x: x.rstrip(os.linesep), ofile.readlines())
     ofile.close()



More information about the grass-commit mailing list