v.db.droptable.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.droptable
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: interface to db.execute to drop an existing table of given vector map
  8. # COPYRIGHT: (C) 2005, 2008 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%Module
  16. #% description: Removes existing attribute table of a vector map.
  17. #% keyword: vector
  18. #% keyword: attribute table
  19. #% keyword: database
  20. #%End
  21. #%flag
  22. #% key: f
  23. #% description: Force removal (required for actual deletion of table)
  24. #%end
  25. #%option G_OPT_V_MAP
  26. #%end
  27. #%option G_OPT_DB_TABLE
  28. #% description: Table name (default: vector map name)
  29. #%end
  30. #%option G_OPT_V_FIELD
  31. #% required : no
  32. #%end
  33. import sys
  34. import os
  35. import grass.script as grass
  36. from grass.exceptions import CalledModuleError
  37. def main():
  38. force = flags['f']
  39. map = options['map']
  40. table = options['table']
  41. layer = options['layer']
  42. # do some paranoia tests as well:
  43. f = grass.vector_layer_db(map, layer)
  44. if not table:
  45. # Removing table name connected to selected layer
  46. table = f['table']
  47. if not table:
  48. grass.fatal(_("No table assigned to layer <%s>") % layer)
  49. else:
  50. # Removing user specified table
  51. existingtable = f['table']
  52. if existingtable != table:
  53. grass.fatal(_("User selected table <%s> but the table <%s> is linked to layer <%s>")
  54. % (table, existingtable, layer))
  55. # we use the DB settings selected layer
  56. database = f['database']
  57. driver = f['driver']
  58. grass.message(_("Removing table <%s> linked to layer <%s> of vector map <%s>")
  59. % (table, layer, map))
  60. if not force:
  61. grass.message(_("You must use the -f (force) flag to actually remove the table. Exiting."))
  62. grass.message(_("Leaving map/table unchanged."))
  63. sys.exit(0)
  64. grass.message(_("Dropping table <%s>...") % table)
  65. try:
  66. grass.write_command('db.execute', stdin="DROP TABLE %s" % table,
  67. input='-', database=database, driver=driver)
  68. except CalledModuleError:
  69. grass.fatal(_("An error occurred while running db.execute"))
  70. grass.run_command('v.db.connect', flags = 'd', map = map, layer = layer)
  71. grass.message(_("Current attribute table link(s):"))
  72. # silently test first to avoid confusing error messages
  73. nuldev = file(os.devnull, 'w')
  74. try:
  75. grass.run_command('v.db.connect', flags='p', map=map, quiet=True,
  76. stdout=nuldev, stderr=nuldev)
  77. except CalledModuleError:
  78. grass.message(_("(No database links remaining)"))
  79. else:
  80. grass.run_command('v.db.connect', flags='p', map=map)
  81. # write cmd history:
  82. grass.vector_history(map)
  83. if __name__ == "__main__":
  84. options, flags = grass.parser()
  85. main()