GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gpyshell.py
Go to the documentation of this file.
1 """!
2 @package gpyshell.py
3 
4 @brief wxGUI Interactive Python Shell
5 
6 Classes:
7  - PyShellWindow
8 
9 @todo run pyshell and evaluate code in a separate instance of python
10 & design the widget communicate back and forth with it
11 
12 (C) 2011 by the GRASS Development Team
13 This program is free software under the GNU General Public
14 License (>=v2). Read the file COPYING that comes with GRASS
15 for details.
16 
17 @author Martin Landa <landa.martin gmail.com>
18 """
19 
20 import os
21 import sys
22 
23 import wx
24 from wx.py.shell import Shell as PyShell
25 from wx.py.version import VERSION
26 
27 import grass.script as grass
28 
29 class PyShellWindow(wx.Panel):
30  """!Python Shell Window"""
31  def __init__(self, parent, id = wx.ID_ANY, **kwargs):
32  self.parent = parent # GMFrame
33 
34  wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
35 
36  self.intro = _("Welcome to wxGUI Interactive Python Shell %s") % VERSION + "\n\n" + \
37  _("Type %s for more GRASS scripting related information.") % "\"help(grass)\"" + "\n" + \
38  _("Type %s to add raster or vector to the layer tree.") % "\"AddLayer()\"" + "\n\n"
39  self.shell = PyShell(parent = self, id = wx.ID_ANY,
40  introText = self.intro, locals = {'grass' : grass,
41  'AddLayer' : self.AddLayer})
42 
43  sys.displayhook = self._displayhook
44 
45  self.btnClear = wx.Button(self, wx.ID_CLEAR)
46  self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
47  self.btnClear.SetToolTipString(_("Delete all text from the shell"))
48 
49  self._layout()
50 
51  def _displayhook(self, value):
52  print value # do not modify __builtin__._
53 
54  def _layout(self):
55  sizer = wx.BoxSizer(wx.VERTICAL)
56 
57  sizer.Add(item = self.shell, proportion = 1,
58  flag = wx.EXPAND)
59 
60  btnSizer = wx.BoxSizer(wx.HORIZONTAL)
61  btnSizer.Add(item = self.btnClear, proportion = 0,
62  flag = wx.EXPAND | wx.RIGHT, border = 5)
63  sizer.Add(item = btnSizer, proportion = 0,
64  flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
65 
66  sizer.Fit(self)
67  sizer.SetSizeHints(self)
68 
69  self.SetSizer(sizer)
70 
71  self.Fit()
72  self.SetAutoLayout(True)
73  self.Layout()
74 
75  def AddLayer(self, name, ltype = 'auto'):
76  """!Add selected map to the layer tree
77 
78  @param name name of raster/vector map to be added
79  @param type map type ('raster', 'vector', 'auto' for autodetection)
80  """
81  fname = None
82  if ltype == 'raster' or ltype != 'vector':
83  # check for raster
84  fname = grass.find_file(name, element = 'cell')['fullname']
85  if fname:
86  ltype = 'raster'
87  lcmd = 'd.rast'
88 
89  if not fname and (ltype == 'vector' or ltype != 'raster'):
90  # if not found check for vector
91  fname = grass.find_file(name, element = 'vector')['fullname']
92  if fname:
93  ltype = 'vector'
94  lcmd = 'd.vect'
95 
96  if not fname:
97  return _("Raster or vector map <%s> not found") % (name)
98 
99  self.parent.GetLayerTree().AddLayer(ltype = ltype,
100  lname = fname,
101  lchecked = True,
102  lcmd = [lcmd, 'map=%s' % fname])
103  if ltype == 'raster':
104  return _('Raster map <%s> added') % fname
105 
106  return _('Vector map <%s> added') % fname
107 
108  def OnClear(self, event):
109  """!Delete all text from the shell
110  """
111  self.shell.clear()
112  self.shell.showIntro(self.intro)
113  self.shell.prompt()