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