globalvar.py 7.3 KB

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