guiutils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. @package startup.guiutils
  3. @brief General GUI-dependent utilities for GUI startup of GRASS GIS
  4. (C) 2018 by Vaclav Petras 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 Vaclav Petras <wenzeslaus gmail com>
  8. This is for code which depend on something from GUI (wx or wxGUI).
  9. """
  10. import os
  11. import wx
  12. import grass.script as gs
  13. from core import globalvar
  14. from core.gcmd import DecodeString, RunCommand
  15. from gui_core.dialogs import TextEntryDialog
  16. from gui_core.widgets import GenericValidator
  17. def SetSessionMapset(database, location, mapset):
  18. """Sets database, location and mapset for the current session"""
  19. RunCommand("g.gisenv", set="GISDBASE=%s" % database)
  20. RunCommand("g.gisenv", set="LOCATION_NAME=%s" % location)
  21. RunCommand("g.gisenv", set="MAPSET=%s" % mapset)
  22. class NewMapsetDialog(TextEntryDialog):
  23. def __init__(self, parent=None, default=None,
  24. validation_failed_handler=None, help_hanlder=None):
  25. if help_hanlder:
  26. style = wx.OK | wx.CANCEL | wx.HELP
  27. else:
  28. style = wx.OK | wx.CANCEL
  29. if validation_failed_handler:
  30. validator=GenericValidator(
  31. gs.legal_name, validation_failed_handler)
  32. else:
  33. validator = None
  34. TextEntryDialog.__init__(
  35. self, parent=parent,
  36. message=_("Name for the new mapset:"),
  37. caption=_("Create new mapset"),
  38. defaultValue=default,
  39. validator=validator,
  40. style=style
  41. )
  42. if help_hanlder:
  43. help_button = self.FindWindowById(wx.ID_HELP)
  44. help_button.Bind(wx.EVT_BUTTON, help_hanlder)
  45. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  46. def read_gisrc():
  47. """Read variables from a current GISRC file
  48. Returns a dictionary representation of the file content.
  49. """
  50. grassrc = {}
  51. gisrc = os.getenv("GISRC")
  52. if gisrc and os.path.isfile(gisrc):
  53. try:
  54. rc = open(gisrc, "r")
  55. for line in rc.readlines():
  56. try:
  57. key, val = line.split(":", 1)
  58. except ValueError as e:
  59. sys.stderr.write(
  60. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  61. grassrc[key.strip()] = DecodeString(val.strip())
  62. finally:
  63. rc.close()
  64. return grassrc
  65. def GetVersion():
  66. """Gets version and revision
  67. Returns tuple `(version, revision)`. For standard releases revision
  68. is an empty string.
  69. Revision string is currently wrapped in parentheses with added
  70. leading space. This is an implementation detail and legacy and may
  71. change anytime.
  72. """
  73. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  74. versionLine = versionFile.readline().rstrip('\n')
  75. versionFile.close()
  76. try:
  77. grassVersion, grassRevision = versionLine.split(' ', 1)
  78. if grassVersion.endswith('dev'):
  79. grassRevisionStr = ' (%s)' % grassRevision
  80. else:
  81. grassRevisionStr = ''
  82. except ValueError:
  83. grassVersion = versionLine
  84. grassRevisionStr = ''
  85. return (grassVersion, grassRevisionStr)