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