guiutils.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.gcmd import DecodeString, RunCommand
  14. from gui_core.dialogs import TextEntryDialog
  15. from gui_core.widgets import GenericValidator
  16. def SetSessionMapset(database, location, mapset):
  17. """Sets database, location and mapset for the current session"""
  18. RunCommand("g.gisenv", set="GISDBASE=%s" % database)
  19. RunCommand("g.gisenv", set="LOCATION_NAME=%s" % location)
  20. RunCommand("g.gisenv", set="MAPSET=%s" % mapset)
  21. class NewMapsetDialog(TextEntryDialog):
  22. def __init__(self, parent=None, default=None,
  23. validation_failed_handler=None, help_hanlder=None):
  24. if help_hanlder:
  25. style = wx.OK | wx.CANCEL | wx.HELP
  26. else:
  27. style = wx.OK | wx.CANCEL
  28. if validation_failed_handler:
  29. validator=GenericValidator(
  30. gs.legal_name, validation_failed_handler)
  31. else:
  32. validator = None
  33. TextEntryDialog.__init__(
  34. self, parent=parent,
  35. message=_("Name for the new mapset:"),
  36. caption=_("Create new mapset"),
  37. defaultValue=default,
  38. validator=validator,
  39. style=style
  40. )
  41. if help_hanlder:
  42. help_button = self.FindWindowById(wx.ID_HELP)
  43. help_button.Bind(wx.EVT_BUTTON, help_hanlder)
  44. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  45. def read_gisrc():
  46. """Read variables from a current GISRC file
  47. Returns a dictionary representation of the file content.
  48. """
  49. grassrc = {}
  50. gisrc = os.getenv("GISRC")
  51. if gisrc and os.path.isfile(gisrc):
  52. try:
  53. rc = open(gisrc, "r")
  54. for line in rc.readlines():
  55. try:
  56. key, val = line.split(":", 1)
  57. except ValueError as e:
  58. sys.stderr.write(
  59. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  60. grassrc[key.strip()] = DecodeString(val.strip())
  61. finally:
  62. rc.close()
  63. return grassrc