pyshell.py 3.8 KB

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