globalvar.py 7.7 KB

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