[GRASS-SVN] r38918 - grass/branches/develbranch_6/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Aug 30 13:37:35 EDT 2009


Author: martinl
Date: 2009-08-30 13:37:35 -0400 (Sun, 30 Aug 2009)
New Revision: 38918

Modified:
   grass/branches/develbranch_6/lib/python/core.py
   grass/branches/develbranch_6/lib/python/db.py
   grass/branches/develbranch_6/lib/python/grasspythonlib.dox
   grass/branches/develbranch_6/lib/python/raster.py
   grass/branches/develbranch_6/lib/python/vector.py
Log:
pythonlib: set_fatal_exit() added (used in wxGUI to call error()
instead of fatal()
	(merge from trunk, r38885)


Modified: grass/branches/develbranch_6/lib/python/core.py
===================================================================
--- grass/branches/develbranch_6/lib/python/core.py	2009-08-30 17:31:07 UTC (rev 38917)
+++ grass/branches/develbranch_6/lib/python/core.py	2009-08-30 17:37:35 UTC (rev 38918)
@@ -50,6 +50,8 @@
 PIPE = subprocess.PIPE
 STDOUT = subprocess.STDOUT
 
+fatal_exit = True # abort on fatal()
+
 def call(*args, **kwargs):
     return Popen(*args, **kwargs).wait()
 
@@ -245,8 +247,6 @@
 
     @param msg message to be displayed
     @param flag flags (given as string)
-
-    @return g.message's exit code
     """
     run_command("g.message", flags = flag, message = msg)
 
@@ -255,8 +255,6 @@
 
     @param msg message to be displayed
     @param debug debug level (0-5)
-
-    @return g.message's exit code
     """
     run_command("g.message", flags = 'd', message = msg, debug = debug)
     
@@ -264,8 +262,6 @@
     """!Display a verbose message using g.message -v
     
     @param msg message to be displayed
-
-    @return g.message's exit code
     """
     message(msg, flag = 'v')
 
@@ -273,39 +269,42 @@
     """!Display an informational message using g.message -i
 
     @param msg message to be displayed
-
-    @return g.message's exit code
     """
     message(msg, flag = 'i')
 
 def warning(msg):
     """!Display a warning message using g.message -w
 
-    @param msg message to be displayed
-
-    @return g.message's exit code
+    @param msg warning message to be displayed
     """
     message(msg, flag = 'w')
 
 def error(msg):
     """!Display an error message using g.message -e
 
-    @param msg message to be displayed
-
-    @return g.message's exit code
+    @param msg error message to be displayed
     """
     message(msg, flag = 'e')
 
 def fatal(msg):
     """!Display an error message using g.message -e, then abort
 
-    @param msg message to be displayed
-
-    @return g.message's exit code
+    @param msg error message to be displayed
     """
     error(msg)
-    sys.exit(1)
+    
+    global fatal_exit
+    if fatal_exit:
+        sys.exit(1)
+    
+def set_fatal_exit(exit = True):
+    """!Set fatal_exit variable
 
+    @param exit True to abort on fatal() otherwise just error message
+    is printed"""
+    global fatal_exit
+    fatal_exit = exit
+    
 # interface to g.parser
 
 def _parse_env():

Modified: grass/branches/develbranch_6/lib/python/db.py
===================================================================
--- grass/branches/develbranch_6/lib/python/db.py	2009-08-30 17:31:07 UTC (rev 38917)
+++ grass/branches/develbranch_6/lib/python/db.py	2009-08-30 17:37:35 UTC (rev 38918)
@@ -7,9 +7,8 @@
 Usage:
 
 @code
-from grass.script import core, db as grass
+from grass.script import db as grass
 
-grass.parser()
 grass.db_describe(table)
 ...
 @endcode
@@ -23,6 +22,8 @@
 @author Martin Landa <landa.martin gmail.com>
 """
 
+import tempfile as pytempfile # conflict with core.tempfile
+
 from core import *
 
 # run "db.describe -c ..." and parse output
@@ -46,7 +47,8 @@
     """
     s = read_command('db.describe', flags = 'c', table = table, **args)
     if not s:
-	return None
+	grass.fatal(_("Unable to describe table <%s>") % table)
+    
     cols = []
     result = {}
     for l in s.splitlines():
@@ -61,6 +63,7 @@
 	else:
 	    result[key] = f[1:]
     result['cols'] = cols
+    
     return result
 
 # run "db.connect -p" and parse output
@@ -79,3 +82,30 @@
     s = read_command('db.connect', flags = 'p')
     return parse_key_val(s, sep = ':')
 
+def db_select(table, sql, file = False, **args):
+    """!Perform SQL select statement
+
+    @param table table name
+    @param sql   SQL select statement (string or file)
+    @param file  True if sql is filename
+    @param args  see db.select arguments
+    """
+    ofile = pytempfile.NamedTemporaryFile(mode = 'w+b')
+    if not file:
+        ret = run_command('db.select', quiet = True,
+                          flags = 'c',
+                          table = table,
+                          sql = sql,
+                          output = ofile.name)
+    else: # -> sql is file
+        ret = run_command('db.select', quiet = True,
+                          flags = 'c',
+                          table = table,
+                          input = sql,
+                          output = ofile.name)
+    
+    if ret != 0:
+        fatal(_("Fetching data from table <%s> failed") % table)
+        
+    return ofile.readlines()
+

Modified: grass/branches/develbranch_6/lib/python/grasspythonlib.dox
===================================================================
--- grass/branches/develbranch_6/lib/python/grasspythonlib.dox	2009-08-30 17:31:07 UTC (rev 38917)
+++ grass/branches/develbranch_6/lib/python/grasspythonlib.dox	2009-08-30 17:37:35 UTC (rev 38918)
@@ -11,19 +11,19 @@
 
 See code in:
 
- - core.py
- - db.py
- - raster.py
- - vector.py
+- core.py
+- db.py
+- raster.py
+- vector.py
 
-<h2>Table of content</h2>
+<b>Table of content</b>
 
- - \subpage scripting
- - \subpage modules
-  - \subpage core
-  - \subpage db
-  - \subpage raster
-  - \subpage vector
+- \subpage scripting
+- \subpage modules
+ - \subpage core
+ - \subpage db
+ - \subpage raster
+ - \subpage vector
 
 \section scripting GRASS scripting tasks for Python provided by "grass.script"
 
@@ -201,6 +201,8 @@
 
  - db_describe()
 
+ - db_select()
+
 \section raster Raster
 
 Interface for <tt>r.*</tt> modules.

Modified: grass/branches/develbranch_6/lib/python/raster.py
===================================================================
--- grass/branches/develbranch_6/lib/python/raster.py	2009-08-30 17:31:07 UTC (rev 38917)
+++ grass/branches/develbranch_6/lib/python/raster.py	2009-08-30 17:37:35 UTC (rev 38918)
@@ -7,9 +7,8 @@
 Usage:
 
 @code
-from grass.script import core, raster as grass
+from grass.script import raster as grass
 
-grass.parser()
 grass.raster_history(map)
 ...
 @endcode

Modified: grass/branches/develbranch_6/lib/python/vector.py
===================================================================
--- grass/branches/develbranch_6/lib/python/vector.py	2009-08-30 17:31:07 UTC (rev 38917)
+++ grass/branches/develbranch_6/lib/python/vector.py	2009-08-30 17:37:35 UTC (rev 38918)
@@ -7,9 +7,8 @@
 Usage:
 
 @code
-from grass.script import core, vector as grass
+from grass.script import vector as grass
 
-grass.parser()
 grass.vector_db(map)
 ...
 @endcode



More information about the grass-commit mailing list