GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
gis_set.py
Go to the documentation of this file.
1 """!
2 @package gis_set
3 
4 GRASS start-up screen.
5 
6 Initialization module for wxPython GRASS GUI.
7 Location/mapset management (selection, creation, etc.).
8 
9 Classes:
10  - gis_set::GRASSStartup
11  - gis_set::GListBox
12  - gis_set::StartUp
13 
14 (C) 2006-2012 by the GRASS Development Team
15 
16 This program is free software under the GNU General Public License
17 (>=v2). Read the file COPYING that comes with GRASS for details.
18 
19 @author Michael Barton and Jachym Cepicky (original author)
20 @author Martin Landa <landa.martin gmail.com> (various updates)
21 """
22 
23 import os
24 import sys
25 import shutil
26 import copy
27 import platform
28 import codecs
29 import getpass
30 
31 ### i18N
32 import gettext
33 gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
34 
35 if __name__ == "__main__":
36  sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython'))
37 from core import globalvar
38 import wx
39 import wx.lib.mixins.listctrl as listmix
40 import wx.lib.scrolledpanel as scrolled
41 
42 from gui_core.ghelp import HelpFrame
43 from core.gcmd import GMessage, GError, DecodeString, RunCommand
44 from core.utils import GetListOfLocations, GetListOfMapsets
45 from location_wizard.dialogs import RegionDef
46 from gui_core.dialogs import TextEntryDialog
47 from gui_core.widgets import GenericValidator
48 
49 from grass.script import core as grass
50 
51 sys.stderr = codecs.getwriter('utf8')(sys.stderr)
52 
53 class GRASSStartup(wx.Frame):
54  """!GRASS start-up screen"""
55  def __init__(self, parent = None, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE):
56 
57  #
58  # GRASS variables
59  #
60  self.gisbase = os.getenv("GISBASE")
61  self.grassrc = self._readGisRC()
62  self.gisdbase = self.GetRCValue("GISDBASE")
63 
64  #
65  # list of locations/mapsets
66  #
67  self.listOfLocations = []
68  self.listOfMapsets = []
70 
71  wx.Frame.__init__(self, parent = parent, id = id, style = style)
72 
73  self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
74 
75  self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
76 
77  # i18N
78  import gettext
79  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
80 
81  #
82  # graphical elements
83  #
84  # image
85  try:
86  name = os.path.join(globalvar.ETCIMGDIR, "startup_banner.gif")
87  self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
88  wx.Bitmap(name = name,
89  type = wx.BITMAP_TYPE_GIF))
90  except:
91  self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
92 
93  # labels
94  ### crashes when LOCATION doesn't exist
95  versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
96  grassVersion = versionFile.readline().split(' ')[0].rstrip('\n')
97  versionFile.close()
98 
99  self.select_box = wx.StaticBox (parent = self.panel, id = wx.ID_ANY,
100  label = " %s " % _("Choose project location and mapset"))
101 
102  self.manage_box = wx.StaticBox (parent = self.panel, id = wx.ID_ANY,
103  label = " %s " % _("Manage"))
104  self.lwelcome = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
105  label = _("Welcome to GRASS GIS %s\n"
106  "The world's leading open source GIS") % grassVersion,
107  style = wx.ALIGN_CENTRE)
108  self.ltitle = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
109  label = _("Select an existing project location and mapset\n"
110  "or define a new location"),
111  style = wx.ALIGN_CENTRE)
112  self.ldbase = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
113  label = _("GIS Data Directory:"))
114  self.llocation = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
115  label = _("Project location\n(projection/coordinate system)"),
116  style = wx.ALIGN_CENTRE)
117  self.lmapset = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
118  label = _("Accessible mapsets\n(directories of GIS files)"),
119  style = wx.ALIGN_CENTRE)
120  self.lcreate = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
121  label = _("Create new mapset\nin selected location"),
122  style = wx.ALIGN_CENTRE)
123  self.ldefine = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
124  label = _("Define new location"),
125  style = wx.ALIGN_CENTRE)
126  self.lmanageloc = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
127  label = _("Rename/delete selected\nmapset or location"),
128  style = wx.ALIGN_CENTRE)
129 
130  # buttons
131  self.bstart = wx.Button(parent = self.panel, id = wx.ID_ANY,
132  label = _("Start &GRASS"))
133  self.bstart.SetDefault()
134  self.bexit = wx.Button(parent = self.panel, id = wx.ID_EXIT)
135  self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
136  self.bhelp = wx.Button(parent = self.panel, id = wx.ID_HELP)
137  self.bbrowse = wx.Button(parent = self.panel, id = wx.ID_ANY,
138  label = _("&Browse"))
139  self.bmapset = wx.Button(parent = self.panel, id = wx.ID_ANY,
140  label = _("&Create mapset"))
141  self.bwizard = wx.Button(parent = self.panel, id = wx.ID_ANY,
142  label = _("&Location wizard"))
143  self.bwizard.SetToolTipString(_("Start location wizard."
144  " After location is created successfully,"
145  " GRASS session is started."))
146  self.manageloc = wx.Choice(parent = self.panel, id = wx.ID_ANY,
147  choices = [_('Rename mapset'), _('Rename location'),
148  _('Delete mapset'), _('Delete location')])
149  self.manageloc.SetSelection(0)
150 
151  # textinputs
152  self.tgisdbase = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY, value = "", size = (300, -1),
153  style = wx.TE_PROCESS_ENTER)
154 
155  # Locations
156  self.lblocations = GListBox(parent = self.panel,
157  id = wx.ID_ANY, size = (180, 200),
158  choices = self.listOfLocations)
159 
160  self.lblocations.SetColumnWidth(0, 180)
161 
162  # TODO: sort; but keep PERMANENT on top of list
163  # Mapsets
164  self.lbmapsets = GListBox(parent = self.panel,
165  id = wx.ID_ANY, size = (180, 200),
166  choices = self.listOfMapsets)
167 
168  self.lbmapsets.SetColumnWidth(0, 180)
169 
170  # layout & properties
171  self._set_properties()
172  self._do_layout()
173 
174  # events
175  self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
176  self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
177  self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
178  self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
179  self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
180  self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
181  self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
182  self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
183  self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
184  self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
185  self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
186  self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
187 
188  def _set_properties(self):
189  """!Set frame properties"""
190  self.SetTitle(_("Welcome to GRASS GIS"))
191  self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
192  wx.BITMAP_TYPE_ICO))
193 
194  self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
195  self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
196 
197  self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
198  self.bstart.SetToolTipString(_("Enter GRASS session"))
199  self.bstart.Enable(False)
200  self.bmapset.Enable(False)
201  self.manageloc.Enable(False)
202 
203  # set database
204  if not self.gisdbase:
205  # sets an initial path for gisdbase if nothing in GISRC
206  if os.path.isdir(os.getenv("HOME")):
207  self.gisdbase = os.getenv("HOME")
208  else:
209  self.gisdbase = os.getcwd()
210  try:
211  self.tgisdbase.SetValue(self.gisdbase)
212  except UnicodeDecodeError:
213  wx.MessageBox(parent = self, caption = _("Error"),
214  message = _("Unable to set GRASS database. "
215  "Check your locale settings."),
216  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
217 
218  self.OnSetDatabase(None)
219  location = self.GetRCValue("LOCATION_NAME")
220  if location == "<UNKNOWN>" or \
221  not os.path.isdir(os.path.join(self.gisdbase, location)):
222  location = None
223 
224  if location:
225  # list of locations
226  self.UpdateLocations(self.gisdbase)
227  try:
228  self.lblocations.SetSelection(self.listOfLocations.index(location),
229  force = True)
230  self.lblocations.EnsureVisible(self.listOfLocations.index(location))
231  except ValueError:
232  print >> sys.stderr, _("ERROR: Location <%s> not found") % location
233 
234  # list of mapsets
235  self.UpdateMapsets(os.path.join(self.gisdbase, location))
236  mapset = self.GetRCValue("MAPSET")
237  if mapset:
238  try:
239  self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
240  force = True)
241  self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
242  except ValueError:
243  self.lbmapsets.Clear()
244  print >> sys.stderr, _("ERROR: Mapset <%s> not found") % mapset
245 
246  def _do_layout(self):
247  sizer = wx.BoxSizer(wx.VERTICAL)
248  dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
249  location_sizer = wx.BoxSizer(wx.HORIZONTAL)
250  select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
251  select_sizer = wx.FlexGridSizer(rows = 2, cols = 2, vgap = 4, hgap = 4)
252  select_sizer.AddGrowableRow(1)
253  select_sizer.AddGrowableCol(0)
254  select_sizer.AddGrowableCol(1)
255  manage_sizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
256  btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
257 
258  # gis data directory
259  dbase_sizer.Add(item = self.ldbase, proportion = 0,
260  flag = wx.ALIGN_CENTER_VERTICAL |
261  wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
262  border = 3)
263  dbase_sizer.Add(item = self.tgisdbase, proportion = 1,
264  flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
265  border = 3)
266  dbase_sizer.Add(item = self.bbrowse, proportion = 0,
267  flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
268  border = 3)
269 
270  # select sizer
271  select_sizer.Add(item = self.llocation, proportion = 0,
272  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
273  border = 3)
274  select_sizer.Add(item = self.lmapset, proportion = 0,
275  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
276  border = 3)
277  select_sizer.Add(item = self.lblocations, proportion = 1,
278  flag = wx.EXPAND)
279  select_sizer.Add(item = self.lbmapsets, proportion = 1,
280  flag = wx.EXPAND)
281 
282  select_boxsizer.Add(item = select_sizer, proportion = 1,
283  flag = wx.EXPAND)
284 
285  # define new location and mapset
286  manage_sizer.Add(item = self.ldefine, proportion = 0,
287  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
288  border = 3)
289  manage_sizer.Add(item = self.bwizard, proportion = 0,
290  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
291  border = 5)
292  manage_sizer.Add(item = self.lcreate, proportion = 0,
293  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
294  border = 3)
295  manage_sizer.Add(item = self.bmapset, proportion = 0,
296  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
297  border = 5)
298  manage_sizer.Add(item = self.lmanageloc, proportion = 0,
299  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
300  border = 3)
301  manage_sizer.Add(item = self.manageloc, proportion = 0,
302  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
303  border = 5)
304 
305  # location sizer
306  location_sizer.Add(item = select_boxsizer, proportion = 1,
307  flag = wx.LEFT | wx.RIGHT | wx.EXPAND,
308  border = 3)
309  location_sizer.Add(item = manage_sizer, proportion = 0,
310  flag = wx.RIGHT | wx.EXPAND,
311  border = 3)
312 
313  # buttons
314  btns_sizer.Add(item = self.bstart, proportion = 0,
315  flag = wx.ALIGN_CENTER_HORIZONTAL |
316  wx.ALIGN_CENTER_VERTICAL |
317  wx.ALL,
318  border = 5)
319  btns_sizer.Add(item = self.bexit, proportion = 0,
320  flag = wx.ALIGN_CENTER_HORIZONTAL |
321  wx.ALIGN_CENTER_VERTICAL |
322  wx.ALL,
323  border = 5)
324  btns_sizer.Add(item = self.bhelp, proportion = 0,
325  flag = wx.ALIGN_CENTER_HORIZONTAL |
326  wx.ALIGN_CENTER_VERTICAL |
327  wx.ALL,
328  border = 5)
329 
330  # main sizer
331  sizer.Add(item = self.hbitmap,
332  proportion = 0,
333  flag = wx.ALIGN_CENTER_VERTICAL |
334  wx.ALIGN_CENTER_HORIZONTAL |
335  wx.ALL,
336  border = 3) # image
337  sizer.Add(item = self.lwelcome, # welcome message
338  proportion = 0,
339  flag = wx.ALIGN_CENTER_VERTICAL |
340  wx.ALIGN_CENTER_HORIZONTAL |
341  wx.BOTTOM,
342  border=1)
343  sizer.Add(item = self.ltitle, # title
344  proportion = 0,
345  flag = wx.ALIGN_CENTER_VERTICAL |
346  wx.ALIGN_CENTER_HORIZONTAL)
347  sizer.Add(item = dbase_sizer, proportion = 0,
348  flag = wx.ALIGN_CENTER_HORIZONTAL |
349  wx.RIGHT | wx.LEFT | wx.EXPAND,
350  border = 20) # GISDBASE setting
351  sizer.Add(item = location_sizer, proportion = 1,
352  flag = wx.RIGHT | wx.LEFT | wx.EXPAND,
353  border = 1)
354  sizer.Add(item = btns_sizer, proportion = 0,
355  flag = wx.ALIGN_CENTER_VERTICAL |
356  wx.ALIGN_CENTER_HORIZONTAL |
357  wx.RIGHT | wx.LEFT,
358  border = 1)
359 
360  self.panel.SetAutoLayout(True)
361  self.panel.SetSizer(sizer)
362  sizer.Fit(self.panel)
363  sizer.SetSizeHints(self)
364 
365  self.Layout()
366 
367  def _readGisRC(self):
368  """
369  Read variables from $HOME/.grassrc6 file
370  """
371 
372  grassrc = {}
373 
374  gisrc = os.getenv("GISRC")
375 
376  if gisrc and os.path.isfile(gisrc):
377  try:
378  rc = open(gisrc, "r")
379  for line in rc.readlines():
380  try:
381  key, val = line.split(":", 1)
382  except ValueError, e:
383  sys.stderr.write(_('Invalid line in GISRC file (%(e)s):%(l)s\n' % \
384  {'e': e, 'l': line}))
385  grassrc[key.strip()] = DecodeString(val.strip())
386  finally:
387  rc.close()
388 
389  return grassrc
390 
391  def GetRCValue(self, value):
392  """!Return GRASS variable (read from GISRC)
393  """
394  if self.grassrc.has_key(value):
395  return self.grassrc[value]
396  else:
397  return None
398 
399  def OnWizard(self, event):
400  """!Location wizard started"""
401  from location_wizard.wizard import LocationWizard
402  gWizard = LocationWizard(parent = self,
403  grassdatabase = self.tgisdbase.GetValue())
404  if gWizard.location != None:
405  self.tgisdbase.SetValue(gWizard.grassdatabase)
406  self.OnSetDatabase(None)
407  self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
408  self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
409  self.lbmapsets.SetSelection(0)
410  self.SetLocation(self.gisdbase, gWizard.location, 'PERMANENT')
411  if gWizard.georeffile:
412  message = _("Do you want to import data source <%(name)s> to created location?"
413  " Default region will be set to match imported map.") % {'name': gWizard.georeffile}
414  dlg = wx.MessageDialog(parent = self,
415  message = message,
416  caption = _("Import data"),
417  style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
418  dlg.CenterOnScreen()
419  if dlg.ShowModal() == wx.ID_YES:
420  self.ImportFile(gWizard.georeffile)
421  else:
422  self.SetDefaultRegion(location = gWizard.location)
423  dlg.Destroy()
424  else:
425  self.SetDefaultRegion(location = gWizard.location)
426 
427  dlg = TextEntryDialog(parent=self,
428  message=_("Do you want to create new mapset?"),
429  caption=_("Create new mapset"),
430  defaultValue=self._getDefaultMapsetName(),
431  validator=GenericValidator(grass.legal_name, self._nameValidationFailed),
432  style=wx.OK | wx.CANCEL | wx.HELP)
433  help = dlg.FindWindowById(wx.ID_HELP)
434  help.Bind(wx.EVT_BUTTON, self.OnHelp)
435  if dlg.ShowModal() == wx.ID_OK:
436  mapsetName = dlg.GetValue()
437  self.CreateNewMapset(mapsetName)
438 
439  def SetDefaultRegion(self, location):
440  """!Asks to set default region."""
441  dlg = wx.MessageDialog(parent = self,
442  message = _("Do you want to set the default "
443  "region extents and resolution now?"),
444  caption = _("Location <%s> created") % location,
445  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
446  dlg.CenterOnScreen()
447  if dlg.ShowModal() == wx.ID_YES:
448  dlg.Destroy()
449  defineRegion = RegionDef(self, location = location)
450  defineRegion.CenterOnScreen()
451  defineRegion.ShowModal()
452  defineRegion.Destroy()
453  else:
454  dlg.Destroy()
455 
456  def ImportFile(self, filePath):
457  """!Tries to import file as vector or raster.
458 
459  If successfull sets default region from imported map.
460  """
461  returncode, stdout, messagesIfVector = RunCommand('v.in.ogr', dsn = filePath, flags = 'l',
462  read = True, getErrorMsg = True)
463  if returncode == 0:
464  wx.BeginBusyCursor()
465  wx.Yield()
466  returncode, messages = RunCommand('v.in.ogr', dsn = filePath,
467  output = os.path.splitext(os.path.basename(filePath))[0],
468  getErrorMsg = True)
469  wx.EndBusyCursor()
470  if returncode != 0:
471  message = _("Import of vector data source <%(name)s> failed.") % {'name': filePath}
472  message += "\n" + messages
473  GError(message = message)
474  else:
475  GMessage(message = _("Vector data source <%(name)s> imported successfully.") % {'name': filePath})
476  stdout = RunCommand('g.list', type = 'vect', read = True)
477  maps = stdout.splitlines()
478  if maps:
479  # TODO: what about resolution?
480  RunCommand('g.region', flags = 's', vect = maps[0])
481 
482  else:
483  wx.BeginBusyCursor()
484  wx.Yield()
485  returncode, messages = RunCommand('r.in.gdal', input = filePath,
486  output = os.path.splitext(os.path.basename(filePath))[0],
487  getErrorMsg = True)
488  wx.EndBusyCursor()
489  if returncode != 0:
490  message = _("Attempt to import data source <%(name)s> as raster or vector failed. ") % {'name': filePath}
491  message += "\n\n" + messagesIfVector + "\n" + messages
492  GError(message = message)
493  else:
494  GMessage(message = _("Raster data source <%(name)s> imported successfully.") % {'name': filePath})
495  stdout = RunCommand('g.list', type = 'rast', read = True)
496  maps = stdout.splitlines()
497  if maps:
498  RunCommand('g.region', flags = 's', rast = maps[0])
499 
500  def OnManageLoc(self, event):
501  """!Location management choice control handler
502  """
503  sel = event.GetSelection()
504  if sel == 0:
505  self.RenameMapset()
506  elif sel == 1:
507  self.RenameLocation()
508  elif sel == 2:
509  self.DeleteMapset()
510  elif sel == 3:
511  self.DeleteLocation()
512 
513  event.Skip()
514 
515  def RenameMapset(self):
516  """!Rename selected mapset
517  """
518  location = self.listOfLocations[self.lblocations.GetSelection()]
519  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
520  if mapset == 'PERMANENT':
521  GMessage(parent = self,
522  message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
523  'This mapset cannot be renamed.'))
524  return
525 
526  dlg = TextEntryDialog(parent = self,
527  message = _('Current name: %s\n\nEnter new name:') % mapset,
528  caption = _('Rename selected mapset'),
529  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
530 
531  if dlg.ShowModal() == wx.ID_OK:
532  newmapset = dlg.GetValue()
533  if newmapset == mapset:
534  dlg.Destroy()
535  return
536 
537  if newmapset in self.listOfMapsets:
538  wx.MessageBox(parent = self,
539  caption = _('Message'),
540  message = _('Unable to rename mapset.\n\n'
541  'Mapset <%s> already exists in location.') % newmapset,
542  style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
543  else:
544  try:
545  os.rename(os.path.join(self.gisdbase, location, mapset),
546  os.path.join(self.gisdbase, location, newmapset))
547  self.OnSelectLocation(None)
548  self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
549  except StandardError, e:
550  wx.MessageBox(parent = self,
551  caption = _('Error'),
552  message = _('Unable to rename mapset.\n\n%s') % e,
553  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
554 
555  dlg.Destroy()
556 
557  def RenameLocation(self):
558  """!Rename selected location
559  """
560  location = self.listOfLocations[self.lblocations.GetSelection()]
561 
562  dlg = TextEntryDialog(parent = self,
563  message = _('Current name: %s\n\nEnter new name:') % location,
564  caption = _('Rename selected location'),
565  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
566 
567  if dlg.ShowModal() == wx.ID_OK:
568  newlocation = dlg.GetValue()
569  if newlocation == location:
570  dlg.Destroy()
571  return
572 
573  if newlocation in self.listOfLocations:
574  wx.MessageBox(parent = self,
575  caption = _('Message'),
576  message = _('Unable to rename location.\n\n'
577  'Location <%s> already exists in GRASS database.') % newlocation,
578  style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
579  else:
580  try:
581  os.rename(os.path.join(self.gisdbase, location),
582  os.path.join(self.gisdbase, newlocation))
583  self.UpdateLocations(self.gisdbase)
584  self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
585  self.UpdateMapsets(newlocation)
586  except StandardError, e:
587  wx.MessageBox(parent = self,
588  caption = _('Error'),
589  message = _('Unable to rename location.\n\n%s') % e,
590  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
591 
592  dlg.Destroy()
593 
594  def DeleteMapset(self):
595  """!Delete selected mapset
596  """
597  location = self.listOfLocations[self.lblocations.GetSelection()]
598  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
599  if mapset == 'PERMANENT':
600  GMessage(parent = self,
601  message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
602  'This mapset cannot be deleted.'))
603  return
604 
605  dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting mapset <%(mapset)s> "
606  "from location <%(location)s>?\n\n"
607  "ALL MAPS included in this mapset will be "
608  "PERMANENTLY DELETED!") % {'mapset' : mapset,
609  'location' : location},
610  caption = _("Delete selected mapset"),
611  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
612 
613  if dlg.ShowModal() == wx.ID_YES:
614  try:
615  shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
616  self.OnSelectLocation(None)
617  self.lbmapsets.SetSelection(0)
618  except:
619  wx.MessageBox(message = _('Unable to delete mapset'))
620 
621  dlg.Destroy()
622 
623  def DeleteLocation(self):
624  """
625  Delete selected location
626  """
627 
628  location = self.listOfLocations[self.lblocations.GetSelection()]
629 
630  dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting "
631  "location <%s>?\n\n"
632  "ALL MAPS included in this location will be "
633  "PERMANENTLY DELETED!") % (location),
634  caption = _("Delete selected location"),
635  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
636 
637  if dlg.ShowModal() == wx.ID_YES:
638  try:
639  shutil.rmtree(os.path.join(self.gisdbase, location))
640  self.UpdateLocations(self.gisdbase)
641  self.lblocations.SetSelection(0)
642  self.OnSelectLocation(None)
643  self.lbmapsets.SetSelection(0)
644  except:
645  wx.MessageBox(message = _('Unable to delete location'))
646 
647  dlg.Destroy()
648 
649  def UpdateLocations(self, dbase):
650  """!Update list of locations"""
651  try:
652  self.listOfLocations = GetListOfLocations(dbase)
653  except UnicodeEncodeError:
654  wx.MessageBox(parent = self, caption = _("Error"),
655  message = _("Unable to set GRASS database. "
656  "Check your locale settings."),
657  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
658 
659  self.lblocations.Clear()
660  self.lblocations.InsertItems(self.listOfLocations, 0)
661 
662  if len(self.listOfLocations) > 0:
663  self.lblocations.SetSelection(0)
664  else:
665  self.lblocations.SetSelection(wx.NOT_FOUND)
666 
667  return self.listOfLocations
668 
669  def UpdateMapsets(self, location):
670  """!Update list of mapsets"""
671  self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
672 
673  self.listOfMapsetsSelectable = list()
674  self.listOfMapsets = GetListOfMapsets(self.gisdbase, location)
675 
676  self.lbmapsets.Clear()
677 
678  # disable mapset with denied permission
679  locationName = os.path.basename(location)
680 
681  ret = RunCommand('g.mapset',
682  read = True,
683  flags = 'l',
684  location = locationName,
685  gisdbase = self.gisdbase)
686 
687  if ret:
688  for line in ret.splitlines():
689  self.listOfMapsetsSelectable += line.split(' ')
690  else:
691  RunCommand("g.gisenv",
692  set = "GISDBASE=%s" % self.gisdbase)
693  RunCommand("g.gisenv",
694  set = "LOCATION_NAME=%s" % locationName)
695  RunCommand("g.gisenv",
696  set = "MAPSET=PERMANENT")
697  # first run only
698  self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
699 
700  disabled = []
701  idx = 0
702  for mapset in self.listOfMapsets:
703  if mapset not in self.listOfMapsetsSelectable or \
704  os.path.isfile(os.path.join(self.gisdbase,
705  locationName,
706  mapset, ".gislock")):
707  disabled.append(idx)
708  idx += 1
709 
710  self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
711 
712  return self.listOfMapsets
713 
714  def OnSelectLocation(self, event):
715  """!Location selected"""
716  if event:
717  self.lblocations.SetSelection(event.GetIndex())
718 
719  if self.lblocations.GetSelection() != wx.NOT_FOUND:
720  self.UpdateMapsets(os.path.join(self.gisdbase,
721  self.listOfLocations[self.lblocations.GetSelection()]))
722  else:
723  self.listOfMapsets = []
724 
725  disabled = []
726  idx = 0
727  try:
728  locationName = self.listOfLocations[self.lblocations.GetSelection()]
729  except IndexError:
730  locationName = ''
731 
732  for mapset in self.listOfMapsets:
733  if mapset not in self.listOfMapsetsSelectable or \
734  os.path.isfile(os.path.join(self.gisdbase,
735  locationName,
736  mapset, ".gislock")):
737  disabled.append(idx)
738  idx += 1
739 
740  self.lbmapsets.Clear()
741  self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
742 
743  if len(self.listOfMapsets) > 0:
744  self.lbmapsets.SetSelection(0)
745  if locationName:
746  # enable start button when location and mapset is selected
747  self.bstart.Enable()
748  self.bmapset.Enable()
749  self.manageloc.Enable()
750  else:
751  self.lbmapsets.SetSelection(wx.NOT_FOUND)
752  self.bstart.Enable(False)
753  self.bmapset.Enable(False)
754  self.manageloc.Enable(False)
755 
756  def OnSelectMapset(self, event):
757  """!Mapset selected"""
758  self.lbmapsets.SetSelection(event.GetIndex())
759 
760  if event.GetText() not in self.listOfMapsetsSelectable:
761  self.lbmapsets.SetSelection(self.FormerMapsetSelection)
762  else:
763  self.FormerMapsetSelection = event.GetIndex()
764  event.Skip()
765 
766  def OnSetDatabase(self, event):
767  """!Database set"""
768  self.gisdbase = self.tgisdbase.GetValue()
769 
770  self.UpdateLocations(self.gisdbase)
771 
772  self.OnSelectLocation(None)
773 
774  def OnBrowse(self, event):
775  """'Browse' button clicked"""
776  if not event:
777  defaultPath = os.getenv('HOME')
778  else:
779  defaultPath = ""
780 
781  dlg = wx.DirDialog(parent = self, message = _("Choose GIS Data Directory"),
782  defaultPath = defaultPath, style = wx.DD_DEFAULT_STYLE)
783 
784  if dlg.ShowModal() == wx.ID_OK:
785  self.gisdbase = dlg.GetPath()
786  self.tgisdbase.SetValue(self.gisdbase)
787  self.OnSetDatabase(event)
788 
789  dlg.Destroy()
790 
791  def OnCreateMapset(self, event):
792  """!Create new mapset"""
793 
794  dlg = TextEntryDialog(parent = self,
795  message = _('Enter name for new mapset:'),
796  caption = _('Create new mapset'),
797  defaultValue = self._getDefaultMapsetName(),
798  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
799 
800  if dlg.ShowModal() == wx.ID_OK:
801  mapset = dlg.GetValue()
802  return self.CreateNewMapset(mapset = mapset)
803  else:
804  return False
805 
806  def CreateNewMapset(self, mapset):
807  if mapset in self.listOfMapsets:
808  GMessage(parent = self,
809  message = _("Mapset <%s> already exists.") % mapset)
810  return False
811 
812  try:
813  self.gisdbase = self.tgisdbase.GetValue()
814  location = self.listOfLocations[self.lblocations.GetSelection()]
815  os.mkdir(os.path.join(self.gisdbase, location, mapset))
816  # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
817  shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
818  os.path.join(self.gisdbase, location, mapset))
819  # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
820  self.OnSelectLocation(None)
821  self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
822  self.bstart.SetFocus()
823  return True
824  except StandardError, e:
825  GError(parent = self,
826  message = _("Unable to create new mapset: %s") % e,
827  showTraceback = False)
828  return False
829 
830  def OnStart(self, event):
831  """'Start GRASS' button clicked"""
832  dbase = self.tgisdbase.GetValue()
833  location = self.listOfLocations[self.lblocations.GetSelection()]
834  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
835 
836  lockfile = os.path.join(dbase, location, mapset, '.gislock')
837  if os.path.isfile(lockfile):
838  dlg = wx.MessageDialog(parent = self,
839  message = _("GRASS is already running in selected mapset <%(mapset)s>\n"
840  "(file %(lock)s found).\n\n"
841  "Concurrent use not allowed.\n\n"
842  "Do you want to try to remove .gislock (note that you "
843  "need permission for this operation) and continue?") %
844  { 'mapset' : mapset, 'lock' : lockfile },
845  caption = _("Lock file found"),
846  style = wx.YES_NO | wx.NO_DEFAULT |
847  wx.ICON_QUESTION | wx.CENTRE)
848 
849  ret = dlg.ShowModal()
850  dlg.Destroy()
851  if ret == wx.ID_YES:
852  dlg1 = wx.MessageDialog(parent = self,
853  message = _("ARE YOU REALLY SURE?\n\n"
854  "If you really are running another GRASS session doing this "
855  "could corrupt your data. Have another look in the processor "
856  "manager just to be sure..."),
857  caption = _("Lock file found"),
858  style = wx.YES_NO | wx.NO_DEFAULT |
859  wx.ICON_QUESTION | wx.CENTRE)
860 
861  ret = dlg1.ShowModal()
862  dlg1.Destroy()
863 
864  if ret == wx.ID_YES:
865  try:
866  os.remove(lockfile)
867  except IOError, e:
868  GError(_("Unable to remove '%(lock)s'.\n\n"
869  "Details: %(reason)s") % { 'lock' : lockfile, 'reason' : e})
870  else:
871  return
872  else:
873  return
874  self.SetLocation(dbase, location, mapset)
875  self.ExitSuccessfully()
876 
877  def SetLocation(self, dbase, location, mapset):
878  RunCommand("g.gisenv",
879  set = "GISDBASE=%s" % dbase)
880  RunCommand("g.gisenv",
881  set = "LOCATION_NAME=%s" % location)
882  RunCommand("g.gisenv",
883  set = "MAPSET=%s" % mapset)
884 
885 
886  def _getDefaultMapsetName(self):
887  """!Returns default name for mapset."""
888  try:
889  defaultName = getpass.getuser()
890  defaultName.encode('ascii') # raise error if not ascii (not valid mapset name)
891  except: # whatever might go wrong
892  defaultName = 'user'
893 
894  return defaultName
895 
896  def ExitSuccessfully(self):
897  self.Destroy()
898  sys.exit(0)
899 
900  def OnExit(self, event):
901  """'Exit' button clicked"""
902  self.Destroy()
903  sys.exit (2)
904 
905  def OnHelp(self, event):
906  """'Help' button clicked"""
907  # help text in lib/init/helptext.html
908  filePath = os.path.join(self.gisbase, "docs", "html", "helptext.html")
909  import webbrowser
910  webbrowser.open(filePath)
911 
912  def OnCloseWindow(self, event):
913  """!Close window event"""
914  event.Skip()
915  sys.exit(2)
916 
917  def _nameValidationFailed(self, ctrl):
918  message = _("Name <%(name)s> is not a valid name for location or mapset. "
919  "Please use only ASCII characters excluding %(chars)s "
920  "and space.") % {'name': ctrl.GetValue(), 'chars': '/"\'@,=*~'}
921  GError(parent=self, message=message, caption=_("Invalid name"))
922 
923 class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
924  """!Use wx.ListCtrl instead of wx.ListBox, different style for
925  non-selectable items (e.g. mapsets with denied permission)"""
926  def __init__(self, parent, id, size,
927  choices, disabled = []):
928  wx.ListCtrl.__init__(self, parent, id, size = size,
929  style = wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
930  wx.BORDER_SUNKEN)
931 
932  listmix.ListCtrlAutoWidthMixin.__init__(self)
933 
934  self.InsertColumn(0, '')
935 
936  self.selected = wx.NOT_FOUND
937 
938  self._LoadData(choices, disabled)
939 
940  def _LoadData(self, choices, disabled = []):
941  """!Load data into list
942 
943  @param choices list of item
944  @param disabled list of indeces of non-selectable items
945  """
946  idx = 0
947  for item in choices:
948  index = self.InsertStringItem(sys.maxint, item)
949  self.SetStringItem(index, 0, item)
950 
951  if idx in disabled:
952  self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
953  idx += 1
954 
955  def Clear(self):
956  self.DeleteAllItems()
957 
958  def InsertItems(self, choices, pos, disabled = []):
959  self._LoadData(choices, disabled)
960 
961  def SetSelection(self, item, force = False):
962  if item != wx.NOT_FOUND and \
963  (platform.system() != 'Windows' or force):
964  ### Windows -> FIXME
965  self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
966 
967  self.selected = item
968 
969  def GetSelection(self):
970  return self.selected
971 
972 class StartUp(wx.App):
973  """!Start-up application"""
974 
975  def OnInit(self):
976  wx.InitAllImageHandlers()
977  StartUp = GRASSStartup()
978  StartUp.CenterOnScreen()
979  self.SetTopWindow(StartUp)
980  StartUp.Show()
981 
982  if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
983  wx.MessageBox(parent = StartUp,
984  caption = _('Starting GRASS for the first time'),
985  message = _('GRASS needs a directory in which to store its data. '
986  'Create one now if you have not already done so. '
987  'A popular choice is "grassdata", located in '
988  'your home directory.'),
989  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
990 
991  StartUp.OnBrowse(None)
992 
993  return 1
994 
995 if __name__ == "__main__":
996  if os.getenv("GISBASE") is None:
997  sys.exit("Failed to start GUI, GRASS GIS is not running.")
998 
999  import gettext
1000  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
1001 
1002  GRASSStartUp = StartUp(0)
1003  GRASSStartUp.MainLoop()