v.db.droptable.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  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 gscript
  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. # We check for existence of the map in the current mapset before
  43. # doing any other operation.
  44. info = gscript.find_file(map, element='vector', mapset=".")
  45. if not info['file']:
  46. mapset = gscript.gisenv()["MAPSET"]
  47. # Message is formulated in the way that it does not mislead
  48. # in case where a map of the same name is in another mapset.
  49. gscript.fatal(_("Vector map <{name}> not found"
  50. " in the current mapset ({mapset})").format(
  51. name=map, mapset=mapset))
  52. # do some paranoia tests as well:
  53. f = gscript.vector_layer_db(map, layer)
  54. if not table:
  55. # Removing table name connected to selected layer
  56. table = f['table']
  57. if not table:
  58. gscript.fatal(_("No table assigned to layer <%s>") % layer)
  59. else:
  60. # Removing user specified table
  61. existingtable = f['table']
  62. if existingtable != table:
  63. gscript.fatal(_("User selected table <%s> but the table <%s> "
  64. "is linked to layer <%s>") %
  65. (table, existingtable, layer))
  66. # we use the DB settings selected layer
  67. database = f['database']
  68. driver = f['driver']
  69. gscript.message(_("Removing table <%s> linked to layer <%s> of vector"
  70. " map <%s>") % (table, layer, map))
  71. if not force:
  72. gscript.message(_("You must use the -f (force) flag to actually "
  73. "remove the table. Exiting."))
  74. gscript.message(_("Leaving map/table unchanged."))
  75. sys.exit(0)
  76. gscript.message(_("Dropping table <%s>...") % table)
  77. try:
  78. gscript.write_command('db.execute', stdin="DROP TABLE %s" % table,
  79. input='-', database=database, driver=driver)
  80. except CalledModuleError:
  81. gscript.fatal(_("An error occurred while running db.execute"))
  82. gscript.run_command('v.db.connect', flags='d', map=map, layer=layer)
  83. gscript.message(_("Current attribute table link(s):"))
  84. # silently test first to avoid confusing error messages
  85. nuldev = open(os.devnull, 'w')
  86. try:
  87. gscript.run_command('v.db.connect', flags='p', map=map, quiet=True,
  88. stdout=nuldev, stderr=nuldev)
  89. except CalledModuleError:
  90. gscript.message(_("(No database links remaining)"))
  91. else:
  92. gscript.run_command('v.db.connect', flags='p', map=map)
  93. # write cmd history:
  94. gscript.vector_history(map)
  95. if __name__ == "__main__":
  96. options, flags = gscript.parser()
  97. main()