globalvar.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. # i18n is taken care of in the grass library code.
  16. # So we need to import it before any of the GUI code.
  17. from grass.script.core import get_commands
  18. from core.debug import Debug
  19. # path to python scripts
  20. ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
  21. GUIDIR = os.path.join(os.getenv("GISBASE"), "gui")
  22. WXGUIDIR = os.path.join(GUIDIR, "wxpython")
  23. ICONDIR = os.path.join(GUIDIR, "icons")
  24. IMGDIR = os.path.join(GUIDIR, "images")
  25. SYMBDIR = os.path.join(IMGDIR, "symbols")
  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. # Importing wx only after checks.
  91. import wx # noqa: E402
  92. if CheckWxPhoenix():
  93. try:
  94. import agw.flatnotebook as FN
  95. except ImportError: # if it's not there locally, try the wxPython lib.
  96. import wx.lib.agw.flatnotebook as FN
  97. else:
  98. import wx.lib.flatnotebook as FN
  99. """
  100. Query layer (generated for example by selecting item in the Attribute Table Manager)
  101. Deleted automatically on re-render action
  102. """
  103. # temporal query layer (removed on re-render action)
  104. QUERYLAYER = 'qlayer'
  105. """Style definition for FlatNotebook pages"""
  106. FNPageStyle = FN.FNB_FF2 | \
  107. FN.FNB_BACKGROUND_GRADIENT | \
  108. FN.FNB_NODRAG | \
  109. FN.FNB_TABS_BORDER_SIMPLE
  110. FNPageDStyle = FN.FNB_FANCY_TABS | \
  111. FN.FNB_BOTTOM | \
  112. FN.FNB_NODRAG | \
  113. FN.FNB_NO_NAV_BUTTONS | \
  114. FN.FNB_NO_X_BUTTON
  115. FNPageColor = wx.Colour(125, 200, 175)
  116. """Dialog widget dimension"""
  117. DIALOG_SPIN_SIZE = (150, -1)
  118. DIALOG_COMBOBOX_SIZE = (300, -1)
  119. DIALOG_GSELECT_SIZE = (400, -1)
  120. DIALOG_TEXTCTRL_SIZE = (400, -1)
  121. DIALOG_LAYER_SIZE = (100, -1)
  122. DIALOG_COLOR_SIZE = (30, 30)
  123. MAP_WINDOW_SIZE = (825, 600)
  124. GM_WINDOW_MIN_SIZE = (525, 400)
  125. # small for ms window which wraps the menu
  126. # small for max os x which has the global menu
  127. # small for ubuntu when menuproxy is defined
  128. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  129. # so the probably problem
  130. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  131. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  132. # to get smaller lmgr
  133. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  134. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  135. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  136. else:
  137. GM_WINDOW_SIZE = (625, 600)
  138. if sys.platform == 'win32':
  139. BIN_EXT = '.exe'
  140. SCT_EXT = '.bat'
  141. else:
  142. BIN_EXT = SCT_EXT = ''
  143. def UpdateGRASSAddOnCommands(eList=None):
  144. """Update list of available GRASS AddOns commands to use when
  145. parsing string from the command line
  146. :param eList: list of AddOns commands to remove
  147. """
  148. global grassCmd, grassScripts
  149. # scan addons (path)
  150. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  151. addonBase = os.getenv('GRASS_ADDON_BASE')
  152. if addonBase:
  153. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  154. if sys.platform != 'win32':
  155. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  156. # remove commands first
  157. if eList:
  158. for ext in eList:
  159. if ext in grassCmd:
  160. grassCmd.remove(ext)
  161. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  162. nCmd = 0
  163. pathList = os.getenv('PATH', '').split(os.pathsep)
  164. for path in addonPath.split(os.pathsep):
  165. if not os.path.exists(path) or not os.path.isdir(path):
  166. continue
  167. # check if addon is in the path
  168. if pathList and path not in pathList:
  169. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  170. for fname in os.listdir(path):
  171. if fname in ['docs', 'modules.xml']:
  172. continue
  173. if grassScripts: # win32
  174. name, ext = os.path.splitext(fname)
  175. if name not in grassCmd:
  176. if ext not in [BIN_EXT, SCT_EXT]:
  177. continue
  178. if name not in grassCmd:
  179. grassCmd.add(name)
  180. Debug.msg(3, "AddOn commands: %s", name)
  181. nCmd += 1
  182. if ext == SCT_EXT and \
  183. ext in grassScripts.keys() and \
  184. name not in grassScripts[ext]:
  185. grassScripts[ext].append(name)
  186. else:
  187. if fname not in grassCmd:
  188. grassCmd.add(fname)
  189. Debug.msg(3, "AddOn commands: %s", fname)
  190. nCmd += 1
  191. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  192. """@brief Collected GRASS-relared binaries/scripts"""
  193. grassCmd, grassScripts = get_commands()
  194. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  195. UpdateGRASSAddOnCommands()
  196. """@Toolbar icon size"""
  197. toolbarSize = (24, 24)
  198. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  199. hasAgw = CheckWxVersion([2, 8, 11, 0])
  200. wxPythonPhoenix = CheckWxPhoenix()
  201. gtk3 = True if 'gtk3' in wx.PlatformInfo else False
  202. """@Add GUIDIR/scripts into path"""
  203. os.environ['PATH'] = os.path.join(
  204. GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']