pyshell.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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,
  28. locals={'grass': grass,
  29. 'AddLayer': self.AddLayer})
  30. sys.displayhook = self._displayhook
  31. self.btnClear = wx.Button(self, wx.ID_CLEAR)
  32. self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
  33. self.btnClear.SetToolTipString(_("Delete all text from the shell"))
  34. self._layout()
  35. def _displayhook(self, value):
  36. print value # do not modify __builtin__._
  37. def _layout(self):
  38. sizer = wx.BoxSizer(wx.VERTICAL)
  39. sizer.Add(item = self.shell, proportion = 1,
  40. flag = wx.EXPAND)
  41. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  42. btnSizer.Add(item = self.btnClear, proportion = 0,
  43. flag = wx.EXPAND | wx.RIGHT, border = 5)
  44. sizer.Add(item = btnSizer, proportion = 0,
  45. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  46. sizer.Fit(self)
  47. sizer.SetSizeHints(self)
  48. self.SetSizer(sizer)
  49. self.Fit()
  50. self.SetAutoLayout(True)
  51. self.Layout()
  52. def AddLayer(self, name, ltype = 'auto'):
  53. """!Add selected map to the layer tree
  54. @param name name of raster/vector map to be added
  55. @param type map type ('raster', 'vector', 'auto' for autodetection)
  56. """
  57. fname = None
  58. if ltype == 'raster' or ltype != 'vector':
  59. # check for raster
  60. fname = grass.find_file(name, element = 'cell')['fullname']
  61. if fname:
  62. ltype = 'raster'
  63. lcmd = 'd.rast'
  64. if not fname and (ltype == 'vector' or ltype != 'raster'):
  65. # if not found check for vector
  66. fname = grass.find_file(name, element = 'vector')['fullname']
  67. if fname:
  68. ltype = 'vector'
  69. lcmd = 'd.vect'
  70. if not fname:
  71. return _("Raster or vector map <%s> not found") % (name)
  72. self.parent.GetLayerTree().AddLayer(ltype = ltype,
  73. lname = fname,
  74. lchecked = True,
  75. lcmd = [lcmd, 'map=%s' % fname])
  76. if ltype == 'raster':
  77. return _('Raster map <%s> added') % fname
  78. return _('Vector map <%s> added') % fname
  79. def OnClear(self, event):
  80. """!Delete all text from the shell
  81. """
  82. self.shell.clear()
  83. self.shell.showIntro(self.intro)
  84. self.shell.prompt()