gpyshell.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """!
  2. @package gpyshell.py
  3. @brief wxGUI Interactive Python Shell
  4. Classes:
  5. - PyShellWindow
  6. @todo run pyshell and evaluate code in a separate instance of python
  7. & design the widget communicate back and forth with it
  8. (C) 2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. @author Martin Landa <landa.martin gmail.com>
  13. """
  14. import os
  15. import sys
  16. import wx
  17. from wx.py.shell import Shell as PyShell
  18. from wx.py.version import VERSION
  19. import grass.script as grass
  20. class PyShellWindow(wx.Panel):
  21. """!Python Shell Window"""
  22. def __init__(self, parent, id = wx.ID_ANY, **kwargs):
  23. self.parent = parent # GMFrame
  24. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  25. self.intro = _("Welcome to wxGUI Interactive Python Shell %s") % VERSION + "\n\n" + \
  26. _("Type %s for more GRASS scripting related information.") % "\"help(grass)\"" + "\n" + \
  27. _("Type %s to add raster or vector to the layer tree.") % "\"AddLayer()\"" + "\n\n"
  28. self.shell = PyShell(parent = self, id = wx.ID_ANY,
  29. introText = self.intro, locals = {'grass' : grass,
  30. 'AddLayer' : self.AddLayer})
  31. sys.displayhook = self._displayhook
  32. self.btnClear = wx.Button(self, wx.ID_CLEAR)
  33. self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
  34. self.btnClear.SetToolTipString(_("Delete all text from the shell"))
  35. self._layout()
  36. def _displayhook(self, value):
  37. print value # do not modify __builtin__._
  38. def _layout(self):
  39. sizer = wx.BoxSizer(wx.VERTICAL)
  40. sizer.Add(item = self.shell, proportion = 1,
  41. flag = wx.EXPAND)
  42. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  43. btnSizer.Add(item = self.btnClear, proportion = 0,
  44. flag = wx.EXPAND | wx.RIGHT, border = 5)
  45. sizer.Add(item = btnSizer, proportion = 0,
  46. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  47. sizer.Fit(self)
  48. sizer.SetSizeHints(self)
  49. self.SetSizer(sizer)
  50. self.Fit()
  51. self.SetAutoLayout(True)
  52. self.Layout()
  53. def AddLayer(self, name, ltype = 'auto'):
  54. """!Add selected map to the layer tree
  55. @param name name of raster/vector map to be added
  56. @param type map type ('raster', 'vector', 'auto' for autodetection)
  57. """
  58. fname = None
  59. if ltype == 'raster' or ltype != 'vector':
  60. # check for raster
  61. fname = grass.find_file(name, element = 'cell')['fullname']
  62. if fname:
  63. ltype = 'raster'
  64. lcmd = 'd.rast'
  65. if not fname and (ltype == 'vector' or ltype != 'raster'):
  66. # if not found check for vector
  67. fname = grass.find_file(name, element = 'vector')['fullname']
  68. if fname:
  69. ltype = 'vector'
  70. lcmd = 'd.vect'
  71. if not fname:
  72. return _("Raster or vector map <%s> not found") % (name)
  73. self.parent.GetLayerTree().AddLayer(ltype = ltype,
  74. lname = fname,
  75. lchecked = True,
  76. lcmd = [lcmd, 'map=%s' % fname])
  77. if ltype == 'raster':
  78. return _('Raster map <%s> added') % fname
  79. return _('Vector map <%s> added') % fname
  80. def OnClear(self, event):
  81. """!Delete all text from the shell
  82. """
  83. self.shell.clear()
  84. self.shell.showIntro(self.intro)
  85. self.shell.prompt()