pyshell.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 grass.script.utils import try_remove
  20. from core.utils import _
  21. class PyShellWindow(wx.Panel):
  22. """Python Shell Window"""
  23. def __init__(self, parent, giface, id = wx.ID_ANY, **kwargs):
  24. self.parent = parent
  25. self.giface = giface
  26. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  27. self.intro = _("Welcome to wxGUI Interactive Python Shell %s") % VERSION + "\n\n" + \
  28. _("Type %s for more GRASS scripting related information.") % "\"help(grass)\"" + "\n" + \
  29. _("Type %s to add raster or vector to the layer tree.") % "\"AddLayer()\"" + "\n\n"
  30. self.shell = PyShell(parent = self, id = wx.ID_ANY,
  31. introText = self.intro,
  32. locals={'grass': grass,
  33. 'AddLayer': self.AddLayer})
  34. sys.displayhook = self._displayhook
  35. self.btnClear = wx.Button(self, wx.ID_CLEAR)
  36. self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
  37. self.btnClear.SetToolTipString(_("Delete all text from the shell"))
  38. self.btnSimpleEditor = wx.Button(self, id=wx.ID_ANY, label=_("Simple &editor"))
  39. self.btnSimpleEditor.Bind(wx.EVT_BUTTON, self.OnSimpleEditor)
  40. self.btnSimpleEditor.SetToolTipString(_("Open a simple Python code editor"))
  41. self._layout()
  42. def _displayhook(self, value):
  43. print value # do not modify __builtin__._
  44. def _layout(self):
  45. sizer = wx.BoxSizer(wx.VERTICAL)
  46. sizer.Add(item = self.shell, proportion = 1,
  47. flag = wx.EXPAND)
  48. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  49. btnSizer.Add(item=self.btnSimpleEditor, proportion=0,
  50. flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5)
  51. btnSizer.AddStretchSpacer()
  52. btnSizer.Add(item = self.btnClear, proportion = 0,
  53. flag = wx.EXPAND | wx.ALIGN_RIGHT, border = 5)
  54. sizer.Add(item = btnSizer, proportion = 0,
  55. flag = wx.ALIGN_RIGHT | wx.ALL | wx.EXPAND, border = 5)
  56. sizer.Fit(self)
  57. sizer.SetSizeHints(self)
  58. self.SetSizer(sizer)
  59. self.Fit()
  60. self.SetAutoLayout(True)
  61. self.Layout()
  62. def AddLayer(self, name, ltype = 'auto'):
  63. """Add selected map to the layer tree
  64. :param name: name of raster/vector map to be added
  65. :param type: map type ('raster', 'vector', 'auto' for autodetection)
  66. """
  67. fname = None
  68. if ltype == 'raster' or ltype != 'vector':
  69. # check for raster
  70. fname = grass.find_file(name, element = 'cell')['fullname']
  71. if fname:
  72. ltype = 'raster'
  73. lcmd = 'd.rast'
  74. if not fname and (ltype == 'vector' or ltype != 'raster'):
  75. # if not found check for vector
  76. fname = grass.find_file(name, element = 'vector')['fullname']
  77. if fname:
  78. ltype = 'vector'
  79. lcmd = 'd.vect'
  80. if not fname:
  81. return _("Raster or vector map <%s> not found") % (name)
  82. self.giface.GetLayerTree().AddLayer(ltype = ltype,
  83. lname = fname,
  84. lchecked = True,
  85. lcmd = [lcmd, 'map=%s' % fname])
  86. if ltype == 'raster':
  87. return _('Raster map <%s> added') % fname
  88. return _('Vector map <%s> added') % fname
  89. def OnClear(self, event):
  90. """Delete all text from the shell
  91. """
  92. self.shell.clear()
  93. self.shell.showIntro(self.intro)
  94. self.shell.prompt()
  95. def OnSimpleEditor(self, event):
  96. # import on demand
  97. from gui_core.pyedit import PyEditFrame
  98. # we don't keep track of them and we don't care about open files
  99. # there when closing the main GUI
  100. simpleEditor = PyEditFrame(parent=self, giface=self.giface)
  101. simpleEditor.SetSize(self.parent.GetSize())
  102. simpleEditor.CenterOnParent()
  103. simpleEditor.Show()