globalvar.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. import os
  10. import sys
  11. import locale
  12. if not os.getenv("GISBASE"):
  13. sys.exit("GRASS is not running. Exiting...")
  14. # path to python scripts
  15. ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
  16. GUIDIR = os.path.join(os.getenv("GISBASE"), "gui")
  17. WXGUIDIR = os.path.join(GUIDIR, "wxpython")
  18. ICONDIR = os.path.join(GUIDIR, "icons")
  19. IMGDIR = os.path.join(GUIDIR, "images")
  20. SYMBDIR = os.path.join(IMGDIR, "symbols")
  21. from core.debug import Debug
  22. # cannot import from the core.utils module to avoid cross dependencies
  23. try:
  24. # intended to be used also outside this module
  25. import gettext
  26. _ = gettext.translation(
  27. 'grasswxpy',
  28. os.path.join(
  29. os.getenv("GISBASE"),
  30. 'locale')).ugettext
  31. except IOError:
  32. # using no translation silently
  33. def null_gettext(string):
  34. return string
  35. _ = null_gettext
  36. from grass.script.core import get_commands
  37. def CheckWxPhoenix():
  38. if 'phoenix' in wx.version():
  39. return True
  40. return False
  41. def CheckWxVersion(version):
  42. """Check wx version"""
  43. ver = wx.__version__
  44. if map(int, ver.split('.')) < version:
  45. return False
  46. return True
  47. def CheckForWx(forceVersion=os.getenv('GRASS_WXVERSION', None)):
  48. """Try to import wx module and check its version
  49. :param forceVersion: force wxPython version, eg. '2.8'
  50. """
  51. if 'wx' in sys.modules.keys():
  52. return
  53. minVersion = [2, 8, 10, 1]
  54. try:
  55. try:
  56. # Note that Phoenix doesn't have wxversion anymore
  57. import wxversion
  58. except ImportError as e:
  59. # if there is no wx raises ImportError
  60. import wx
  61. return
  62. if forceVersion:
  63. wxversion.select(forceVersion)
  64. wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
  65. import wx
  66. version = wx.__version__
  67. if map(int, version.split('.')) < minVersion:
  68. raise ValueError(
  69. 'Your wxPython version is %s.%s.%s.%s' %
  70. tuple(version.split('.')))
  71. except ImportError as e:
  72. print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
  73. sys.exit(1)
  74. except (ValueError, wxversion.VersionError) as e:
  75. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(
  76. minVersion) + '%s.' % (str(e))
  77. sys.exit(1)
  78. except locale.Error as e:
  79. print >> sys.stderr, "Unable to set locale:", e
  80. os.environ['LC_ALL'] = ''
  81. if not os.getenv("GRASS_WXBUNDLED"):
  82. CheckForWx()
  83. import wx
  84. if CheckWxPhoenix():
  85. try:
  86. import agw.flatnotebook as FN
  87. except ImportError: # if it's not there locally, try the wxPython lib.
  88. import wx.lib.agw.flatnotebook as FN
  89. else:
  90. import wx.lib.flatnotebook as FN
  91. """
  92. Query layer (generated for example by selecting item in the Attribute Table Manager)
  93. Deleted automatically on re-render action
  94. """
  95. # temporal query layer (removed on re-render action)
  96. QUERYLAYER = 'qlayer'
  97. """Style definition for FlatNotebook pages"""
  98. FNPageStyle = FN.FNB_VC8 | \
  99. FN.FNB_BACKGROUND_GRADIENT | \
  100. FN.FNB_NODRAG | \
  101. FN.FNB_TABS_BORDER_SIMPLE
  102. FNPageDStyle = FN.FNB_FANCY_TABS | \
  103. FN.FNB_BOTTOM | \
  104. FN.FNB_NO_NAV_BUTTONS | \
  105. FN.FNB_NO_X_BUTTON
  106. FNPageColor = wx.Colour(125, 200, 175)
  107. """Dialog widget dimension"""
  108. DIALOG_SPIN_SIZE = (150, -1)
  109. DIALOG_COMBOBOX_SIZE = (300, -1)
  110. DIALOG_GSELECT_SIZE = (400, -1)
  111. DIALOG_TEXTCTRL_SIZE = (400, -1)
  112. DIALOG_LAYER_SIZE = (100, -1)
  113. DIALOG_COLOR_SIZE = (30, 30)
  114. MAP_WINDOW_SIZE = (825, 600)
  115. GM_WINDOW_MIN_SIZE = (525, 400)
  116. # small for ms window which wraps the menu
  117. # small for max os x which has the global menu
  118. # small for ubuntu when menuproxy is defined
  119. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  120. # so the probably problem
  121. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  122. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  123. # to get smaller lmgr
  124. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  125. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  126. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  127. else:
  128. GM_WINDOW_SIZE = (625, 600)
  129. if sys.platform == 'win32':
  130. BIN_EXT = '.exe'
  131. SCT_EXT = '.bat'
  132. else:
  133. BIN_EXT = SCT_EXT = ''
  134. def UpdateGRASSAddOnCommands(eList=None):
  135. """Update list of available GRASS AddOns commands to use when
  136. parsing string from the command line
  137. :param eList: list of AddOns commands to remove
  138. """
  139. global grassCmd, grassScripts
  140. # scan addons (path)
  141. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  142. addonBase = os.getenv('GRASS_ADDON_BASE')
  143. if addonBase:
  144. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  145. if sys.platform != 'win32':
  146. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  147. # remove commands first
  148. if eList:
  149. for ext in eList:
  150. if ext in grassCmd:
  151. grassCmd.remove(ext)
  152. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  153. nCmd = 0
  154. pathList = os.getenv('PATH', '').split(os.pathsep)
  155. for path in addonPath.split(os.pathsep):
  156. if not os.path.exists(path) or not os.path.isdir(path):
  157. continue
  158. # check if addon is in the path
  159. if pathList and path not in pathList:
  160. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  161. for fname in os.listdir(path):
  162. if fname in ['docs', 'modules.xml']:
  163. continue
  164. if grassScripts: # win32
  165. name, ext = os.path.splitext(fname)
  166. if name not in grassCmd:
  167. if ext not in [BIN_EXT, SCT_EXT]:
  168. continue
  169. if name not in grassCmd:
  170. grassCmd.add(name)
  171. Debug.msg(3, "AddOn commands: %s", name)
  172. nCmd += 1
  173. if ext == SCT_EXT and \
  174. ext in grassScripts.keys() and \
  175. name not in grassScripts[ext]:
  176. grassScripts[ext].append(name)
  177. else:
  178. if fname not in grassCmd:
  179. grassCmd.add(fname)
  180. Debug.msg(3, "AddOn commands: %s", fname)
  181. nCmd += 1
  182. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  183. """@brief Collected GRASS-relared binaries/scripts"""
  184. grassCmd, grassScripts = get_commands()
  185. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  186. UpdateGRASSAddOnCommands()
  187. """@Toolbar icon size"""
  188. toolbarSize = (24, 24)
  189. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  190. hasAgw = CheckWxVersion([2, 8, 11, 0])
  191. wxPython3 = CheckWxVersion([3, 0, 0, 0])
  192. wxPythonPhoenix = CheckWxPhoenix()
  193. gtk3 = True if 'gtk3' in wx.PlatformInfo else False
  194. """@Add GUIDIR/scripts into path"""
  195. os.environ['PATH'] = os.path.join(
  196. GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']