giface.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. from core.utils import _
  17. import grass.script as grass
  18. from grass.pydispatch.signal import Signal
  19. # to disable Abstract class not referenced
  20. #pylint: disable=R0921
  21. class Layer(object):
  22. """!Layer is generaly usable layer object.
  23. @note Currently without specifying the interface.
  24. Current implementations only provides all attributes of existing layer
  25. as used in lmgr.
  26. """
  27. class LayerList(object):
  28. def GetSelectedLayers(self, checkedOnly=True):
  29. """!Returns list of selected layers.
  30. @note Usage of checked and selected is still subject to change.
  31. Checked layers should be showed. Selected are for analyses.
  32. However, this may be the same for some implementations
  33. (e.g. it d.mon has all layers checked and selected).
  34. """
  35. raise NotImplementedError()
  36. class GrassInterface:
  37. """!GrassInterface provides the functionality which should be available
  38. to every GUI component.
  39. @note The GrassInterface process is not finished.
  40. """
  41. def RunCmd(self, *args, **kwargs):
  42. """!Executes a command.
  43. """
  44. raise NotImplementedError()
  45. def Help(self, entry):
  46. """Shows a manual page for a given entry.
  47. """
  48. raise NotImplementedError()
  49. def WriteLog(self, text, wrap=None, switchPage=False, priority=1):
  50. """!Writes log message.
  51. @note It is not clear how the switchPage and priority should work.
  52. @note Use priority rather than switchPage, switchPage will be removed.
  53. """
  54. raise NotImplementedError()
  55. def WriteCmdLog(self, line, pid=None, switchPage=True):
  56. """!Writes message related to start or end of the command.
  57. """
  58. raise NotImplementedError()
  59. def WriteWarning(self, line):
  60. """!Writes warning message for the user.
  61. Currently used also for important messages for user.
  62. Overlaps with log message with high priority.
  63. """
  64. raise NotImplementedError()
  65. def WriteError(self, line):
  66. """!Writes error message for the user."""
  67. raise NotImplementedError()
  68. def GetLayerTree(self):
  69. """!Returns LayerManager's tree GUI object.
  70. @note Will be removed from the interface.
  71. """
  72. raise NotImplementedError()
  73. def GetLayerList(self):
  74. """!Returns a layer management object.
  75. """
  76. raise NotImplementedError()
  77. def GetMapDisplay(self):
  78. """!Returns current map display.
  79. @note For layer related tasks use GetLayerList().
  80. @return MapFrame instance
  81. @return None when no mapdisplay open
  82. """
  83. raise NotImplementedError()
  84. def GetAllMapDisplays(self):
  85. """!Get list of all map displays.
  86. @note Might be removed from the interface.
  87. @return list of MapFrame instances
  88. """
  89. raise NotImplementedError()
  90. def GetMapWindow(self):
  91. """!Returns current map window.
  92. @note For layer related tasks use GetLayerList().
  93. """
  94. raise NotImplementedError()
  95. def GetProgress(self):
  96. """!Returns object which shows the progress.
  97. @note Some implementations may not implement this method.
  98. """
  99. raise NotImplementedError()
  100. class StandaloneGrassInterface():
  101. """!@implements GrassInterface"""
  102. def __init__(self):
  103. # Signal when some map is created or updated by a module.
  104. # attributes: name: map name, ltype: map type,
  105. # add: if map should be added to layer tree (questionable attribute)
  106. self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
  107. # Signal emitted to request updating of map
  108. self.updateMap = Signal('StandaloneGrassInterface.updateMap')
  109. self._gconsole = GConsole()
  110. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  111. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  112. self._gconsole.Bind(EVT_WRITE_LOG,
  113. lambda event: self.WriteLog(text=event.text))
  114. self._gconsole.Bind(EVT_WRITE_CMD_LOG,
  115. lambda event: self.WriteCmdLog(line=event.line))
  116. self._gconsole.Bind(EVT_WRITE_WARNING,
  117. lambda event: self.WriteWarning(line=event.line))
  118. self._gconsole.Bind(EVT_WRITE_ERROR,
  119. lambda event: self.WriteError(line=event.line))
  120. def _onCmdOutput(self, event):
  121. """!Print command output"""
  122. message = event.text
  123. style = event.type
  124. if style == 'warning':
  125. self.WriteWarning(message)
  126. elif style == 'error':
  127. self.WriteError(message)
  128. else:
  129. self.WriteLog(message)
  130. event.Skip()
  131. def _onCmdProgress(self, event):
  132. """!Update progress message info"""
  133. grass.percent(event.value, 100, 1)
  134. event.Skip()
  135. def RunCmd(self, command, compReg=True, switchPage=False, skipInterface=False,
  136. onDone=None, onPrepare=None, userData=None, priority=1):
  137. self._gconsole.RunCmd(command=command, compReg=compReg, switchPage=switchPage,
  138. skipInterface=skipInterface, onDone=onDone,
  139. onPrepare=onPrepare, userData=userData, priority=priority)
  140. def Help(self, entry):
  141. self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
  142. def WriteLog(self, text, wrap=None,
  143. switchPage=False, priority=1):
  144. self._write(grass.message, text)
  145. def WriteCmdLog(self, line, pid=None, switchPage=True):
  146. if pid:
  147. line = '(' + str(pid) + ') ' + line
  148. self._write(grass.message, line)
  149. def WriteWarning(self, line):
  150. self._write(grass.warning, line)
  151. def WriteError(self, line):
  152. self._write(grass.error, line)
  153. def _write(self, function, text):
  154. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  155. os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
  156. function(text)
  157. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  158. def GetLayerList(self):
  159. return None
  160. def GetMapDisplay(self):
  161. """!Get current map display.
  162. """
  163. return None
  164. def GetAllMapDisplays(self):
  165. """!Get list of all map displays.
  166. """
  167. return []
  168. def GetMapWindow(self):
  169. return None
  170. def GetProgress(self):
  171. raise NotImplementedError()