giface.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. """
  2. @package core.giface
  3. @brief GRASS interface for standalone application (without layer manager)
  4. Classes:
  5. - giface::StandaloneGrassInterface
  6. (C) 2012-2014 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. import sys
  14. import grass.script as grass
  15. from grass.pydispatch.signal import Signal
  16. # to disable Abstract class not referenced
  17. # pylint: disable=R0921
  18. class Notification:
  19. """Enum class for notifications suggestions.
  20. Can be used for log messages, commands, warnings, errors.
  21. The value is the suggestion how user should be notified
  22. about the new message.
  23. """
  24. NO_NOTIFICATION = 0
  25. HIGHLIGHT = 1
  26. MAKE_VISIBLE = 2
  27. RAISE_WINDOW = 3
  28. class Layer(object):
  29. """Layer is generally usable layer object.
  30. .. note::
  31. Currently without specifying the interface.
  32. Current implementations only provides all attributes of existing
  33. layer as used in lmgr.
  34. """
  35. pass
  36. class LayerList(object):
  37. def GetSelectedLayers(self, checkedOnly=True):
  38. """Returns list of selected layers.
  39. .. note::
  40. Usage of checked and selected is still subject to change.
  41. Checked layers should be showed. Selected are for analyses.
  42. However, this may be the same for some implementations
  43. (e.g. it d.mon has all layers checked and selected).
  44. """
  45. raise NotImplementedError()
  46. def GetSelectedLayer(self, checkedOnly=False):
  47. """Returns selected layer or None when there is no selected layer.
  48. .. note::
  49. Parameter checkedOnly is here False by default. This might
  50. change if we find the right way of handling unchecked layers.
  51. """
  52. raise NotImplementedError()
  53. def AddLayer(self, ltype, name=None, checked=None, opacity=1.0, cmd=None):
  54. """Adds a new layer to the layer list.
  55. Launches property dialog if needed (raster, vector, etc.)
  56. :param ltype: layer type (raster, vector, raster_3d, ...)
  57. :param name: layer name
  58. :param checked: if True layer is checked
  59. :param opacity: layer opacity level
  60. :param cmd: command (given as a list)
  61. """
  62. raise NotImplementedError()
  63. def GetLayersByName(self, name):
  64. """Returns list of layers with a given name.
  65. :param name: fully qualified map name
  66. .. todo::
  67. if common usage is just to check the presence of layer,
  68. intoroduce a new method ContainsLayerByName(name)
  69. """
  70. raise NotImplementedError()
  71. def GetLayerByData(self, key, value):
  72. """Returns layer with specified.
  73. .. note::
  74. Returns only one layer. This might change.
  75. .. warning::
  76. Avoid using this method, it might be removed in the future.
  77. """
  78. raise NotImplementedError()
  79. class GrassInterface:
  80. """GrassInterface provides the functionality which should be available
  81. to every GUI component.
  82. .. note::
  83. The GrassInterface process is not finished.
  84. """
  85. def RunCmd(self, *args, **kwargs):
  86. """Executes a command."""
  87. raise NotImplementedError()
  88. def Help(self, entry):
  89. """Shows a manual page for a given entry."""
  90. raise NotImplementedError()
  91. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  92. """Writes log message."""
  93. raise NotImplementedError()
  94. def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
  95. """Writes message related to start or end of the command."""
  96. raise NotImplementedError()
  97. def WriteWarning(self, text):
  98. """Writes warning message for the user."""
  99. raise NotImplementedError()
  100. def WriteError(self, text):
  101. """Writes error message for the user."""
  102. raise NotImplementedError()
  103. def GetLog(self, err=False):
  104. """Returns file-like object for writing."""
  105. raise NotImplementedError()
  106. def GetLayerTree(self):
  107. """Returns LayerManager's tree GUI object.
  108. .. note::
  109. Will be removed from the interface.
  110. """
  111. raise NotImplementedError()
  112. def GetLayerList(self):
  113. """Returns a layer management object."""
  114. raise NotImplementedError()
  115. def GetMapDisplay(self):
  116. """Returns current map display.
  117. .. note::
  118. For layer related tasks use GetLayerList().
  119. :return: MapFrame instance
  120. :return: None when no mapdisplay open
  121. """
  122. raise NotImplementedError()
  123. def GetAllMapDisplays(self):
  124. """Get list of all map displays.
  125. .. note::
  126. Might be removed from the interface.
  127. :return: list of MapFrame instances
  128. """
  129. raise NotImplementedError()
  130. def GetMapWindow(self):
  131. """Returns current map window.
  132. .. note::
  133. For layer related tasks use GetLayerList().
  134. """
  135. raise NotImplementedError()
  136. def GetProgress(self):
  137. """Returns object which shows the progress.
  138. .. note::
  139. Some implementations may not implement this method.
  140. """
  141. raise NotImplementedError()
  142. def UpdateCmdHistory(self, cmd):
  143. """Add the command to the current history list shown to the user
  144. .. note::
  145. Some implementations may not implement this method or do nothing.
  146. """
  147. raise NotImplementedError()
  148. class StandaloneGrassInterface(GrassInterface):
  149. """@implements GrassInterface"""
  150. def __init__(self):
  151. # Signal when some map is created or updated by a module.
  152. # Used for adding/refreshing displayed layers.
  153. # attributes: name: map name, ltype: map type,
  154. # add: if map should be added to layer tree (questionable attribute)
  155. self.mapCreated = Signal("StandaloneGrassInterface.mapCreated")
  156. # Signal for communicating current mapset has been switched
  157. self.currentMapsetChanged = Signal(
  158. "StandaloneGrassInterface.currentMapsetChanged"
  159. )
  160. # Signal for communicating something in current grassdb has changed.
  161. # Parameters:
  162. # action: required, is one of 'new', 'rename', 'delete'
  163. # element: required, can be one of 'grassdb', 'location', 'mapset', 'raster', 'vector' and 'raster_3d'
  164. # grassdb: path to grass db, required
  165. # location: location name, required
  166. # mapset: mapset name, required when element is 'mapset', 'raster', 'vector' or 'raster_3d'
  167. # map: map name, required when element is 'raster', 'vector' or 'raster_3d'
  168. # newname: new name (of mapset, map), required with action='rename'
  169. self.grassdbChanged = Signal("StandaloneGrassInterface.grassdbChanged")
  170. # Signal emitted to request updating of map
  171. self.updateMap = Signal("StandaloneGrassInterface.updateMap")
  172. # Signal emitted when workspace is changed
  173. self.workspaceChanged = Signal("StandaloneGrassInterface.workspaceChanged")
  174. # workaround, standalone grass interface should be moved to sep. file
  175. from core.gconsole import GConsole, EVT_CMD_OUTPUT, EVT_CMD_PROGRESS
  176. self._gconsole = GConsole()
  177. self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
  178. self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
  179. self._gconsole.writeLog.connect(self.WriteLog)
  180. self._gconsole.writeCmdLog.connect(self.WriteCmdLog)
  181. self._gconsole.writeWarning.connect(self.WriteWarning)
  182. self._gconsole.writeError.connect(self.WriteError)
  183. def _onCmdOutput(self, event):
  184. """Print command output"""
  185. message = event.text
  186. style = event.type
  187. if style == "warning":
  188. self.WriteWarning(message)
  189. elif style == "error":
  190. self.WriteError(message)
  191. else:
  192. self.WriteLog(message)
  193. event.Skip()
  194. def _onCmdProgress(self, event):
  195. """Update progress message info"""
  196. grass.percent(event.value, 100, 1)
  197. event.Skip()
  198. def RunCmd(
  199. self,
  200. command,
  201. compReg=True,
  202. env=None,
  203. skipInterface=False,
  204. onDone=None,
  205. onPrepare=None,
  206. userData=None,
  207. addLayer=None,
  208. notification=Notification.MAKE_VISIBLE,
  209. ):
  210. self._gconsole.RunCmd(
  211. command=command,
  212. compReg=compReg,
  213. env=env,
  214. skipInterface=skipInterface,
  215. onDone=onDone,
  216. onPrepare=onPrepare,
  217. userData=userData,
  218. addLayer=addLayer,
  219. notification=notification,
  220. )
  221. def Help(self, entry):
  222. self._gconsole.RunCmd(["g.manual", "entry=%s" % entry])
  223. def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
  224. self._write(grass.message, text)
  225. def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
  226. if pid:
  227. text = "(" + str(pid) + ") " + text
  228. self._write(grass.message, text)
  229. def WriteWarning(self, text):
  230. self._write(grass.warning, text)
  231. def WriteError(self, text):
  232. self._write(grass.error, text)
  233. def _write(self, function, text):
  234. orig = os.getenv("GRASS_MESSAGE_FORMAT")
  235. os.environ["GRASS_MESSAGE_FORMAT"] = "standard"
  236. function(text)
  237. os.environ["GRASS_MESSAGE_FORMAT"] = orig
  238. def GetLog(self, err=False):
  239. if err:
  240. return sys.stdout
  241. return sys.stderr
  242. def GetLayerList(self):
  243. return []
  244. def GetLayerTree(self):
  245. return None
  246. def GetMapDisplay(self):
  247. """Get current map display."""
  248. return None
  249. def GetAllMapDisplays(self):
  250. """Get list of all map displays."""
  251. return []
  252. def GetMapWindow(self):
  253. raise NotImplementedError()
  254. def GetProgress(self):
  255. # TODO: implement some progress with same inface as gui one
  256. # (probably using g.message or similarly to Write... functions)
  257. raise NotImplementedError()
  258. def UpdateCmdHistory(self, cmd):
  259. """There is no history displayed to the user, doing nothing"""
  260. pass