GRASS Programmer's Manual  6.4.2(2012)
globalvar.py
Go to the documentation of this file.
00001 """!
00002 @package global.py
00003 
00004 @brief Global variables
00005 
00006 This module provide the space for global variables
00007 used in the code.
00008 
00009 (C) 2007-2010 by the GRASS Development Team
00010 
00011 This program is free software under the GNU General Public License
00012 (>=v2). Read the file COPYING that comes with GRASS for details.
00013 
00014 @author Martin Landa <landa.martin gmail.com>
00015 """
00016 
00017 import os
00018 import sys
00019 import locale
00020 
00021 if not os.getenv("GISBASE"):
00022     sys.exit("GRASS is not running. Exiting...")
00023 ### i18N
00024 import gettext
00025 gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
00026 
00027 # path to python scripts
00028 ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
00029 ETCICONDIR = os.path.join(os.getenv("GISBASE"), "etc", "gui", "icons")
00030 ETCWXDIR = os.path.join(ETCDIR, "wxpython")
00031 ETCIMGDIR = os.path.join(ETCDIR, "gui", "images")
00032 
00033 sys.path.append(os.path.join(ETCDIR, "python"))
00034 import grass.script as grass
00035 
00036 def CheckWxVersion(version = [2, 8, 11, 0]):
00037     """!Check wx version"""
00038     ver = wx.version().split(' ')[0]
00039     if map(int, ver.split('.')) < version:
00040         return False
00041     
00042     return True
00043 
00044 def CheckForWx():
00045     """!Try to import wx module and check its version"""
00046     if 'wx' in sys.modules.keys():
00047         return
00048     
00049     minVersion = [2, 8, 1, 1]
00050     try:
00051         try:
00052             import wxversion
00053         except ImportError, e:
00054             raise ImportError(e)
00055         # wxversion.select(str(minVersion[0]) + '.' + str(minVersion[1]))
00056         wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
00057         import wx
00058         version = wx.version().split(' ')[0]
00059         
00060         if map(int, version.split('.')) < minVersion:
00061             raise ValueError('Your wxPython version is %s.%s.%s.%s' % tuple(version.split('.')))
00062 
00063     except ImportError, e:
00064         print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
00065         sys.exit(1)
00066     except (ValueError, wxversion.VersionError), e:
00067         print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(minVersion) + \
00068             '%s.' % (str(e))
00069         sys.exit(1)
00070     except locale.Error, e:
00071         print >> sys.stderr, "Unable to set locale:", e
00072         os.environ['LC_ALL'] = ''
00073     
00074 if not os.getenv("GRASS_WXBUNDLED"):
00075     CheckForWx()
00076 import wx
00077 import wx.lib.flatnotebook as FN
00078 
00079 """
00080 Query layer (generated for example by selecting item in the Attribute Table Manager)
00081 Deleted automatically on re-render action
00082 """
00083 # temporal query layer (removed on re-render action)
00084 QUERYLAYER = 'qlayer'
00085 
00086 """!Style definition for FlatNotebook pages"""
00087 FNPageStyle = FN.FNB_VC8 | \
00088     FN.FNB_BACKGROUND_GRADIENT | \
00089     FN.FNB_NODRAG | \
00090     FN.FNB_TABS_BORDER_SIMPLE 
00091 
00092 FNPageDStyle = FN.FNB_FANCY_TABS | \
00093     FN.FNB_BOTTOM | \
00094     FN.FNB_NO_NAV_BUTTONS | \
00095     FN.FNB_NO_X_BUTTON
00096 
00097 FNPageColor = wx.Colour(125,200,175)
00098 
00099 """!Dialog widget dimension"""
00100 DIALOG_SPIN_SIZE = (150, -1)
00101 DIALOG_COMBOBOX_SIZE = (300, -1)
00102 DIALOG_GSELECT_SIZE = (400, -1)
00103 DIALOG_TEXTCTRL_SIZE = (400, -1)
00104 DIALOG_LAYER_SIZE = (100, -1)
00105 DIALOG_COLOR_SIZE = (30, 30)
00106 
00107 MAP_WINDOW_SIZE = (800, 600)
00108 HIST_WINDOW_SIZE = (500, 350)
00109 GM_WINDOW_SIZE = (500, 600)
00110 
00111 MAP_DISPLAY_STATUSBAR_MODE = [_("Coordinates"),
00112                               _("Extent"),
00113                               _("Comp. region"),
00114                               _("Show comp. extent"),
00115                               _("Display mode"),
00116                               _("Display geometry"),
00117                               _("Map scale"),
00118                               _("Go to"),
00119                               _("Projection"),]
00120 
00121 """!File name extension binaries/scripts"""
00122 if sys.platform == 'win32':
00123     EXT_BIN = '.exe'
00124     EXT_SCT = '.bat'
00125 else:
00126     EXT_BIN = ''
00127     EXT_SCT = ''
00128 
00129 def GetGRASSCmds(bin = True, scripts = True, gui_scripts = True):
00130     """!Create list of available GRASS commands to use when parsing
00131     string from the command line
00132 
00133     @param bin True to include executable into list
00134     @param scripts True to include scripts into list
00135     @param gui_scripts True to include GUI scripts into list
00136     """
00137     gisbase = os.environ['GISBASE']
00138     cmd = list()
00139     if bin:
00140         for executable in os.listdir(os.path.join(gisbase, 'bin')):
00141             ext = os.path.splitext(executable)[1]
00142             if not EXT_BIN or \
00143                     ext in (EXT_BIN, EXT_SCT):
00144                 cmd.append(executable)
00145         
00146         # add special call for setting vector colors
00147         cmd.append('vcolors')
00148     if scripts and sys.platform != "win32":
00149         cmd = cmd + os.listdir(os.path.join(gisbase, 'scripts')) 
00150     if gui_scripts:
00151         os.environ["PATH"] = os.getenv("PATH") + os.pathsep + os.path.join(gisbase, 'etc', 'gui', 'scripts')
00152         os.environ["PATH"] = os.getenv("PATH") + os.pathsep + os.path.join(gisbase, 'etc', 'wxpython', 'scripts')
00153         cmd = cmd + os.listdir(os.path.join(gisbase, 'etc', 'gui', 'scripts'))
00154     
00155     if os.getenv('GRASS_ADDON_PATH'):
00156         for path in os.getenv('GRASS_ADDON_PATH').split(os.pathsep):
00157             if not os.path.exists(path) or not os.path.isdir(path):
00158                 continue
00159             for fname in os.listdir(path):
00160                 name, ext = os.path.splitext(fname)
00161                 if bin:
00162                     if ext == EXT_BIN:
00163                         cmd.append(name)
00164                 if scripts:
00165                     if ext == EXT_SCT:
00166                         cmd.append(name)
00167                     elif ext == '.py':
00168                         cmd.append(fname)
00169         
00170     if sys.platform == 'win32':
00171         for idx in range(len(cmd)):
00172             name, ext = os.path.splitext(cmd[idx])
00173             if ext in (EXT_BIN, EXT_SCT):
00174                 cmd[idx] = name
00175     
00176     return cmd
00177 
00178 """@brief Collected GRASS-relared binaries/scripts"""
00179 grassCmd = {}
00180 grassCmd['all'] = GetGRASSCmds()
00181 grassCmd['script'] = GetGRASSCmds(bin = False, gui_scripts = False)
00182 
00183 """@Toolbar icon size"""
00184 toolbarSize = (24, 24)
00185 
00186 """@Is g.mlist available?"""
00187 if 'g.mlist' in grassCmd['all']:
00188     have_mlist = True
00189 else:
00190     have_mlist = False
00191 
00192 """@Check version of wxPython, use agwStyle for 2.8.11+"""
00193 hasAgw = CheckWxVersion()
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines