[GRASS-SVN] r35180 - grass/trunk/general/g.mapsets

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jan 3 06:08:19 EST 2009


Author: martinl
Date: 2009-01-03 06:08:18 -0500 (Sat, 03 Jan 2009)
New Revision: 35180

Added:
   grass/trunk/general/g.mapsets/g.mapsets.py
Removed:
   grass/trunk/general/g.mapsets/g.mapsets.tcl
Modified:
   grass/trunk/general/g.mapsets/Makefile
   grass/trunk/general/g.mapsets/dsply_maps.c
   grass/trunk/general/g.mapsets/dsply_path.c
   grass/trunk/general/g.mapsets/local_proto.h
   grass/trunk/general/g.mapsets/main.c
Log:
g.mapsets: new parameter: field separator
	   g.mapsets.tcl -> g.mapsets.py


Modified: grass/trunk/general/g.mapsets/Makefile
===================================================================
--- grass/trunk/general/g.mapsets/Makefile	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/Makefile	2009-01-03 11:08:18 UTC (rev 35180)
@@ -2,7 +2,7 @@
 MODULE_TOPDIR = ../..
 
 PGM = g.mapsets
-GUI = $(ETC)/g.mapsets.tcl
+GUI = $(ETC)/gui/g.mapsets.py
 
 LIBES     = $(GISLIB)
 DEPENDENCIES= $(GISDEP)
@@ -11,6 +11,6 @@
 
 default: cmd $(GUI)
 
-$(ETC)/%: %
+$(ETC)/gui/%: %
 	if [ ! -d $(ETC) ]; then $(MKDIR) $(ETC); fi
 	$(INSTALL) $< $@

Modified: grass/trunk/general/g.mapsets/dsply_maps.c
===================================================================
--- grass/trunk/general/g.mapsets/dsply_maps.c	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/dsply_maps.c	2009-01-03 11:08:18 UTC (rev 35180)
@@ -1,16 +1,19 @@
 #include <string.h>
 #include <stdio.h>
+
+#include <grass/glocale.h>
+
 #include "externs.h"
 
 static int display1(void);
-static int display2(void);
+static int display2(const char *fs);
 
-int display_available_mapsets(int verbose)
+int display_available_mapsets(const char* fs)
 {
-    if (verbose)
+    if (!fs)
 	display1();
     else
-	display2();
+	display2(fs);
 
     return 0;
 }
@@ -19,7 +22,7 @@
 {
     int n;
 
-    fprintf(stdout, "Available mapsets:");
+    fprintf(stdout, _("Available mapsets:"));
     for (n = 0; n < nmapsets; n++) {
 	if (n % 4)
 	    fprintf(stdout, " ");
@@ -29,13 +32,13 @@
     }
     fprintf(stdout, "\n");
     if (nmapsets == 0)
-	fprintf(stdout, "** no mapsets **\n");
+	fprintf(stdout, _("** no mapsets **\n"));
     fprintf(stdout, "\n");
 
     return 0;
 }
 
-static int display2(void)
+static int display2(const char *fs)
 {
     int nleft, len, n;
     char *name;
@@ -47,7 +50,9 @@
 	    fprintf(stdout, "\n");
 	    nleft = 78;
 	}
-	fprintf(stdout, "%s ", name);
+	fprintf(stdout, "%s", name);
+	if (n < nmapsets-1)
+	    fprintf(stdout, "%s", fs);
 	nleft -= (len + 1);
     }
     fprintf(stdout, "\n");

Modified: grass/trunk/general/g.mapsets/dsply_path.c
===================================================================
--- grass/trunk/general/g.mapsets/dsply_path.c	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/dsply_path.c	2009-01-03 11:08:18 UTC (rev 35180)
@@ -1,8 +1,9 @@
 #include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
 #include "externs.h"
-#include <grass/gis.h>
 
-int display_mapset_path(int verbose)
+int display_mapset_path(const char *fs)
 {
     int n;
     int map;			/* pointer into list of available mapsets */
@@ -11,22 +12,22 @@
     int len;
     int nleft;
 
-    if (verbose) {
+    if (!fs) {
 	/* account for largest mapset number in offset value */
 	for (n = nmapsets; n /= 10; offset++) ;
 
-	fprintf(stdout, "Your mapset search list:\n");
+	fprintf(stdout, _("Your mapset search list:\n"));
 	ncurr_mapsets = 0;
     }
 
     nleft = 78;
-    for (n = 0; name = G__mapset_name(n); n++) {
+    for (n = 0; (name = G__mapset_name(n)); n++) {
 	/* match each mapset to its numeric equivalent */
-	if (verbose) {
+	if (!fs) {
 	    for (map = 0; map < nmapsets && strcmp(mapset_name[map], name);
 		 map++) ;
 	    if (map == nmapsets)
-		G_fatal_error("%s not found in mapset list:  call greg",
+		G_fatal_error(_("<%s> not found in mapset list"),
 			      name);
 	}
 
@@ -36,7 +37,7 @@
 	    nleft = 78;
 	}
 
-	if (verbose) {
+	if (!fs) {
 	    if (n)
 		fprintf(stdout, ", ");
 	    fprintf(stdout, "%s <%d>", name, map + 1);
@@ -45,12 +46,14 @@
 	    ++ncurr_mapsets;
 	}
 	else {
-	    fprintf(stdout, "%s ", name);
+	    fprintf(stdout, "%s", name);
+	    if (G__mapset_name(n+1))
+		fprintf(stdout, "%s", fs);
 	    nleft -= (len + 1);
 	}
     }
     fprintf(stdout, "\n");
-    if (verbose)
+    if (!fs)
 	fprintf(stdout, "\n");
 
     return 0;

Added: grass/trunk/general/g.mapsets/g.mapsets.py
===================================================================
--- grass/trunk/general/g.mapsets/g.mapsets.py	                        (rev 0)
+++ grass/trunk/general/g.mapsets/g.mapsets.py	2009-01-03 11:08:18 UTC (rev 35180)
@@ -0,0 +1,160 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import pwd
+
+import grass
+
+import wx
+import wx.lib.mixins.listctrl as listmix
+
+class MapsetsFrame(wx.Frame):
+    def __init__(self):
+        wx.Frame.__init__(self, parent = None, id = wx.ID_ANY,
+                          title = "Check mapsets to access")
+        
+        
+        self.SetMinSize((350, 400))
+
+        sizer = wx.BoxSizer(wx.VERTICAL)
+        
+        # list of mapsets
+        self.mapsetlb = CheckListMapset(parent=self)
+        self.mapsetlb.LoadData()
+        
+        sizer.Add(item=self.mapsetlb, proportion=1,
+                  flag=wx.ALL | wx.EXPAND, border=5)
+
+        
+        # dialog buttons
+        line = wx.StaticLine(parent=self, id=wx.ID_ANY,
+                             style=wx.LI_HORIZONTAL)
+        sizer.Add(item=line, proportion=0,
+                  flag=wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border=5)
+        
+        btnsizer = wx.StdDialogButtonSizer()
+        btnOK = wx.Button(self, wx.ID_OK)
+        btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
+        btnOK.SetToolTipString("Close dialog and apply changes")
+        btnOK.SetDefault()
+        btnsizer.AddButton(btnOK)
+        
+        btnCancel = wx.Button(self, wx.ID_CANCEL)
+        btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
+        btnCancel.SetToolTipString("Close dialog and ignore changes")
+        btnsizer.AddButton(btnCancel)
+        btnsizer.Realize()
+        
+        sizer.Add(item=btnsizer, proportion=0,
+                  flag=wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
+        
+        # do layout
+        self.Layout()
+        self.SetSizer(sizer)
+        sizer.Fit(self)
+
+    def GetMapsets(self):
+        """Get list of checked mapsets"""
+        ms = []
+        i = 0
+        for mset in self.mapsetlb.mapsets:
+            if self.mapsetlb.IsChecked(i):
+                ms.append(mset)
+            i += 1
+
+        return ms
+
+    def OnCancel(self, event):
+        """Button 'Cancel' pressed"""
+        self.Close()
+        
+    def OnOK(self, event):
+        """Button 'OK' pressed"""
+        mapsets = ','.join(self.GetMapsets())
+
+        grass.run_command('g.mapsets',
+                          quiet = True,
+                          mapset = mapsets)
+        
+        self.Close()
+        
+class CheckListMapset(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.CheckListCtrlMixin):
+    """List of mapset/owner/group"""
+    def __init__(self, parent, pos=wx.DefaultPosition,
+                 log=None):
+        self.parent = parent
+        
+        wx.ListCtrl.__init__(self, parent, wx.ID_ANY,
+                             style=wx.LC_REPORT)
+        listmix.CheckListCtrlMixin.__init__(self)
+        self.log = log
+        
+        # setup mixins
+        listmix.ListCtrlAutoWidthMixin.__init__(self)
+    
+    def LoadData(self):
+        """Load data into list"""
+        self.InsertColumn(0, 'Mapset')
+        self.InsertColumn(1, 'Owner')
+        self.InsertColumn(2, 'Group')
+
+        gisenv = grass.gisenv()
+        locationPath = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'])
+        self.curr_mapset = gisenv['MAPSET']
+        
+        ret = grass.read_command('g.mapsets',
+                                 flags = 'l',
+                                 fs = ';')
+        self.mapsets = []
+        if ret:
+            self.mapsets = ret.replace('\n', '').split(';')
+            
+
+        ret = grass.read_command('g.mapsets',
+                                 flags = 'p',
+                                 fs = ';')
+        mapsets_access = []
+        if ret:
+            mapsets_access = ret.replace('\n', '').split(';')
+            
+        for mapset in self.mapsets:
+            index = self.InsertStringItem(sys.maxint, mapset)
+            mapsetPath = os.path.join(locationPath,
+                                      mapset)
+            stat_info = os.stat(mapsetPath)
+	    if os.name in ('posix', 'mac'):
+                self.SetStringItem(index, 1, "%s" % pwd.getpwuid(stat_info.st_uid)[0])
+                # FIXME: get group name
+                self.SetStringItem(index, 2, "%-8s" % stat_info.st_gid) 
+	    else:
+                # FIXME: no pwd under MS Windows (owner: 0, group: 0)
+                self.SetStringItem(index, 1, "%-8s" % stat_info.st_uid)
+                self.SetStringItem(index, 2, "%-8s" % stat_info.st_gid)
+
+            if mapset in mapsets_access:
+                self.CheckItem(self.mapsets.index(mapset), True)
+            
+        self.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE)
+        self.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE)
+
+    def OnCheckItem(self, index, flag):
+        """Mapset checked/unchecked"""
+        mapset = self.mapsets[index]
+        if mapset == 'PERMANENT' or mapset == self.curr_mapset:
+            self.CheckItem(index, True)
+    
+class MyApp(wx.App):
+    def OnInit(self):
+        frame = MapsetsFrame()
+        
+        frame.CentreOnScreen()
+        frame.Show()
+        
+        self.SetTopWindow(frame)
+        
+        return True
+
+if __name__ == "__main__":
+    app = MyApp(0)
+    app.MainLoop()


Property changes on: grass/trunk/general/g.mapsets/g.mapsets.py
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/x-python
Name: svn:keywords
   + Author Date Id
Name: svn:eol-style
   + native

Deleted: grass/trunk/general/g.mapsets/g.mapsets.tcl
===================================================================
--- grass/trunk/general/g.mapsets/g.mapsets.tcl	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/g.mapsets.tcl	2009-01-03 11:08:18 UTC (rev 35180)
@@ -1,94 +0,0 @@
-#!/bin/sh
-# the next line restarts using wish \
-exec $GRASS_WISH "$0" "$@"
-
-# Put current mapset and PERMANENT at the top of the list, mapsets are later written to SEARCH_PATH
-# and searched in this order
-
-lappend auto_path $env(GISBASE)/bwidget
-package require -exact BWidget 1.2.1
-
-set env(GISDBASE) [exec g.gisenv get=GISDBASE]
-set env(LOCATION_NAME) [exec g.gisenv get=LOCATION_NAME]
-set env(MAPSET) [exec g.gisenv get=MAPSET]
-
-# Include the select dialog code because it defines scroll bindings
-source $env(GISBASE)/etc/gtcltk/select.tcl
-
-proc set_mapsets { } {
-    global ms_ch ms_name nms
-
-    set first 1
-    set arg "mapset="
-    for {set i 0} {$i < $nms} {incr i 1} {
-        set ms $ms_name($i)
-        if { $ms_ch($ms) } { 
-            if { !$first } {  append arg "," }
-            append arg "$ms"
-            set first 0
-        }
-    }
-    exec g.mapsets $arg
-}
-
-set mainw [TitleFrame .mainw -relief sunken -borderwidth 1 \
-	-text "Check mapsets to access" -fg mediumblue -side center -baseline top]
-set mainfr [$mainw getframe]
-pack $mainw -fill both -expand yes
-
-set sw [ScrolledWindow $mainfr.sw -relief flat -borderwidth 2]
-set sf [ScrollableFrame $mainfr.sf -width 175 -height 200]
-
-$sw setwidget $sf
-
-bind_scroll $sf
-
-pack $sw -fill both -expand yes
-
-set sframe [$sf getframe]
-
-
-# Add current mapset and PERMANENT
-set current [exec g.gisenv get=MAPSET]
-if { $current == "PERMANENT" } {
-	set msts $current
-} else {
-	set msts [list $current PERMANENT]
-}
-
-set tmp_msts [ lsort [split [exec g.mapsets -l] " \n"] ]
-foreach ms $tmp_msts {
-    if { $ms == $current || $ms == "PERMANENT" } { continue }
-    lappend msts $ms 
-}
-
-set nms 0
-foreach ms $msts {
-    if { [string length $ms] == 0 } { continue }
-    set fr [frame $sframe.f$nms]
-    pack $fr -side top -anchor w 
-    set cb [checkbutton $fr.cb -text "$ms" -variable ms_ch($ms) -command set_mapsets]
-    if { $ms == $current } { 
-		$cb configure -state disabled 
-    }
-    pack $cb -side left
-    set ms_name($nms) $ms
-    incr nms
-}
-
-# check in selected
-set msts [split [exec g.mapsets -p] " \n"]
-foreach ms $msts {
-    if { $ms == $current } { 
-        set ms_ch($ms) 1
-        continue
-    }
-    if { [string length $ms] == 0 } { continue }
-    set ms_ch($ms) 1
-}
-
-set close [button $mainw.close -text "OK" -command { exit } ]
-
-
-#pack $mainw
-pack $close -side bottom

Modified: grass/trunk/general/g.mapsets/local_proto.h
===================================================================
--- grass/trunk/general/g.mapsets/local_proto.h	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/local_proto.h	2009-01-03 11:08:18 UTC (rev 35180)
@@ -1,8 +1,8 @@
 /* dsply_maps.c */
-int display_available_mapsets(int);
+int display_available_mapsets(const char *);
 
 /* dsply_path.c */
-int display_mapset_path(int);
+int display_mapset_path(const char *);
 
 /* get_maps.c */
 int get_available_mapsets(void);

Modified: grass/trunk/general/g.mapsets/main.c
===================================================================
--- grass/trunk/general/g.mapsets/main.c	2009-01-02 23:49:02 UTC (rev 35179)
+++ grass/trunk/general/g.mapsets/main.c	2009-01-03 11:08:18 UTC (rev 35180)
@@ -8,8 +8,9 @@
  *               Hamish Bowman <hamish_nospam yahoo.com>, 
  *               Markus Neteler <neteler itc.it>, 
  *               Moritz Lennert <mlennert club.worldonline.be>
+ *               Martin Landa <landa.martin gmail.com>
  * PURPOSE:      set current mapset path
- * COPYRIGHT:    (C) 1994-2008 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1994-2009 by the GRASS Development Team
  *
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -46,12 +47,16 @@
     int no_tokens;
     FILE *fp;
     char path[GPATH_MAX];
-    struct GModule *module;
-    struct Option *opt1, *opt2, *opt3;
-    struct Flag *print;
-    struct Flag *list;
-    struct Flag *tcl;
 
+    struct GModule *module;    
+    struct _opt {
+	struct Option *mapset, *add, *remove;
+	struct Option *fs;
+	struct Flag *print;
+	struct Flag *list;
+	struct Flag *dialog;
+    } opt;
+
     G_gisinit(argv[0]);
 
     module = G_define_module();
@@ -61,59 +66,62 @@
 	  "search path, affecting the user's access to data existing "
 	  "under the other GRASS mapsets in the current location.");
 
-    opt1 = G_define_option();
-    opt1->key = "mapset";
-    opt1->type = TYPE_STRING;
-    opt1->required = NO;
-    opt1->multiple = YES;
-    opt1->description = _("Name(s) of existing mapset(s)");
+    opt.mapset = G_define_option();
+    opt.mapset->key = "mapset";
+    opt.mapset->type = TYPE_STRING;
+    opt.mapset->required = NO;
+    opt.mapset->multiple = YES;
+    opt.mapset->description = _("Name(s) of existing mapset(s)");
 
-    opt2 = G_define_option();
-    opt2->key = "addmapset";
-    opt2->type = TYPE_STRING;
-    opt2->required = NO;
-    opt2->multiple = YES;
-    opt2->description =
+    opt.add = G_define_option();
+    opt.add->key = "addmapset";
+    opt.add->type = TYPE_STRING;
+    opt.add->required = NO;
+    opt.add->multiple = YES;
+    opt.add->description =
 	_("Name(s) of existing mapset(s) to add to search list");
 
-    opt3 = G_define_option();
-    opt3->key = "removemapset";
-    opt3->type = TYPE_STRING;
-    opt3->required = NO;
-    opt3->multiple = YES;
-    opt3->description =
+    opt.remove = G_define_option();
+    opt.remove->key = "removemapset";
+    opt.remove->type = TYPE_STRING;
+    opt.remove->required = NO;
+    opt.remove->multiple = YES;
+    opt.remove->description =
 	_("Name(s) of existing mapset(s) to remove from search list");
+    
+    opt.fs = G_define_standard_option(G_OPT_F_SEP);
+    opt.fs->answer = " ";
+    
+    opt.list = G_define_flag();
+    opt.list->key = 'l';
+    opt.list->description = _("List all available mapsets");
 
-    list = G_define_flag();
-    list->key = 'l';
-    list->description = _("List all available mapsets");
+    opt.print = G_define_flag();
+    opt.print->key = 'p';
+    opt.print->description = _("Print current mapset search path");
 
-    print = G_define_flag();
-    print->key = 'p';
-    print->description = _("Print current mapset search path");
+    opt.dialog = G_define_flag();
+    opt.dialog->key = 's';
+    opt.dialog->description = _("Show mapset selection dialog");
 
-    tcl = G_define_flag();
-    tcl->key = 's';
-    tcl->description = _("Show mapset selection dialog");
-
     Path[0] = '\0';
     nchoices = 0;
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    if (list->answer) {
+    if (opt.list->answer) {
 	get_available_mapsets();
-	display_available_mapsets(0);
+	display_available_mapsets(opt.fs->answer);
     }
 
-    if (tcl->answer) {
-	sprintf(path, "%s/etc/g.mapsets.tcl", G_gisbase());
-	G_spawn(path, "g.mapsets.tcl", NULL);
+    if (opt.dialog->answer) {
+	sprintf(path, "%s/etc/gui/g.mapsets.py", G_gisbase());
+	G_spawn("python", "g.mapsets.py", path, NULL);
     }
 
-    if (opt1->answer) {
-	for (ptr = opt1->answers; *ptr != NULL; ptr++) {
+    if (opt.mapset->answer) {
+	for (ptr = opt.mapset->answers; *ptr != NULL; ptr++) {
 	    const char *mapset;
 
 	    mapset = *ptr;
@@ -126,7 +134,7 @@
     }
 
     /* add to existing search path */
-    if (opt2->answer) {
+    if (opt.add->answer) {
 	const char *oldname;
 
 	Path[0] = '\0';
@@ -138,7 +146,7 @@
 	}
 
 	/* fetch and add new mapsets from param list */
-	for (ptr = opt2->answers; *ptr != NULL; ptr++) {
+	for (ptr = opt.add->answers; *ptr != NULL; ptr++) {
 	    char *mapset;
 
 	    mapset = *ptr;
@@ -155,7 +163,7 @@
     }
 
     /* remove from existing search path */
-    if (opt3->answer) {
+    if (opt.remove->answer) {
 	const char *oldname;
 
 	Path[0] = '\0';
@@ -164,7 +172,7 @@
 	for (n = 0; (oldname = G__mapset_name(n)); n++) {
 	    int found = 0;
 
-	    for (ptr = opt3->answers; *ptr; ptr++)
+	    for (ptr = opt.remove->answers; *ptr; ptr++)
 		if (strcmp(oldname, *ptr) == 0)
 		    found = 1;
 
@@ -183,7 +191,10 @@
     /* stuffem sets nchoices */
 
     if (nchoices == 0) {
-	goto DISPLAY;
+	if (opt.print->answer)
+	    display_mapset_path(opt.fs->answer);
+
+	exit(EXIT_SUCCESS);
     }
 
     /* note I'm assuming that mapsets cannot have ' 's in them */
@@ -232,9 +243,5 @@
     fclose(fp);
     G_free_tokens(tokens);
 
-  DISPLAY:
-    if (print->answer)
-	display_mapset_path(0);
-
     exit(EXIT_SUCCESS);
 }



More information about the grass-commit mailing list