pyshell.py 4.4 KB

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