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