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