[GRASS-SVN] r34953 - in grass/branches/develbranch_6:
gui/wxpython/gui_modules lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Dec 20 08:04:23 EST 2008
Author: martinl
Date: 2008-12-20 08:04:22 -0500 (Sat, 20 Dec 2008)
New Revision: 34953
Modified:
grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py
grass/branches/develbranch_6/lib/python/grass.py
Log:
grass.py: vector_db returns dictionary instead of list
new fn vector_layer_db() for common use of vector_db()
(merge from devbr6, r34952)
Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py 2008-12-20 12:59:36 UTC (rev 34952)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gselect.py 2008-12-20 13:04:22 UTC (rev 34953)
@@ -322,34 +322,9 @@
def __CheckDBConnection(self):
"""Check DB connection"""
- layerCommand = gcmd.Command(cmd=["v.db.connect",
- "-g", "--q",
- "map=%s" % self.map],
- rerr=None, stderr=None)
- 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]
- }
-
+ nuldev = file(os.devnull, 'w+')
+ self.layers = grass.vector_db(map=self.map, stderr=nuldev)
+ nuldev.close()
if (len(self.layers.keys()) == 0):
return False
Modified: grass/branches/develbranch_6/lib/python/grass.py
===================================================================
--- grass/branches/develbranch_6/lib/python/grass.py 2008-12-20 12:59:36 UTC (rev 34952)
+++ grass/branches/develbranch_6/lib/python/grass.py 2008-12-20 13:04:22 UTC (rev 34953)
@@ -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):
More information about the grass-commit
mailing list