GRASS Programmer's Manual
6.4.2(2012)
|
00001 """!@package grass.script.db 00002 00003 @brief GRASS Python scripting module (database functions) 00004 00005 Database related functions to be used in Python scripts. 00006 00007 Usage: 00008 00009 @code 00010 from grass.script import db as grass 00011 00012 grass.db_describe(table) 00013 ... 00014 @endcode 00015 00016 (C) 2008-2009 by the GRASS Development Team 00017 This program is free software under the GNU General Public 00018 License (>=v2). Read the file COPYING that comes with GRASS 00019 for details. 00020 00021 @author Glynn Clements 00022 @author Martin Landa <landa.martin gmail.com> 00023 """ 00024 00025 import tempfile as pytempfile # conflict with core.tempfile 00026 00027 from core import * 00028 00029 # i18N 00030 import gettext 00031 gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True) 00032 00033 def db_describe(table, **args): 00034 """!Return the list of columns for a database table 00035 (interface to `db.describe -c'). Example: 00036 00037 \code 00038 >>> grass.db_describe('lakes') 00039 {'nrows': 15279, 'cols': [['cat', 'INTEGER', '11'], ['AREA', 'DOUBLE PRECISION', '20'], 00040 ['PERIMETER', 'DOUBLE PRECISION', '20'], ['FULL_HYDRO', 'DOUBLE PRECISION', '20'], 00041 ['FULL_HYDR2', 'DOUBLE PRECISION', '20'], ['FTYPE', 'CHARACTER', '24'], 00042 ['FCODE', 'INTEGER', '11'], ['NAME', 'CHARACTER', '99']], 'ncols': 8} 00043 \endcode 00044 00045 @param table table name 00046 @param args 00047 00048 @return parsed module output 00049 """ 00050 s = read_command('db.describe', flags = 'c', table = table, **args) 00051 if not s: 00052 fatal(_("Unable to describe table <%s>") % table) 00053 00054 cols = [] 00055 result = {} 00056 for l in s.splitlines(): 00057 f = l.split(':') 00058 key = f[0] 00059 f[1] = f[1].lstrip(' ') 00060 if key.startswith('Column '): 00061 n = int(key.split(' ')[1]) 00062 cols.insert(n, f[1:]) 00063 elif key in ['ncols', 'nrows']: 00064 result[key] = int(f[1]) 00065 else: 00066 result[key] = f[1:] 00067 result['cols'] = cols 00068 00069 return result 00070 00071 # run "db.connect -p" and parse output 00072 00073 def db_connection(): 00074 """!Return the current database connection parameters 00075 (interface to `db.connect -p'). Example: 00076 00077 \code 00078 >>> grass.db_connection() 00079 {'group': 'x', 'schema': '', 'driver': 'dbf', 'database': '$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/'} 00080 \endcode 00081 00082 @return parsed output of db.connect 00083 """ 00084 s = read_command('db.connect', flags = 'p') 00085 return parse_key_val(s, sep = ':') 00086 00087 def db_select(table, sql, file = False, **args): 00088 """!Perform SQL select statement 00089 00090 @param table table name 00091 @param sql SQL select statement (string or file) 00092 @param file True if sql is filename 00093 @param args see db.select arguments 00094 """ 00095 fname = tempfile() 00096 if not file: 00097 ret = run_command('db.select', quiet = True, 00098 flags = 'c', 00099 table = table, 00100 sql = sql, 00101 output = fname, 00102 **args) 00103 else: # -> sql is file 00104 ret = run_command('db.select', quiet = True, 00105 flags = 'c', 00106 table = table, 00107 input = sql, 00108 output = fname, 00109 **args) 00110 00111 if ret != 0: 00112 fatal(_("Fetching data from table <%s> failed") % table) 00113 00114 ofile = open(fname) 00115 result = map(lambda x: x.rstrip(os.linesep), ofile.readlines()) 00116 ofile.close() 00117 try_remove(fname) 00118 00119 return result