pyshell.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """!
  2. @package lmgr.pyshell
  3. @brief wxGUI Interactive Python Shell for Layer Manager
  4. Classes:
  5. - pyshell::PyShellWindow
  6. @todo Run pyshell and evaluate code in a separate instance of python &
  7. design the widget communicate back and forth with it
  8. (C) 2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Martin Landa <landa.martin gmail.com>
  12. """
  13. import sys
  14. import wx
  15. from wx.py.shell import Shell as PyShell
  16. from wx.py.version import VERSION
  17. import grass.script as grass
  18. import grass.pygrass as pygrass
  19. import grass.pygrass.modules as pgmodules
  20. class PyShellWindow(wx.Panel):
  21. """!Python Shell Window"""
  22. def __init__(self, parent, id = wx.ID_ANY, **kwargs):
  23. self.parent = parent # GMFrame
  24. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  25. self.intro = _("Welcome to wxGUI Interactive Python Shell %s") % VERSION + "\n\n" + \
  26. _("Type %s for more GRASS scripting related information.") % "\"help(grass)\"" + "\n" + \
  27. _("Type %s to add raster or vector to the layer tree.") % "\"AddLayer()\"" + "\n\n"
  28. self.shell = PyShell(parent = self, id = wx.ID_ANY,
  29. introText = self.intro,
  30. locals={'grass': grass,
  31. 'pygrass': pygrass,
  32. 'r': pgmodules.shortcuts.raster,
  33. 'r3': pgmodules.shortcuts.raster3D,
  34. 'v': pgmodules.shortcuts.vector,
  35. 'i': pgmodules.shortcuts.imagery,
  36. 'db': pgmodules.shortcuts.database,
  37. 'g': pgmodules.shortcuts.general,
  38. 'ps': pgmodules.shortcuts.postscript,
  39. 't': pgmodules.shortcuts.temporal,
  40. 'giface': self.parent._giface, # for experimetal reasons only
  41. 'AddLayer': self.AddLayer})
  42. sys.displayhook = self._displayhook
  43. self.btnClear = wx.Button(self, wx.ID_CLEAR)
  44. self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
  45. self.btnClear.SetToolTipString(_("Delete all text from the shell"))
  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(item = self.shell, proportion = 1,
  52. flag = wx.EXPAND)
  53. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  54. btnSizer.Add(item = self.btnClear, proportion = 0,
  55. flag = wx.EXPAND | wx.RIGHT, border = 5)
  56. sizer.Add(item = btnSizer, proportion = 0,
  57. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  58. sizer.Fit(self)
  59. sizer.SetSizeHints(self)
  60. self.SetSizer(sizer)
  61. self.Fit()
  62. self.SetAutoLayout(True)
  63. self.Layout()
  64. def AddLayer(self, name, ltype = 'auto'):
  65. """!Add selected map to the layer tree
  66. @param name name of raster/vector map to be added
  67. @param type map type ('raster', 'vector', 'auto' for autodetection)
  68. """
  69. fname = None
  70. if ltype == 'raster' or ltype != 'vector':
  71. # check for raster
  72. fname = grass.find_file(name, element = 'cell')['fullname']
  73. if fname:
  74. ltype = 'raster'
  75. lcmd = 'd.rast'
  76. if not fname and (ltype == 'vector' or ltype != 'raster'):
  77. # if not found check for vector
  78. fname = grass.find_file(name, element = 'vector')['fullname']
  79. if fname:
  80. ltype = 'vector'
  81. lcmd = 'd.vect'
  82. if not fname:
  83. return _("Raster or vector map <%s> not found") % (name)
  84. self.parent.GetLayerTree().AddLayer(ltype = ltype,
  85. lname = fname,
  86. lchecked = True,
  87. lcmd = [lcmd, 'map=%s' % fname])
  88. if ltype == 'raster':
  89. return _('Raster map <%s> added') % fname
  90. return _('Vector map <%s> added') % fname
  91. def OnClear(self, event):
  92. """!Delete all text from the shell
  93. """
  94. self.shell.clear()
  95. self.shell.showIntro(self.intro)
  96. self.shell.prompt()