globalvar.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_FF2 | \
  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_NODRAG | \
  112. FN.FNB_NO_NAV_BUTTONS | \
  113. FN.FNB_NO_X_BUTTON
  114. FNPageColor = wx.Colour(125, 200, 175)
  115. """Dialog widget dimension"""
  116. DIALOG_SPIN_SIZE = (150, -1)
  117. DIALOG_COMBOBOX_SIZE = (300, -1)
  118. DIALOG_GSELECT_SIZE = (400, -1)
  119. DIALOG_TEXTCTRL_SIZE = (400, -1)
  120. DIALOG_LAYER_SIZE = (100, -1)
  121. DIALOG_COLOR_SIZE = (30, 30)
  122. MAP_WINDOW_SIZE = (825, 600)
  123. GM_WINDOW_MIN_SIZE = (525, 400)
  124. # small for ms window which wraps the menu
  125. # small for max os x which has the global menu
  126. # small for ubuntu when menuproxy is defined
  127. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  128. # so the probably problem
  129. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  130. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  131. # to get smaller lmgr
  132. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  133. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  134. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  135. else:
  136. GM_WINDOW_SIZE = (625, 600)
  137. if sys.platform == 'win32':
  138. BIN_EXT = '.exe'
  139. SCT_EXT = '.bat'
  140. else:
  141. BIN_EXT = SCT_EXT = ''
  142. def UpdateGRASSAddOnCommands(eList=None):
  143. """Update list of available GRASS AddOns commands to use when
  144. parsing string from the command line
  145. :param eList: list of AddOns commands to remove
  146. """
  147. global grassCmd, grassScripts
  148. # scan addons (path)
  149. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  150. addonBase = os.getenv('GRASS_ADDON_BASE')
  151. if addonBase:
  152. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  153. if sys.platform != 'win32':
  154. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  155. # remove commands first
  156. if eList:
  157. for ext in eList:
  158. if ext in grassCmd:
  159. grassCmd.remove(ext)
  160. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  161. nCmd = 0
  162. pathList = os.getenv('PATH', '').split(os.pathsep)
  163. for path in addonPath.split(os.pathsep):
  164. if not os.path.exists(path) or not os.path.isdir(path):
  165. continue
  166. # check if addon is in the path
  167. if pathList and path not in pathList:
  168. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  169. for fname in os.listdir(path):
  170. if fname in ['docs', 'modules.xml']:
  171. continue
  172. if grassScripts: # win32
  173. name, ext = os.path.splitext(fname)
  174. if name not in grassCmd:
  175. if ext not in [BIN_EXT, SCT_EXT]:
  176. continue
  177. if name not in grassCmd:
  178. grassCmd.add(name)
  179. Debug.msg(3, "AddOn commands: %s", name)
  180. nCmd += 1
  181. if ext == SCT_EXT and \
  182. ext in grassScripts.keys() and \
  183. name not in grassScripts[ext]:
  184. grassScripts[ext].append(name)
  185. else:
  186. if fname not in grassCmd:
  187. grassCmd.add(fname)
  188. Debug.msg(3, "AddOn commands: %s", fname)
  189. nCmd += 1
  190. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  191. """@brief Collected GRASS-relared binaries/scripts"""
  192. grassCmd, grassScripts = get_commands()
  193. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  194. UpdateGRASSAddOnCommands()
  195. """@Toolbar icon size"""
  196. toolbarSize = (24, 24)
  197. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  198. hasAgw = CheckWxVersion([2, 8, 11, 0])
  199. wxPythonPhoenix = CheckWxPhoenix()
  200. gtk3 = True if 'gtk3' in wx.PlatformInfo else False
  201. """@Add GUIDIR/scripts into path"""
  202. os.environ['PATH'] = os.path.join(
  203. GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']