giface.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """!
  2. @package core.giface
  3. @brief GRASS interface for standalone application (without layer manager)
  4. Classes:
  5. - giface::StandaloneGrassInterface
  6. (C) 2012 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Anna Kratochvilova <kratochanna gmail.com>
  10. @author Vaclav Petras <wenzeslaus gmail.com>
  11. """
  12. import os
  13. from core.gconsole import GConsole, \
  14. EVT_CMD_OUTPUT, EVT_CMD_PROGRESS, EVT_CMD_RUN, EVT_CMD_DONE, \
  15. EVT_WRITE_LOG, EVT_WRITE_CMD_LOG, EVT_WRITE_WARNING, EVT_WRITE_ERROR
  16. import grass.script as grass
  17. class StandaloneGrassInterface():
  18. def __init__(self):
  19. self._gconsole = GConsole()
  20. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  21. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  22. self._gconsole.Bind(EVT_WRITE_LOG,
  23. lambda event:
  24. self.WriteLog(text = event.text))
  25. self._gconsole.Bind(EVT_WRITE_CMD_LOG,
  26. lambda event:
  27. self.WriteCmdLog(line = event.line))
  28. self._gconsole.Bind(EVT_WRITE_WARNING,
  29. lambda event:
  30. self.WriteWarning(line = event.line))
  31. self._gconsole.Bind(EVT_WRITE_ERROR,
  32. lambda event:
  33. self.WriteError(line = event.line))
  34. def _onCmdOutput(self, event):
  35. """!Print command output"""
  36. message = event.text
  37. style = event.type
  38. if style == 'warning':
  39. self.WriteWarning(message)
  40. elif style == 'error':
  41. self.WriteError(message)
  42. else:
  43. self.WriteLog(message)
  44. event.Skip()
  45. def _onCmdProgress(self, event):
  46. """!Update progress message info"""
  47. grass.percent(event.value, 100, 1)
  48. event.Skip()
  49. def RunCmd(self, command, compReg=True, switchPage=False, skipInterface=False,
  50. onDone=None, onPrepare=None, userData=None, priority=1):
  51. self._gconsole.RunCmd(command=command, compReg=compReg, switchPage=switchPage,
  52. skipInterface=skipInterface, onDone=onDone,
  53. onPrepare=onPrepare, userData=userData, priority=priority)
  54. def Help(self, entry):
  55. self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
  56. def WriteLog(self, text, wrap = None,
  57. switchPage = False, priority = 1):
  58. self._write(grass.message, text)
  59. def WriteCmdLog(self, line, pid = None, switchPage = True):
  60. if pid:
  61. line = '(' + str(pid) + ') ' + line
  62. self._write(grass.message, line)
  63. def WriteWarning(self, line):
  64. self._write(grass.warning, line)
  65. def WriteError(self, line):
  66. self._write(grass.error, line)
  67. def _write(self, function, text):
  68. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  69. os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
  70. function(text)
  71. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  72. def GetLayerTree(self):
  73. return None
  74. def GetMapDisplay(self):
  75. """!Get current map display.
  76. """
  77. return None
  78. def GetAllMapDisplays(self):
  79. """!Get list of all map displays.
  80. """
  81. return []
  82. def GetMapWindow(self):
  83. return None
  84. def GetProgress(self):
  85. raise NotImplementedError