render_cmd.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from grass.script import core as grass
  5. from grass.script import task as gtask
  6. cmd, dcmd = gtask.cmdstring_to_tuple(sys.argv[1])
  7. if not cmd or cmd == 'd.mon':
  8. sys.exit(0)
  9. path = os.path.dirname(os.path.abspath(__file__))
  10. cmd_file = os.path.join(path, 'cmd')
  11. env_file = os.path.join(path, 'env')
  12. # read environment variables from file
  13. fd = open(env_file, 'r')
  14. if fd is None:
  15. grass.fatal("Unable to open file '%s'" % env_file)
  16. lines = fd.readlines()
  17. for l in lines:
  18. if l.startswith('#'):
  19. continue
  20. k, v = l.rstrip('\n').split('#', 1)[0].strip().split('=', 1)
  21. os.environ[k] = v
  22. fd.close()
  23. # run display command
  24. try:
  25. grass.run_command(cmd, **dcmd)
  26. except Exception as e:
  27. sys.exit("ERROR: %s" % e)
  28. # update cmd file
  29. ignoredCmd = ('d.colorlist', 'd.font', 'd.fontlist',
  30. 'd.frame', 'd.info', 'd.mon', 'd.out.file',
  31. 'd.redraw', 'd.to.rast', 'd.what.rast',
  32. 'd.what.vect', 'd.where')
  33. if cmd not in ignoredCmd:
  34. mode = 'w' if cmd == 'd.erase' else 'a'
  35. # update cmd file
  36. fd = open(cmd_file, mode)
  37. if fd is None:
  38. grass.fatal("Unable to open file '%s'" % cmd_file)
  39. if mode == 'a':
  40. fd.write(sys.argv[1])
  41. fd.write('\n')
  42. else:
  43. fd.write('')
  44. fd.close()
  45. sys.exit(0)