pyshell.py 4.3 KB

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