d.redraw.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  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 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. #% keywords: display
  17. #% keywords: graphics
  18. #% keywords: monitors
  19. #% keywords: CLI
  20. #%end
  21. import sys
  22. import shlex
  23. from grass.script import core as grass
  24. def split(s):
  25. """!Platform spefic shlex.split"""
  26. if sys.version_info >= (2, 6):
  27. return shlex.split(s, posix = (sys.platform != "win32"))
  28. elif sys.platform == "win32":
  29. return shlex.split(s.replace('\\', r'\\'))
  30. else:
  31. return shlex.split(s)
  32. def main():
  33. env = grass.gisenv()
  34. mon = env.get('MONITOR', None)
  35. if not mon:
  36. grass.fatal(_("No monitor selected. Run `d.mon` to select monitor."))
  37. monCmd = env.get('MONITOR_%s_CMDFILE' % mon)
  38. if not monCmd:
  39. grass.fatal(_("No cmd file found for monitor <%s>") % mon)
  40. try:
  41. fd = open(monCmd, 'r')
  42. cmdList = fd.readlines()
  43. grass.run_command('d.erase')
  44. for cmd in cmdList:
  45. grass.call(split(cmd))
  46. except IOError, e:
  47. grass.fatal(_("Unable to open file '%s' for reading. Details: %s") % \
  48. (monCmd, e))
  49. fd.close()
  50. # restore cmd file
  51. try:
  52. fd = open(monCmd, "w")
  53. fd.writelines(cmdList)
  54. except IOError, e:
  55. grass.fatal(_("Unable to open file '%s' for writing. Details: %s") % \
  56. (monCmd, e))
  57. return 0
  58. if __name__ == "__main__":
  59. options, flags = grass.parser()
  60. sys.exit(main())