pyshell.py 3.9 KB

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