GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
pyshell.py
Go to the documentation of this file.
00001 """!
00002 @package lmgr.pyshell
00003 
00004 @brief wxGUI Interactive Python Shell for Layer Manager
00005 
00006 Classes:
00007  - pyshell::PyShellWindow
00008 
00009 @todo Run pyshell and evaluate code in a separate instance of python &
00010 design the widget communicate back and forth with it
00011 
00012 (C) 2011 by the GRASS Development Team
00013 
00014 This program is free software under the GNU General Public License
00015 (>=v2). Read the file COPYING that comes with GRASS for details.
00016 
00017 @author Martin Landa <landa.martin gmail.com>
00018 """
00019 
00020 import sys
00021 
00022 import wx
00023 from wx.py.shell   import Shell as PyShell
00024 from wx.py.version import VERSION
00025 
00026 import grass.script as grass
00027 
00028 class PyShellWindow(wx.Panel):
00029     """!Python Shell Window"""
00030     def __init__(self, parent, id = wx.ID_ANY, **kwargs):
00031         self.parent = parent # GMFrame
00032         
00033         wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
00034         
00035         self.intro = _("Welcome to wxGUI Interactive Python Shell %s") % VERSION + "\n\n" + \
00036             _("Type %s for more GRASS scripting related information.") % "\"help(grass)\"" + "\n" + \
00037             _("Type %s to add raster or vector to the layer tree.") % "\"AddLayer()\"" + "\n\n"
00038         self.shell = PyShell(parent = self, id = wx.ID_ANY,
00039                              introText = self.intro, locals = {'grass' : grass,
00040                                                                'AddLayer' : self.AddLayer})
00041         
00042         sys.displayhook = self._displayhook
00043         
00044         self.btnClear = wx.Button(self, wx.ID_CLEAR)
00045         self.btnClear.Bind(wx.EVT_BUTTON, self.OnClear)
00046         self.btnClear.SetToolTipString(_("Delete all text from the shell"))
00047                 
00048         self._layout()
00049         
00050     def _displayhook(self, value):
00051         print value # do not modify __builtin__._
00052         
00053     def _layout(self):
00054         sizer = wx.BoxSizer(wx.VERTICAL)
00055         
00056         sizer.Add(item = self.shell, proportion = 1,
00057                   flag = wx.EXPAND)
00058         
00059         btnSizer = wx.BoxSizer(wx.HORIZONTAL)
00060         btnSizer.Add(item = self.btnClear, proportion = 0,
00061                      flag = wx.EXPAND | wx.RIGHT, border = 5)
00062         sizer.Add(item = btnSizer, proportion = 0,
00063                   flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
00064         
00065         sizer.Fit(self)
00066         sizer.SetSizeHints(self)
00067         
00068         self.SetSizer(sizer)
00069         
00070         self.Fit()
00071         self.SetAutoLayout(True)        
00072         self.Layout()
00073 
00074     def AddLayer(self, name, ltype = 'auto'):
00075         """!Add selected map to the layer tree
00076 
00077         @param name name of raster/vector map to be added
00078         @param type map type ('raster', 'vector', 'auto' for autodetection)
00079         """
00080         fname = None
00081         if ltype == 'raster' or ltype != 'vector':
00082             # check for raster
00083             fname = grass.find_file(name, element = 'cell')['fullname']
00084             if fname:
00085                 ltype = 'raster'
00086                 lcmd = 'd.rast'
00087         
00088         if not fname and (ltype == 'vector' or ltype != 'raster'):
00089             # if not found check for vector
00090             fname = grass.find_file(name, element = 'vector')['fullname']
00091             if fname:
00092                 ltype = 'vector'
00093                 lcmd = 'd.vect'
00094         
00095         if not fname:
00096             return _("Raster or vector map <%s> not found") % (name)
00097         
00098         self.parent.GetLayerTree().AddLayer(ltype = ltype,
00099                                             lname = fname,
00100                                             lchecked = True,
00101                                             lcmd = [lcmd, 'map=%s' % fname])
00102         if ltype == 'raster':
00103             return _('Raster map <%s> added') % fname
00104         
00105         return _('Vector map <%s> added') % fname
00106     
00107     def OnClear(self, event):
00108         """!Delete all text from the shell
00109         """
00110         self.shell.clear()
00111         self.shell.showIntro(self.intro)
00112         self.shell.prompt()