globalvar.py 7.4 KB

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