globalvar.py 7.4 KB

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