[GRASS-SVN] r34954 - grass/branches/releasebranch_6_4/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Dec 20 08:12:05 EST 2008
Author: martinl
Date: 2008-12-20 08:12:04 -0500 (Sat, 20 Dec 2008)
New Revision: 34954
Modified:
grass/branches/releasebranch_6_4/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, r34953)
Modified: grass/branches/releasebranch_6_4/lib/python/grass.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/grass.py 2008-12-20 13:04:22 UTC (rev 34953)
+++ grass/branches/releasebranch_6_4/lib/python/grass.py 2008-12-20 13:12:04 UTC (rev 34954)
@@ -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