globalvar.py 8.1 KB

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