def addNode(parent, data, model): for k, v in data.iteritems(): if isinstance(v, str): k = DecodeString(k) if isinstance(v, dict): node = model.AppendNode(parent=parent, label=k) addNode(parent=node, data=v, model=model) else: if not isinstance(v, basestring): v = str(v) elif isinstance(v, str): v = DecodeString(v) node = model.AppendNode(parent=parent, label=k, data={column: v})
def print_error(msg, type): """Redirect stderr""" global log if log: if sys.version_info.major >= 3: msg = DecodeString(msg.data) log.write(msg + os.linesep) else: print(msg) global last_error last_error += " " + msg return 0
def _readGisRC(self): """Read variables from $HOME/.grass7/rc file """ grassrc = {} gisrc = os.getenv("GISRC") if gisrc and os.path.isfile(gisrc): try: rc = open(gisrc, "r") for line in rc.readlines(): try: key, val = line.split(":", 1) except ValueError as e: sys.stderr.write( _('Invalid line in GISRC file (%s):%s\n' % (e, line))) grassrc[key.strip()] = DecodeString(val.strip()) finally: rc.close() return grassrc
def read_gisrc(): """Read variables from a current GISRC file Returns a dictionary representation of the file content. """ grassrc = {} gisrc = os.getenv("GISRC") if gisrc and os.path.isfile(gisrc): try: rc = open(gisrc, "r") for line in rc.readlines(): try: key, val = line.split(":", 1) except ValueError as e: sys.stderr.write( _('Invalid line in GISRC file (%s):%s\n' % (e, line))) grassrc[key.strip()] = DecodeString(val.strip()) finally: rc.close() return grassrc
def read2_command(*args, **kwargs): kwargs["stdout"] = gcore.PIPE kwargs["stderr"] = gcore.PIPE ps = gcore.start_command(*args, **kwargs) stdout, stderr = ps.communicate() return ps.returncode, DecodeString(stdout), DecodeString(stderr)
def OnChar(self, event): """Key char capture for autocompletion, calltips, and command history .. todo:: event.ControlDown() for manual autocomplete """ # keycodes used: "." = 46, "=" = 61, "-" = 45 pos = self.GetCurrentPos() # complete command after pressing '.' if event.GetKeyCode() == 46: self.autoCompList = list() entry = self.GetTextLeft() self.InsertText(pos, '.') self.CharRight() self.toComplete = self.EntityToComplete() try: if self.toComplete['entity'] == 'command': for command in globalvar.grassCmd: try: if command.find(self.toComplete['cmd']) == 0: dotNumber = list(self.toComplete['cmd']).count('.') self.autoCompList.append(command.split('.',dotNumber)[-1]) except UnicodeDecodeError as e: # TODO: fix it sys.stderr.write(DecodeString(command) + ": " + unicode(e)) except (KeyError, TypeError): return self.ShowList() # complete flags after pressing '-' elif (event.GetKeyCode() == 45) \ or event.GetKeyCode() == wx.WXK_NUMPAD_SUBTRACT \ or event.GetKeyCode() == wx.WXK_SUBTRACT: self.autoCompList = list() entry = self.GetTextLeft() self.InsertText(pos, '-') self.CharRight() self.toComplete = self.EntityToComplete() if self.toComplete['entity'] == 'flags' and self.cmdDesc: if self.GetTextLeft()[-2:] == ' -': # complete e.g. --quite for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append(flag['name']) else: for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) > 1: self.autoCompList.append(flag['name']) self.ShowList() # complete map or values after parameter elif event.GetKeyCode() == 61: self.autoCompList = list() self.InsertText(pos, '=') self.CharRight() self.toComplete = self.EntityToComplete() if self.toComplete['entity'] == 'raster map': self.autoCompList = self.mapList['raster'] elif self.toComplete['entity'] == 'vector map': self.autoCompList = self.mapList['vector'] elif self.toComplete['entity'] == 'param values': param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =') self.autoCompList = self.cmdDesc.get_param(param)['values'] self.ShowList() # complete mapset ('@') elif event.GetKeyCode() == 64: self.autoCompList = list() self.InsertText(pos, '@') self.CharRight() self.toComplete = self.EntityToComplete() if self.toComplete['entity'] == 'mapsets': self.autoCompList = self.mapsetList self.ShowList() # complete after pressing CTRL + Space elif event.GetKeyCode() == wx.WXK_SPACE and event.ControlDown(): self.autoCompList = list() self.toComplete = self.EntityToComplete() #complete command if self.toComplete['entity'] == 'command': for command in globalvar.grassCmd: if command.find(self.toComplete['cmd']) == 0: dotNumber = list(self.toComplete['cmd']).count('.') self.autoCompList.append(command.split('.',dotNumber)[-1]) # complete flags in such situations (| is cursor): # r.colors -| ...w, q, l # r.colors -w| ...w, q, l elif self.toComplete['entity'] == 'flags' and self.cmdDesc: for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append(flag['name']) # complete parameters in such situations (| is cursor): # r.colors -w | ...color, map, rast, rules # r.colors col| ...color elif self.toComplete['entity'] == 'params' and self.cmdDesc: for param in self.cmdDesc.get_options()['params']: if param['name'].find(self.GetWordLeft(withDelimiter=False)) == 0: self.autoCompList.append(param['name']) # complete flags or parameters in such situations (| is cursor): # r.colors | ...-w, -q, -l, color, map, rast, rules # r.colors color=grey | ...-w, -q, -l, color, map, rast, rules elif self.toComplete['entity'] == 'params+flags' and self.cmdDesc: self.autoCompList = list() for param in self.cmdDesc.get_options()['params']: self.autoCompList.append(param['name']) for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append('-' + flag['name']) else: self.autoCompList.append('--' + flag['name']) self.ShowList() # complete map or values after parameter # r.buffer input=| ...list of raster maps # r.buffer units=| ... feet, kilometers, ... elif self.toComplete['entity'] == 'raster map': self.autoCompList = list() self.autoCompList = self.mapList['raster'] elif self.toComplete['entity'] == 'vector map': self.autoCompList = list() self.autoCompList = self.mapList['vector'] elif self.toComplete['entity'] == 'param values': self.autoCompList = list() param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =') self.autoCompList = self.cmdDesc.get_param(param)['values'] self.ShowList() elif event.GetKeyCode() == wx.WXK_SPACE: items = self.GetTextLeft().split() if len(items) == 1: cmd = items[0].strip() if cmd in globalvar.grassCmd and \ (not self.cmdDesc or cmd != self.cmdDesc.get_name()): try: self.cmdDesc = gtask.parse_interface(cmd) except IOError: self.cmdDesc = None event.Skip() else: event.Skip()