globalvar.py 6.2 KB

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