pyshell.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 gui_core.wrap import Button, ClearButton, IsDark
  21. from gui_core.pystc import SetDarkMode
  22. from core.globalvar import CheckWxVersion
  23. class PyShellWindow(wx.Panel):
  24. """Python Shell Window"""
  25. def __init__(
  26. self, parent, giface, id=wx.ID_ANY, simpleEditorHandler=None, **kwargs
  27. ):
  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.") % '"AddLayer()"'
  37. + "\n\n"
  38. )
  39. shellargs = dict(
  40. parent=self,
  41. id=wx.ID_ANY,
  42. introText=self.intro,
  43. locals={"gs": grass, "AddLayer": self.AddLayer},
  44. )
  45. # useStockId (available since wxPython 4.0.2) should be False on macOS
  46. if sys.platform == "darwin" and CheckWxVersion([4, 0, 2]):
  47. shellargs["useStockId"] = False
  48. self.shell = PyShell(**shellargs)
  49. if IsDark():
  50. SetDarkMode(self.shell)
  51. sys.displayhook = self._displayhook
  52. self.btnClear = ClearButton(self)
  53. self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
  54. self.btnClear.SetToolTip(_("Delete all text from the shell"))
  55. self.simpleEditorHandler = simpleEditorHandler
  56. if simpleEditorHandler:
  57. self.btnSimpleEditor = Button(self, id=wx.ID_ANY, label=_("Simple &editor"))
  58. self.btnSimpleEditor.Bind(wx.EVT_BUTTON, simpleEditorHandler)
  59. self.btnSimpleEditor.SetToolTip(_("Open a simple Python code editor"))
  60. self._layout()
  61. def _displayhook(self, value):
  62. print(value) # do not modify __builtin__._
  63. def _layout(self):
  64. sizer = wx.BoxSizer(wx.VERTICAL)
  65. sizer.Add(self.shell, proportion=1, flag=wx.EXPAND)
  66. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  67. if self.simpleEditorHandler:
  68. btnSizer.Add(
  69. self.btnSimpleEditor,
  70. proportion=0,
  71. flag=wx.EXPAND | wx.LEFT | wx.RIGHT,
  72. border=5,
  73. )
  74. btnSizer.AddStretchSpacer()
  75. btnSizer.Add(self.btnClear, proportion=0, flag=wx.EXPAND, border=5)
  76. sizer.Add(btnSizer, proportion=0, 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(
  104. ltype=ltype, lname=fname, lchecked=True, lcmd=[lcmd, "map=%s" % fname]
  105. )
  106. if ltype == "raster":
  107. return _("Raster map <%s> added") % fname
  108. return _("Vector map <%s> added") % fname
  109. def OnClear(self, event):
  110. """Delete all text from the shell"""
  111. self.shell.clear()
  112. self.shell.showIntro(self.intro)
  113. self.shell.prompt()