d.redraw.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: d.redraw
  5. # AUTHOR(S): Martin Landa <landa.martin gmail.com>
  6. # PURPOSE: Redraws the content of currently selected monitor
  7. # COPYRIGHT: (C) 2011-2015 by the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General
  10. # Public License (>=v2). Read the file COPYING that
  11. # comes with GRASS for details.
  12. #
  13. #############################################################################
  14. # %module
  15. # % description: Redraws the content of currently selected monitor.
  16. # % keyword: display
  17. # % keyword: graphics
  18. # % keyword: monitors
  19. # %end
  20. import os
  21. import sys
  22. from grass.script import core as grass
  23. from grass.script.utils import split
  24. def main():
  25. mon = grass.gisenv().get("MONITOR", None)
  26. if not mon:
  27. grass.fatal(
  28. _("No graphics device selected. Use d.mon to select graphics device.")
  29. )
  30. monCmd = grass.parse_command("d.mon", flags="g").get("cmd", None)
  31. if not monCmd or not os.path.isfile(monCmd):
  32. grass.fatal(_("Unable to open file '%s'") % monCmd)
  33. try:
  34. fd = open(monCmd, "r")
  35. cmdList = fd.readlines()
  36. grass.run_command("d.erase")
  37. for cmd in cmdList:
  38. if cmd.startswith("#"):
  39. continue
  40. grass.call(split(cmd))
  41. except IOError as e:
  42. grass.fatal(
  43. _("Unable to open file '%s' for reading. Details: %s") % (monCmd, e)
  44. )
  45. fd.close()
  46. # restore cmd file
  47. try:
  48. fd = open(monCmd, "w")
  49. fd.writelines(cmdList)
  50. except IOError as e:
  51. grass.fatal(
  52. _("Unable to open file '%s' for writing. Details: %s") % (monCmd, e)
  53. )
  54. return 0
  55. if __name__ == "__main__":
  56. options, flags = grass.parser()
  57. sys.exit(main())