globalvar.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. print >> sys.stderr, ('You can still use GRASS GIS modules in'
  74. ' the command line or in Python.')
  75. sys.exit(1)
  76. except (ValueError, wxversion.VersionError) as e:
  77. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(
  78. minVersion) + '%s.' % (str(e))
  79. sys.exit(1)
  80. except locale.Error as e:
  81. print >> sys.stderr, "Unable to set locale:", e
  82. os.environ['LC_ALL'] = ''
  83. if not os.getenv("GRASS_WXBUNDLED"):
  84. CheckForWx()
  85. import wx
  86. if CheckWxPhoenix():
  87. try:
  88. import agw.flatnotebook as FN
  89. except ImportError: # if it's not there locally, try the wxPython lib.
  90. import wx.lib.agw.flatnotebook as FN
  91. else:
  92. import wx.lib.flatnotebook as FN
  93. """
  94. Query layer (generated for example by selecting item in the Attribute Table Manager)
  95. Deleted automatically on re-render action
  96. """
  97. # temporal query layer (removed on re-render action)
  98. QUERYLAYER = 'qlayer'
  99. """Style definition for FlatNotebook pages"""
  100. FNPageStyle = FN.FNB_VC8 | \
  101. FN.FNB_BACKGROUND_GRADIENT | \
  102. FN.FNB_NODRAG | \
  103. FN.FNB_TABS_BORDER_SIMPLE
  104. FNPageDStyle = FN.FNB_FANCY_TABS | \
  105. FN.FNB_BOTTOM | \
  106. FN.FNB_NO_NAV_BUTTONS | \
  107. FN.FNB_NO_X_BUTTON
  108. FNPageColor = wx.Colour(125, 200, 175)
  109. """Dialog widget dimension"""
  110. DIALOG_SPIN_SIZE = (150, -1)
  111. DIALOG_COMBOBOX_SIZE = (300, -1)
  112. DIALOG_GSELECT_SIZE = (400, -1)
  113. DIALOG_TEXTCTRL_SIZE = (400, -1)
  114. DIALOG_LAYER_SIZE = (100, -1)
  115. DIALOG_COLOR_SIZE = (30, 30)
  116. MAP_WINDOW_SIZE = (825, 600)
  117. GM_WINDOW_MIN_SIZE = (525, 400)
  118. # small for ms window which wraps the menu
  119. # small for max os x which has the global menu
  120. # small for ubuntu when menuproxy is defined
  121. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  122. # so the probably problem
  123. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  124. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  125. # to get smaller lmgr
  126. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  127. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  128. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  129. else:
  130. GM_WINDOW_SIZE = (625, 600)
  131. if sys.platform == 'win32':
  132. BIN_EXT = '.exe'
  133. SCT_EXT = '.bat'
  134. else:
  135. BIN_EXT = SCT_EXT = ''
  136. def UpdateGRASSAddOnCommands(eList=None):
  137. """Update list of available GRASS AddOns commands to use when
  138. parsing string from the command line
  139. :param eList: list of AddOns commands to remove
  140. """
  141. global grassCmd, grassScripts
  142. # scan addons (path)
  143. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  144. addonBase = os.getenv('GRASS_ADDON_BASE')
  145. if addonBase:
  146. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  147. if sys.platform != 'win32':
  148. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  149. # remove commands first
  150. if eList:
  151. for ext in eList:
  152. if ext in grassCmd:
  153. grassCmd.remove(ext)
  154. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  155. nCmd = 0
  156. pathList = os.getenv('PATH', '').split(os.pathsep)
  157. for path in addonPath.split(os.pathsep):
  158. if not os.path.exists(path) or not os.path.isdir(path):
  159. continue
  160. # check if addon is in the path
  161. if pathList and path not in pathList:
  162. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  163. for fname in os.listdir(path):
  164. if fname in ['docs', 'modules.xml']:
  165. continue
  166. if grassScripts: # win32
  167. name, ext = os.path.splitext(fname)
  168. if name not in grassCmd:
  169. if ext not in [BIN_EXT, SCT_EXT]:
  170. continue
  171. if name not in grassCmd:
  172. grassCmd.add(name)
  173. Debug.msg(3, "AddOn commands: %s", name)
  174. nCmd += 1
  175. if ext == SCT_EXT and \
  176. ext in grassScripts.keys() and \
  177. name not in grassScripts[ext]:
  178. grassScripts[ext].append(name)
  179. else:
  180. if fname not in grassCmd:
  181. grassCmd.add(fname)
  182. Debug.msg(3, "AddOn commands: %s", fname)
  183. nCmd += 1
  184. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  185. """@brief Collected GRASS-relared binaries/scripts"""
  186. grassCmd, grassScripts = get_commands()
  187. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  188. UpdateGRASSAddOnCommands()
  189. """@Toolbar icon size"""
  190. toolbarSize = (24, 24)
  191. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  192. hasAgw = CheckWxVersion([2, 8, 11, 0])
  193. wxPython3 = CheckWxVersion([3, 0, 0, 0])
  194. wxPythonPhoenix = CheckWxPhoenix()
  195. gtk3 = True if 'gtk3' in wx.PlatformInfo else False
  196. """@Add GUIDIR/scripts into path"""
  197. os.environ['PATH'] = os.path.join(
  198. GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']