pyshell.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(
  39. self, id=wx.ID_ANY, label=_("Simple &editor"))
  40. self.btnSimpleEditor.Bind(wx.EVT_BUTTON, self.OnSimpleEditor)
  41. self.btnSimpleEditor.SetToolTipString(
  42. _("Open a simple Python code editor"))
  43. self._layout()
  44. def _displayhook(self, value):
  45. print value # do not modify __builtin__._
  46. def _layout(self):
  47. sizer = wx.BoxSizer(wx.VERTICAL)
  48. sizer.Add(item=self.shell, proportion=1,
  49. flag=wx.EXPAND)
  50. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  51. btnSizer.Add(item=self.btnSimpleEditor, proportion=0,
  52. flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5)
  53. btnSizer.AddStretchSpacer()
  54. btnSizer.Add(item=self.btnClear, proportion=0,
  55. flag=wx.EXPAND | wx.ALIGN_RIGHT, border=5)
  56. sizer.Add(item=btnSizer, proportion=0,
  57. flag=wx.ALIGN_RIGHT | wx.ALL | wx.EXPAND, border=5)
  58. sizer.Fit(self)
  59. sizer.SetSizeHints(self)
  60. self.SetSizer(sizer)
  61. self.Fit()
  62. self.SetAutoLayout(True)
  63. self.Layout()
  64. def AddLayer(self, name, ltype='auto'):
  65. """Add selected map to the layer tree
  66. :param name: name of raster/vector map to be added
  67. :param type: map type ('raster', 'vector', 'auto' for autodetection)
  68. """
  69. fname = None
  70. if ltype == 'raster' or ltype != 'vector':
  71. # check for raster
  72. fname = grass.find_file(name, element='cell')['fullname']
  73. if fname:
  74. ltype = 'raster'
  75. lcmd = 'd.rast'
  76. if not fname and (ltype == 'vector' or ltype != 'raster'):
  77. # if not found check for vector
  78. fname = grass.find_file(name, element='vector')['fullname']
  79. if fname:
  80. ltype = 'vector'
  81. lcmd = 'd.vect'
  82. if not fname:
  83. return _("Raster or vector map <%s> not found") % (name)
  84. self.giface.GetLayerTree().AddLayer(ltype=ltype,
  85. lname=fname,
  86. lchecked=True,
  87. lcmd=[lcmd, 'map=%s' % fname])
  88. if ltype == 'raster':
  89. return _('Raster map <%s> added') % fname
  90. return _('Vector map <%s> added') % fname
  91. def OnClear(self, event):
  92. """Delete all text from the shell
  93. """
  94. self.shell.clear()
  95. self.shell.showIntro(self.intro)
  96. self.shell.prompt()
  97. def OnSimpleEditor(self, event):
  98. # import on demand
  99. from gui_core.pyedit import PyEditFrame
  100. # we don't keep track of them and we don't care about open files
  101. # there when closing the main GUI
  102. simpleEditor = PyEditFrame(parent=self, giface=self.giface)
  103. simpleEditor.SetSize(self.parent.GetSize())
  104. simpleEditor.CenterOnScreen()
  105. simpleEditor.Show()