pyshell.py 4.5 KB

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