GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
globalvar.py
Go to the documentation of this file.
1 """!
2 @package global.py
3 
4 @brief Global variables
5 
6 This module provide the space for global variables
7 used in the code.
8 
9 (C) 2007-2010 by the GRASS Development Team
10 
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
13 
14 @author Martin Landa <landa.martin gmail.com>
15 """
16 
17 import os
18 import sys
19 import locale
20 
21 if not os.getenv("GISBASE"):
22  sys.exit("GRASS is not running. Exiting...")
23 ### i18N
24 import gettext
25 gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
26 
27 # path to python scripts
28 ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
29 ETCICONDIR = os.path.join(os.getenv("GISBASE"), "etc", "gui", "icons")
30 ETCWXDIR = os.path.join(ETCDIR, "wxpython")
31 ETCIMGDIR = os.path.join(ETCDIR, "gui", "images")
32 
33 sys.path.append(os.path.join(ETCDIR, "python"))
34 import grass.script as grass
35 
36 def CheckWxVersion(version = [2, 8, 11, 0]):
37  """!Check wx version"""
38  ver = wx.version().split(' ')[0]
39  if map(int, ver.split('.')) < version:
40  return False
41 
42  return True
43 
44 def CheckForWx():
45  """!Try to import wx module and check its version"""
46  if 'wx' in sys.modules.keys():
47  return
48 
49  minVersion = [2, 8, 1, 1]
50  try:
51  try:
52  import wxversion
53  except ImportError, e:
54  raise ImportError(e)
55  # wxversion.select(str(minVersion[0]) + '.' + str(minVersion[1]))
56  wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
57  import wx
58  version = wx.version().split(' ')[0]
59 
60  if map(int, version.split('.')) < minVersion:
61  raise ValueError('Your wxPython version is %s.%s.%s.%s' % tuple(version.split('.')))
62 
63  except ImportError, e:
64  print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
65  sys.exit(1)
66  except (ValueError, wxversion.VersionError), e:
67  print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(minVersion) + \
68  '%s.' % (str(e))
69  sys.exit(1)
70  except locale.Error, e:
71  print >> sys.stderr, "Unable to set locale:", e
72  os.environ['LC_ALL'] = ''
73 
74 if not os.getenv("GRASS_WXBUNDLED"):
75  CheckForWx()
76 import wx
77 import wx.lib.flatnotebook as FN
78 
79 """
80 Query layer (generated for example by selecting item in the Attribute Table Manager)
81 Deleted automatically on re-render action
82 """
83 # temporal query layer (removed on re-render action)
84 QUERYLAYER = 'qlayer'
85 
86 """!Style definition for FlatNotebook pages"""
87 FNPageStyle = FN.FNB_VC8 | \
88  FN.FNB_BACKGROUND_GRADIENT | \
89  FN.FNB_NODRAG | \
90  FN.FNB_TABS_BORDER_SIMPLE
91 
92 FNPageDStyle = FN.FNB_FANCY_TABS | \
93  FN.FNB_BOTTOM | \
94  FN.FNB_NO_NAV_BUTTONS | \
95  FN.FNB_NO_X_BUTTON
96 
97 FNPageColor = wx.Colour(125,200,175)
98 
99 """!Dialog widget dimension"""
100 DIALOG_SPIN_SIZE = (150, -1)
101 DIALOG_COMBOBOX_SIZE = (300, -1)
102 DIALOG_GSELECT_SIZE = (400, -1)
103 DIALOG_TEXTCTRL_SIZE = (400, -1)
104 DIALOG_LAYER_SIZE = (100, -1)
105 DIALOG_COLOR_SIZE = (30, 30)
106 
107 MAP_WINDOW_SIZE = (800, 600)
108 HIST_WINDOW_SIZE = (500, 350)
109 GM_WINDOW_SIZE = (500, 600)
110 
111 MAP_DISPLAY_STATUSBAR_MODE = [_("Coordinates"),
112  _("Extent"),
113  _("Comp. region"),
114  _("Show comp. extent"),
115  _("Display mode"),
116  _("Display geometry"),
117  _("Map scale"),
118  _("Go to"),
119  _("Projection"),]
120 
121 """!File name extension binaries/scripts"""
122 if sys.platform == 'win32':
123  EXT_BIN = '.exe'
124  EXT_SCT = '.bat'
125 else:
126  EXT_BIN = ''
127  EXT_SCT = ''
128 
129 def GetGRASSCmds(bin = True, scripts = True, gui_scripts = True):
130  """!Create list of available GRASS commands to use when parsing
131  string from the command line
132 
133  @param bin True to include executable into list
134  @param scripts True to include scripts into list
135  @param gui_scripts True to include GUI scripts into list
136  """
137  gisbase = os.environ['GISBASE']
138  cmd = list()
139  if bin:
140  for executable in os.listdir(os.path.join(gisbase, 'bin')):
141  ext = os.path.splitext(executable)[1]
142  if not EXT_BIN or \
143  ext in (EXT_BIN, EXT_SCT):
144  cmd.append(executable)
145 
146  # add special call for setting vector colors
147  cmd.append('vcolors')
148  if scripts and sys.platform != "win32":
149  cmd = cmd + os.listdir(os.path.join(gisbase, 'scripts'))
150  if gui_scripts:
151  os.environ["PATH"] = os.getenv("PATH") + os.pathsep + os.path.join(gisbase, 'etc', 'gui', 'scripts')
152  os.environ["PATH"] = os.getenv("PATH") + os.pathsep + os.path.join(gisbase, 'etc', 'wxpython', 'scripts')
153  cmd = cmd + os.listdir(os.path.join(gisbase, 'etc', 'gui', 'scripts'))
154 
155  if os.getenv('GRASS_ADDON_PATH'):
156  for path in os.getenv('GRASS_ADDON_PATH').split(os.pathsep):
157  if not os.path.exists(path) or not os.path.isdir(path):
158  continue
159  for fname in os.listdir(path):
160  name, ext = os.path.splitext(fname)
161  if bin:
162  if ext == EXT_BIN:
163  cmd.append(name)
164  if scripts:
165  if ext == EXT_SCT:
166  cmd.append(name)
167  elif ext == '.py':
168  cmd.append(fname)
169 
170  if sys.platform == 'win32':
171  for idx in range(len(cmd)):
172  name, ext = os.path.splitext(cmd[idx])
173  if ext in (EXT_BIN, EXT_SCT):
174  cmd[idx] = name
175 
176  return cmd
177 
178 """@brief Collected GRASS-relared binaries/scripts"""
179 grassCmd = {}
180 grassCmd['all'] = GetGRASSCmds()
181 grassCmd['script'] = GetGRASSCmds(bin = False, gui_scripts = False)
182 
183 """@Toolbar icon size"""
184 toolbarSize = (24, 24)
185 
186 """@Is g.mlist available?"""
187 if 'g.mlist' in grassCmd['all']:
188  have_mlist = True
189 else:
190  have_mlist = False
191 
192 """@Check version of wxPython, use agwStyle for 2.8.11+"""
193 hasAgw = CheckWxVersion()