globalvar.py 7.4 KB

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