[GRASS-SVN] r34952 - in grass/trunk: gui/wxpython/gui_modules
lib/python scripts/db.droptable scripts/v.db.addcol
scripts/v.db.addtable scripts/v.db.dropcol
scripts/v.db.droptable scripts/v.db.join
scripts/v.db.reconnect.all scripts/v.db.renamecol
scripts/v.db.update scripts/v.dissolve scripts/v.rast.stats
scripts/v.report scripts/v.to.3d
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Dec 20 07:59:36 EST 2008
Author: martinl
Date: 2008-12-20 07:59:36 -0500 (Sat, 20 Dec 2008)
New Revision: 34952
Modified:
grass/trunk/gui/wxpython/gui_modules/gselect.py
grass/trunk/lib/python/grass.py
grass/trunk/scripts/db.droptable/db.droptable.py
grass/trunk/scripts/v.db.addcol/v.db.addcol.py
grass/trunk/scripts/v.db.addtable/v.db.addtable.py
grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py
grass/trunk/scripts/v.db.droptable/v.db.droptable.py
grass/trunk/scripts/v.db.join/v.db.join.py
grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py
grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py
grass/trunk/scripts/v.db.update/v.db.update.py
grass/trunk/scripts/v.dissolve/v.dissolve.py
grass/trunk/scripts/v.rast.stats/v.rast.stats.py
grass/trunk/scripts/v.report/v.report.py
grass/trunk/scripts/v.to.3d/v.to.3d.py
Log:
grass.py: vector_db returns dictionary instead of list
new fn vector_layer_db() for common use of vector_db()
Modified: grass/trunk/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gselect.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/gui/wxpython/gui_modules/gselect.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -322,35 +322,10 @@
def __CheckDBConnection(self):
"""Check DB connection"""
- layerCommand = gcmd.Command(cmd=["v.db.connect",
- "-g", "--q",
- "map=%s" % self.map],
- rerr=None, stderr=None)
+ nuldev = file(os.devnull, 'w+')
+ self.layers = grass.vector_db(map=self.map, stderr=nuldev)
+ nuldev.close()
- if layerCommand.returncode != 0:
- return False
-
- # list of available layers & (table, database, driver)
- for line in layerCommand.ReadStdOutput():
- lineList = line.split(' ')
- layer = lineList[0]
- if '/' in layer:
- layer, layer_name = lineList[0].split('/')
- else:
- layer_name = None
- # database can contain ' ' in it's path
- if len(lineList) > 5:
- database = ''.join(lineList[3:-1])
- else:
- database = lineList[3]
- self.layers[int(layer)] = {
- "name" : layer_name,
- "table" : lineList[1],
- "key" : lineList[2],
- "database" : database,
- "driver" : lineList[-1]
- }
-
if (len(self.layers.keys()) == 0):
return False
Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/lib/python/grass.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -441,22 +441,48 @@
# run "v.db.connect -g ..." and parse output
-def vector_db(map, layer = None, **args):
+def vector_db(map, **args):
"""Return the database connection details for a vector map
(interface to `v.db.connect -g').
+
+ @param map vector map
+
+ @return dictionary { layer : { 'layer', 'table, 'database', 'driver', 'key' }
"""
- s = read_command('v.db.connect', flags = 'g', map = map, layer = layer, fs = '|', **args)
- result = []
+ s = read_command('v.db.connect', flags = 'g', map = map, fs = '|', **args)
+ result = {}
for l in s.splitlines():
f = l.split('|')
if len(f) != 5:
continue
- if layer and int(layer) == int(f[0]):
- return f
- result.append(f)
- if not layer:
- return result
+ if '/' in f[0]:
+ f1 = f.split('/')
+ layer = f1[0]
+ name = f1[1]
+ else:
+ layer = f[0]
+ name = ''
+
+ result[int(layer)] = {
+ 'layer' : layer,
+ 'name' : name,
+ 'table' : f[1],
+ 'key' : f[2],
+ 'database' : f[3],
+ 'driver' : f[4] }
+
+ return result
+def vector_layer_db(map, layer):
+ """Return the database connection details for a vector map layer.
+ If db connection for given layer is not defined, fatal() is called."""
+ try:
+ f = vector_db(map)[int(layer)]
+ except KeyError:
+ grass.fatal("Database connection not defined for layer %s" % layer)
+
+ return f
+
# run "db.describe -c ..." and parse output
def db_describe(table, **args):
Modified: grass/trunk/scripts/db.droptable/db.droptable.py
===================================================================
--- grass/trunk/scripts/db.droptable/db.droptable.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/db.droptable/db.droptable.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -62,10 +62,10 @@
used = []
vects = grass.list_strings('vect')
for vect in vects:
- for f in vector_db(vect, stderr = nuldev):
+ for f in vector_db(vect, stderr = nuldev).itervalues():
if not f:
continue
- if f[1] == table:
+ if f['table'] == table:
used.append(vect)
break
if used:
Modified: grass/trunk/scripts/v.db.addcol/v.db.addcol.py
===================================================================
--- grass/trunk/scripts/v.db.addcol/v.db.addcol.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.addcol/v.db.addcol.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -62,12 +62,13 @@
if not exists:
grass.fatal("Vector map <%s> not found in current mapset" % map)
- f = grass.vector_db(map, layer)
- if not f:
+ try:
+ f = grass.vector_db(map)[int(layer)]
+ except KeyError:
grass.fatal("There is no table connected to this map. Run v.db.connect or v.db.addtable first.")
- table = f[1]
- database = f[3]
- driver = f[4]
+ table = f['table']
+ database = f['database']
+ driver = f['driver']
colnum = len(columns)
Modified: grass/trunk/scripts/v.db.addtable/v.db.addtable.py
===================================================================
--- grass/trunk/scripts/v.db.addtable/v.db.addtable.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.addtable/v.db.addtable.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -85,9 +85,9 @@
nuldev = file(os.devnull, 'w')
db = grass.vector_db(map, stderr = nuldev)
if db:
- f = db[0]
- database = f[3]
- driver = f[4]
+ f = db[min(db.keys())]
+ database = f['database']
+ driver = f['driver']
else:
# nothing defined
grass.message("Creating new DB connection based on default mapset settings...")
@@ -96,9 +96,12 @@
driver = kv['driver']
# maybe there is already a table linked to the selected layer?
- if grass.vector_db(map, layer, stderr = nuldev):
+ try:
+ grass.vector_db(map, stderr = nuldev)[int(layer)]
grass.fatal("There is already a table linked to layer <%s>" % layer)
-
+ except KeyError:
+ pass
+
# maybe there is already a table with that name?
found = False
p = grass.pipe_command('db.tables', database = database, driver = driver, stderr = nuldev)
Modified: grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py
===================================================================
--- grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.dropcol/v.db.dropcol.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -63,14 +63,13 @@
if not grass.find_file(map, element = 'vector', mapset = mapset):
grass.fatal("Vector map <%s> not found in current mapset" % map)
- f = grass.vector_db(map, layer)
- if not f:
- grass.fatal("An error occured while running v.db.connect")
- table = f[1]
- keycol = f[2]
- database = f[3]
- driver = f[4]
+ f = grass.vector_layer_db(map, layer)
+ table = f['table']
+ keycol = f['key']
+ database = f['database']
+ driver = f['driver']
+
if not table:
grass.fatal("There is no table connected to the input vector map Cannot delete any column")
Modified: grass/trunk/scripts/v.db.droptable/v.db.droptable.py
===================================================================
--- grass/trunk/scripts/v.db.droptable/v.db.droptable.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.droptable/v.db.droptable.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -55,25 +55,23 @@
layer = options['layer']
# do some paranoia tests as well:
- f = grass.vector_db(map, layer)
- if not f:
- grass.fatal("An error occured while running v.db.connect")
-
+ f = grass.vector_layer_db(map, layer)
+
if not table:
# Removing table name connected to selected layer
- table = f[1]
+ table = f['table']
if not table:
- grass.fatal("No table assigned to layer <$GIS_OPT_LAYER>")
+ grass.fatal("No table assigned to layer <%s>" % layer)
else:
# Removing user specified table
- existingtable = f[1]
+ existingtable = f['table']
if existingtable != table:
grass.fatal("User selected table <%s> but the table <%s> is linked to layer <%s>"
% (table, existingtable, layer))
# we use the DB settings selected layer
- database = f[3]
- driver = f[4]
+ database = f['database']
+ driver = f['driver']
grass.message("Removing table <%s> linked to layer <%s> of vector map <%s>"
% (table, layer, map))
Modified: grass/trunk/scripts/v.db.join/v.db.join.py
===================================================================
--- grass/trunk/scripts/v.db.join/v.db.join.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.join/v.db.join.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -73,13 +73,12 @@
otable = options['otable']
ocolumn = options['ocolumn']
- f = grass.vector_db(map, layer)
- if not f:
- grass.fatal("An error occured while running v.db.connect")
- maptable = f[1]
- database = f[3]
- driver = f[4]
+ f = grass.vector_layer_db(map, layer)
+ maptable = f['table']
+ database = f['database']
+ driver = f['driver']
+
if driver == 'dbf':
grass.fatal("JOIN is not supported for tables stored in DBF format.")
Modified: grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py
===================================================================
--- grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -59,12 +59,12 @@
for vect in grass.list_grouped('vect')[mapset]:
vect = "%s@%s" % (vect, mapset)
grass.message("Reconnecting vector <%s>" % vect)
- for f in grass.vector_db(map, stderr = nuldev):
- layer = f[0]
- schema_table = f[1]
- key = f[2]
- database = f[3]
- driver = f[4]
+ for f in grass.vector_db(map, stderr = nuldev).itervalues():
+ layer = f['layer']
+ schema_table = f['table']
+ key = f['key']
+ database = f['database']
+ driver = f['driver']
if '.' in schema_table:
st = schema_table.split('.', 1)
schema = st[0]
Modified: grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py
===================================================================
--- grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.renamecol/v.db.renamecol.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -62,14 +62,13 @@
if not grass.find_file(map, element = 'vector', mapset = mapset):
grass.fatal("Vector map <%s> not found in current mapset" % map)
- f = grass.vector_db(map, layer)
- if not f:
- grass.fatal("An error occured while running v.db.connect")
- table = f[1]
- keycol = f[2]
- database = f[3]
- driver = f[4]
+ f = grass.vector_layer_db(map, layer)
+ table = f['table']
+ keycol = f['key']
+ database = f['database']
+ driver = f['driver']
+
if not table:
grass.fatal("There is no table connected to the input vector map. Cannot rename any column")
Modified: grass/trunk/scripts/v.db.update/v.db.update.py
===================================================================
--- grass/trunk/scripts/v.db.update/v.db.update.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.db.update/v.db.update.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -79,13 +79,14 @@
if not grass.find_file(map, element = 'vector', mapset = mapset)['file']:
grass.fatal("Vector map '$GIS_OPT_MAP' not found in current mapset")
- f = grass.vector_db(map, layer)
- if not f:
+ try:
+ f = grass.vector_db(map)[int(layer)]
+ except KeyError:
grass.fatal('There is no table connected to this map. Run v.db.connect or v.db.addtable first.')
- table = f[1]
- database = f[3]
- driver = f[4]
+ table = f['table']
+ database = f['database']
+ driver = f['driver']
# checking column types
try:
Modified: grass/trunk/scripts/v.dissolve/v.dissolve.py
===================================================================
--- grass/trunk/scripts/v.dissolve/v.dissolve.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.dissolve/v.dissolve.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -88,11 +88,9 @@
if coltype not in ('INTEGER', 'CHARACTER'):
grass.fatal("Key column must be of type integer or string")
- f = grass.vector_db(input, layer)
- if not f:
- grass.fatal("There is no table connected to this map")
+ f = grass.vector_layer_db(input, layer)
- table = f[1]
+ table = f['table']
tmpfile = '%s_%s' % (output, tmp)
Modified: grass/trunk/scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/v.rast.stats.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.rast.stats/v.rast.stats.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -156,8 +156,9 @@
grass.fatal("No categories found in raster map")
#check if DBF driver used, in this case cut to 10 chars col names:
- f = grass.vector_db(vector, layer)
- if not f:
+ try:
+ f = grass.vector_db(map = vector)[int(layer)]
+ except KeyError:
grass.fatal('There is no table connected to this map. Run v.db.connect or v.db.addtable first.')
# we need this for non-DBF driver:
table = f[1]
Modified: grass/trunk/scripts/v.report/v.report.py
===================================================================
--- grass/trunk/scripts/v.report/v.report.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.report/v.report.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -116,11 +116,13 @@
records1.sort()
if len(records1) == 0:
- f = grass.vector_db(mapname, layer)
- key = f[2]
- grass.fatal("There is a table connected to input vector map '%s', but" +
- "there are no categories present in the key column '%s'. Consider using" +
- "v.to.db to correct this." % (mapname, key))
+ try:
+ f = grass.vector_db(map = mapname)[int(layer)]
+ grass.fatal("There is a table connected to input vector map '%s', but" +
+ "there are no categories present in the key column '%s'. Consider using" +
+ "v.to.db to correct this." % (mapname, f['key']))
+ except KeyError:
+ pass
#fetch the requested attribute sorted by cat:
p = grass.pipe_command('v.to.db', flags = 'p',
Modified: grass/trunk/scripts/v.to.3d/v.to.3d.py
===================================================================
--- grass/trunk/scripts/v.to.3d/v.to.3d.py 2008-12-19 23:07:41 UTC (rev 34951)
+++ grass/trunk/scripts/v.to.3d/v.to.3d.py 2008-12-20 12:59:36 UTC (rev 34952)
@@ -104,7 +104,8 @@
'DOUBLE PRECISION'):
grass.fatal("Column type must be numeric")
- table = grass.vector_db(map = input, layer = layer)[1]
+ f = grass.vector_layer_db(map = input, layer = layer)
+ table = f['table']
columns = "zshift:%s" % column
grass.run_command('v.transform', input = input, output = output,
More information about the grass-commit
mailing list