globalvar.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. """
  2. @package core.globalvar
  3. @brief Global variables used by wxGUI
  4. (C) 2007-2016 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. GUIDIR = os.path.join(os.getenv("GISBASE"), "gui")
  17. WXGUIDIR = os.path.join(GUIDIR, "wxpython")
  18. ICONDIR = os.path.join(GUIDIR, "icons")
  19. IMGDIR = os.path.join(GUIDIR, "images")
  20. SYMBDIR = os.path.join(IMGDIR, "symbols")
  21. from core.debug import Debug
  22. # cannot import from the core.utils module to avoid cross dependencies
  23. try:
  24. # intended to be used also outside this module
  25. import gettext
  26. _ = gettext.translation(
  27. 'grasswxpy',
  28. os.path.join(
  29. os.getenv("GISBASE"),
  30. 'locale')).ugettext
  31. except IOError:
  32. # using no translation silently
  33. def null_gettext(string):
  34. return string
  35. _ = null_gettext
  36. from grass.script.core import get_commands
  37. def CheckWxVersion(version):
  38. """Check wx version"""
  39. ver = wx.__version__
  40. if map(int, ver.split('.')) < version:
  41. return False
  42. return True
  43. def CheckForWx(forceVersion=os.getenv('GRASS_WXVERSION', None)):
  44. """Try to import wx module and check its version
  45. :param forceVersion: force wxPython version, eg. '2.8'
  46. """
  47. if 'wx' in sys.modules.keys():
  48. return
  49. minVersion = [2, 8, 10, 1]
  50. try:
  51. try:
  52. import wxversion
  53. except ImportError as e:
  54. raise ImportError(e)
  55. if forceVersion:
  56. wxversion.select(forceVersion)
  57. wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
  58. import wx
  59. version = wx.__version__
  60. if map(int, version.split('.')) < minVersion:
  61. raise ValueError(
  62. 'Your wxPython version is %s.%s.%s.%s' %
  63. tuple(version.split('.')))
  64. except ImportError as e:
  65. print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
  66. sys.exit(1)
  67. except (ValueError, wxversion.VersionError) as e:
  68. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(
  69. minVersion) + '%s.' % (str(e))
  70. sys.exit(1)
  71. except locale.Error as e:
  72. print >> sys.stderr, "Unable to set locale:", e
  73. os.environ['LC_ALL'] = ''
  74. if not os.getenv("GRASS_WXBUNDLED"):
  75. CheckForWx()
  76. import wx
  77. import wx.lib.flatnotebook as FN
  78. """
  79. Query layer (generated for example by selecting item in the Attribute Table Manager)
  80. Deleted automatically on re-render action
  81. """
  82. # temporal query layer (removed on re-render action)
  83. QUERYLAYER = 'qlayer'
  84. """Style definition for FlatNotebook pages"""
  85. FNPageStyle = FN.FNB_VC8 | \
  86. FN.FNB_BACKGROUND_GRADIENT | \
  87. FN.FNB_NODRAG | \
  88. FN.FNB_TABS_BORDER_SIMPLE
  89. FNPageDStyle = FN.FNB_FANCY_TABS | \
  90. FN.FNB_BOTTOM | \
  91. FN.FNB_NO_NAV_BUTTONS | \
  92. FN.FNB_NO_X_BUTTON
  93. FNPageColor = wx.Colour(125, 200, 175)
  94. """Dialog widget dimension"""
  95. DIALOG_SPIN_SIZE = (150, -1)
  96. DIALOG_COMBOBOX_SIZE = (300, -1)
  97. DIALOG_GSELECT_SIZE = (400, -1)
  98. DIALOG_TEXTCTRL_SIZE = (400, -1)
  99. DIALOG_LAYER_SIZE = (100, -1)
  100. DIALOG_COLOR_SIZE = (30, 30)
  101. MAP_WINDOW_SIZE = (825, 600)
  102. GM_WINDOW_MIN_SIZE = (525, 400)
  103. # small for ms window which wraps the menu
  104. # small for max os x which has the global menu
  105. # small for ubuntu when menuproxy is defined
  106. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  107. # so the probably problem
  108. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  109. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  110. # to get smaller lmgr
  111. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  112. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  113. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  114. else:
  115. GM_WINDOW_SIZE = (625, 600)
  116. if sys.platform == 'win32':
  117. BIN_EXT = '.exe'
  118. SCT_EXT = '.bat'
  119. else:
  120. BIN_EXT = SCT_EXT = ''
  121. def UpdateGRASSAddOnCommands(eList=None):
  122. """Update list of available GRASS AddOns commands to use when
  123. parsing string from the command line
  124. :param eList: list of AddOns commands to remove
  125. """
  126. global grassCmd, grassScripts
  127. # scan addons (path)
  128. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  129. addonBase = os.getenv('GRASS_ADDON_BASE')
  130. if addonBase:
  131. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  132. if sys.platform != 'win32':
  133. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  134. # remove commands first
  135. if eList:
  136. for ext in eList:
  137. if ext in grassCmd:
  138. grassCmd.remove(ext)
  139. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  140. nCmd = 0
  141. pathList = os.getenv('PATH', '').split(os.pathsep)
  142. for path in addonPath.split(os.pathsep):
  143. if not os.path.exists(path) or not os.path.isdir(path):
  144. continue
  145. # check if addon is in the path
  146. if pathList and path not in pathList:
  147. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  148. for fname in os.listdir(path):
  149. if fname in ['docs', 'modules.xml']:
  150. continue
  151. if grassScripts: # win32
  152. name, ext = os.path.splitext(fname)
  153. if name not in grassCmd:
  154. if ext not in [BIN_EXT, SCT_EXT]:
  155. continue
  156. if name not in grassCmd:
  157. grassCmd.add(name)
  158. Debug.msg(3, "AddOn commands: %s", name)
  159. nCmd += 1
  160. if ext == SCT_EXT and \
  161. ext in grassScripts.keys() and \
  162. name not in grassScripts[ext]:
  163. grassScripts[ext].append(name)
  164. else:
  165. if fname not in grassCmd:
  166. grassCmd.add(fname)
  167. Debug.msg(3, "AddOn commands: %s", fname)
  168. nCmd += 1
  169. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  170. """@brief Collected GRASS-relared binaries/scripts"""
  171. grassCmd, grassScripts = get_commands()
  172. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  173. UpdateGRASSAddOnCommands()
  174. """@Toolbar icon size"""
  175. toolbarSize = (24, 24)
  176. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  177. hasAgw = CheckWxVersion([2, 8, 11, 0])
  178. wxPython3 = CheckWxVersion([3, 0, 0, 0])
  179. """@Add GUIDIR/scripts into path"""
  180. os.environ['PATH'] = os.path.join(
  181. GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']