pyshell.py 5.0 KB

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