globalvar.py 5.3 KB

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