v.db.droptable.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(
  50. _(
  51. "Vector map <{name}> not found" " in the current mapset ({mapset})"
  52. ).format(name=map, mapset=mapset)
  53. )
  54. # do some paranoia tests as well:
  55. f = gscript.vector_layer_db(map, layer)
  56. if not table:
  57. # Removing table name connected to selected layer
  58. table = f["table"]
  59. if not table:
  60. gscript.fatal(_("No table assigned to layer <%s>") % layer)
  61. else:
  62. # Removing user specified table
  63. existingtable = f["table"]
  64. if existingtable != table:
  65. gscript.fatal(
  66. _(
  67. "User selected table <%s> but the table <%s> "
  68. "is linked to layer <%s>"
  69. )
  70. % (table, existingtable, layer)
  71. )
  72. # we use the DB settings selected layer
  73. database = f["database"]
  74. driver = f["driver"]
  75. gscript.message(
  76. _("Removing table <%s> linked to layer <%s> of vector" " map <%s>")
  77. % (table, layer, map)
  78. )
  79. if not force:
  80. gscript.message(
  81. _(
  82. "You must use the -f (force) flag to actually "
  83. "remove the table. Exiting."
  84. )
  85. )
  86. gscript.message(_("Leaving map/table unchanged."))
  87. sys.exit(0)
  88. gscript.message(_("Dropping table <%s>...") % table)
  89. try:
  90. gscript.write_command(
  91. "db.execute",
  92. stdin="DROP TABLE %s" % table,
  93. input="-",
  94. database=database,
  95. driver=driver,
  96. )
  97. except CalledModuleError:
  98. gscript.fatal(_("An error occurred while running db.execute"))
  99. gscript.run_command("v.db.connect", flags="d", map=map, layer=layer)
  100. gscript.message(_("Current attribute table link(s):"))
  101. # silently test first to avoid confusing error messages
  102. nuldev = open(os.devnull, "w")
  103. try:
  104. gscript.run_command(
  105. "v.db.connect", flags="p", map=map, quiet=True, stdout=nuldev, stderr=nuldev
  106. )
  107. except CalledModuleError:
  108. gscript.message(_("(No database links remaining)"))
  109. else:
  110. gscript.run_command("v.db.connect", flags="p", map=map)
  111. # write cmd history:
  112. gscript.vector_history(map)
  113. if __name__ == "__main__":
  114. options, flags = gscript.parser()
  115. main()