class StandaloneGrassInterface(GrassInterface): """@implements GrassInterface""" def __init__(self): # Signal when some map is created or updated by a module. # Used for adding/refreshing displayed layers. # attributes: name: map name, ltype: map type, # add: if map should be added to layer tree (questionable attribute) self.mapCreated = Signal("StandaloneGrassInterface.mapCreated") # Signal for communicating current mapset has been switched self.currentMapsetChanged = Signal( "StandaloneGrassInterface.currentMapsetChanged") # Signal for communicating something in current grassdb has changed. # Parameters: # action: required, is one of 'new', 'rename', 'delete' # element: required, can be one of 'grassdb', 'location', 'mapset', 'raster', 'vector' and 'raster_3d' # grassdb: path to grass db, required # location: location name, required # mapset: mapset name, required when element is 'mapset', 'raster', 'vector' or 'raster_3d' # map: map name, required when element is 'raster', 'vector' or 'raster_3d' # newname: new name (of mapset, map), required with action='rename' self.grassdbChanged = Signal("StandaloneGrassInterface.grassdbChanged") # Signal emitted to request updating of map self.updateMap = Signal("StandaloneGrassInterface.updateMap") # Signal emitted when workspace is changed self.workspaceChanged = Signal( "StandaloneGrassInterface.workspaceChanged") # workaround, standalone grass interface should be moved to sep. file from core.gconsole import GConsole, EVT_CMD_OUTPUT, EVT_CMD_PROGRESS self._gconsole = GConsole() self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress) self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput) self._gconsole.writeLog.connect(self.WriteLog) self._gconsole.writeCmdLog.connect(self.WriteCmdLog) self._gconsole.writeWarning.connect(self.WriteWarning) self._gconsole.writeError.connect(self.WriteError) def _onCmdOutput(self, event): """Print command output""" message = event.text style = event.type if style == "warning": self.WriteWarning(message) elif style == "error": self.WriteError(message) else: self.WriteLog(message) event.Skip() def _onCmdProgress(self, event): """Update progress message info""" grass.percent(event.value, 100, 1) event.Skip() def RunCmd( self, command, compReg=True, env=None, skipInterface=False, onDone=None, onPrepare=None, userData=None, addLayer=None, notification=Notification.MAKE_VISIBLE, ): self._gconsole.RunCmd( command=command, compReg=compReg, env=env, skipInterface=skipInterface, onDone=onDone, onPrepare=onPrepare, userData=userData, addLayer=addLayer, notification=notification, ) def Help(self, entry): self._gconsole.RunCmd(["g.manual", "entry=%s" % entry]) def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT): self._write(grass.message, text) def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE): if pid: text = "(" + str(pid) + ") " + text self._write(grass.message, text) def WriteWarning(self, text): self._write(grass.warning, text) def WriteError(self, text): self._write(grass.error, text) def _write(self, function, text): orig = os.getenv("GRASS_MESSAGE_FORMAT") os.environ["GRASS_MESSAGE_FORMAT"] = "standard" function(text) os.environ["GRASS_MESSAGE_FORMAT"] = orig def GetLog(self, err=False): if err: return sys.stdout return sys.stderr def GetLayerList(self): return [] def GetLayerTree(self): return None def GetMapDisplay(self): """Get current map display.""" return None def GetAllMapDisplays(self): """Get list of all map displays.""" return [] def GetMapWindow(self): raise NotImplementedError() def GetProgress(self): # TODO: implement some progress with same inface as gui one # (probably using g.message or similarly to Write... functions) raise NotImplementedError() def UpdateCmdHistory(self, cmd): """There is no history displayed to the user, doing nothing""" pass
class StandaloneGrassInterface(): """@implements GrassInterface""" def __init__(self): # Signal when some map is created or updated by a module. # attributes: name: map name, ltype: map type, # add: if map should be added to layer tree (questionable attribute) self.mapCreated = Signal('StandaloneGrassInterface.mapCreated') # Signal emitted to request updating of map self.updateMap = Signal('StandaloneGrassInterface.updateMap') # workaround, standalone grass interface should be moved to sep. file from core.gconsole import GConsole, \ EVT_CMD_OUTPUT, EVT_CMD_PROGRESS self._gconsole = GConsole() self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress) self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput) self._gconsole.writeLog.connect(self.WriteLog) self._gconsole.writeCmdLog.connect(self.WriteCmdLog) self._gconsole.writeWarning.connect(self.WriteWarning) self._gconsole.writeError.connect(self.WriteError) def _onCmdOutput(self, event): """Print command output""" message = event.text style = event.type if style == 'warning': self.WriteWarning(message) elif style == 'error': self.WriteError(message) else: self.WriteLog(message) event.Skip() def _onCmdProgress(self, event): """Update progress message info""" grass.percent(event.value, 100, 1) event.Skip() def RunCmd(self, command, compReg=True, skipInterface=False, onDone=None, onPrepare=None, userData=None, notification=Notification.MAKE_VISIBLE): self._gconsole.RunCmd(command=command, compReg=compReg, skipInterface=skipInterface, onDone=onDone, onPrepare=onPrepare, userData=userData, notification=notification) def Help(self, entry): self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry]) def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT): self._write(grass.message, text) def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE): if pid: text = '(' + str(pid) + ') ' + text self._write(grass.message, text) def WriteWarning(self, text): self._write(grass.warning, text) def WriteError(self, text): self._write(grass.error, text) def _write(self, function, text): orig = os.getenv("GRASS_MESSAGE_FORMAT") os.environ["GRASS_MESSAGE_FORMAT"] = 'standard' function(text) os.environ["GRASS_MESSAGE_FORMAT"] = orig def GetLayerList(self): raise NotImplementedError() def GetLayerTree(self): return None def GetMapDisplay(self): """Get current map display. """ return None def GetAllMapDisplays(self): """Get list of all map displays. """ return [] def GetMapWindow(self): raise NotImplementedError() def GetProgress(self): # TODO: implement some progress with same inface as gui one # (probably using g.message or similarly to Write... functions) raise NotImplementedError() def UpdateCmdHistory(self, cmd): raise NotImplementedError()