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