[GRASS-SVN] r55046 - in grass/trunk: lib/python/script scripts/db.droptable
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Feb 14 05:08:55 PST 2013
Author: lucadelu
Date: 2013-02-14 05:08:55 -0800 (Thu, 14 Feb 2013)
New Revision: 55046
Modified:
grass/trunk/lib/python/script/db.py
grass/trunk/scripts/db.droptable/db.droptable.py
Log:
add function to check if a table is connected with some vectors; use the new function in db.droptable
Modified: grass/trunk/lib/python/script/db.py
===================================================================
--- grass/trunk/lib/python/script/db.py 2013-02-14 12:36:26 UTC (rev 55045)
+++ grass/trunk/lib/python/script/db.py 2013-02-14 13:08:55 UTC (rev 55046)
@@ -160,3 +160,26 @@
try_remove(fname)
return tuple(result)
+
+
+def db_table_in_vector(table):
+ """Return the name of vector connected to the table.
+ It returns False if no vectors are connected to the table.
+
+ Example
+
+ @params table name of table to query
+
+ """
+ from vector import vector_db
+ nuldev = file(os.devnull, 'w')
+ used = []
+ vects = list_strings('vect')
+ for vect in vects:
+ for f in vector_db(vect, stderr=nuldev).itervalues():
+ if not f:
+ continue
+ if f['table'] == table:
+ used.append(vect)
+ break
+ return used
Modified: grass/trunk/scripts/db.droptable/db.droptable.py
===================================================================
--- grass/trunk/scripts/db.droptable/db.droptable.py 2013-02-14 12:36:26 UTC (rev 55045)
+++ grass/trunk/scripts/db.droptable/db.droptable.py 2013-02-14 13:08:55 UTC (rev 55046)
@@ -3,7 +3,7 @@
############################################################################
#
# MODULE: db.droptable
-# AUTHOR(S): Markus Neteler
+# AUTHOR(S): Markus Neteler
# Converted to Python by Glynn Clements
# PURPOSE: Interface to db.execute to drop an attribute table
# COPYRIGHT: (C) 2007, 2012 by Markus Neteler and the GRASS Development Team
@@ -43,9 +43,9 @@
#%end
import sys
-import os
import grass.script as grass
+
def main():
table = options['table']
force = flags['f']
@@ -53,7 +53,7 @@
if not options['driver'] or not options['database']:
# check if DB parameters are set, and if not set them.
grass.run_command('db.connect', flags = 'c')
-
+
kv = grass.db_connection()
if options['database']:
database = options['database']
@@ -64,45 +64,35 @@
else:
driver = kv['driver']
# schema needed for PG?
-
+
if force:
grass.message(_("Forcing ..."))
-
+
# check if table exists
- nuldev = file(os.devnull, 'w')
if not grass.db_table_exist(table):
grass.fatal(_("Table <%s> not found in database <%s>") % \
(table, database))
-
+
# check if table is used somewhere (connected to vector map)
- used = []
- vects = grass.list_strings('vect')
- for vect in vects:
- for f in grass.vector_db(vect, stderr = nuldev).itervalues():
- if not f:
- continue
- if f['table'] == table:
- used.append(vect)
- break
+ used = grass.db.db_table_in_vector(table)
if len(used) > 0:
grass.warning(_("Deleting table <%s> which is attached to following map(s):") % table)
for vect in used:
grass.warning("%s" % vect)
-
+
if not force:
grass.message(_("The table <%s> would be deleted.") % table)
grass.message("")
grass.message(_("You must use the force flag to actually remove it. Exiting."))
sys.exit(0)
-
+
p = grass.feed_command('db.execute', input = '-', database = database, driver = driver)
p.stdin.write("DROP TABLE " + table)
p.stdin.close()
p.wait()
if p.returncode != 0:
grass.fatal(_("Cannot continue (problem deleting table)."))
-
+
if __name__ == "__main__":
options, flags = grass.parser()
main()
-
More information about the grass-commit
mailing list