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