Browse Source

Add new methods to the Mapset class to set, delete mapsets.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@55598 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 12 years ago
parent
commit
3af41a7ca5
1 changed files with 29 additions and 2 deletions
  1. 29 2
      lib/python/pygrass/gis/__init__.py

+ 29 - 2
lib/python/pygrass/gis/__init__.py

@@ -4,6 +4,7 @@ from __future__ import print_function
 #import os
 from os import listdir
 from os.path import join
+import shutil
 import ctypes as ct
 import fnmatch
 
@@ -59,6 +60,14 @@ def _check(value, path, type):
                                                  join(path, value)))
 
 
+def set_current_mapset(mapset, location=None, gisdbase=None):
+    libgis.G_setenv('MAPSET', mapset)
+    if location:
+        libgis.G_setenv('LOCATION_NAME', location)
+    if gisdbase:
+        libgis.G_setenv('GISDBASEE', gisdbase)
+
+
 def make_mapset(mapset, location=None, gisdbase=None):
     if libgis.G__make_mapset(gisdbase, location, mapset) != 0:
         raise GrassError("I cannot create a new mapset.")
@@ -187,14 +196,17 @@ class Location(object):
     def __repr__(self):
         return 'Location(%r)' % self.name
 
-    def mapsets(self):
+    def mapsets(self, pattern=None):
         """Return a list of the available mapsets. ::
 
             >>> location = Location()
             >>> location.mapsets()
             ['PERMANENT', 'user1']
         """
-        return [mapset for mapset in self]
+        mapsets = [mapset for mapset in self]
+        if pattern:
+            return fnmatch.filter(mapsets, pattern)
+        return mapsets
 
 
 class Mapset(object):
@@ -285,3 +297,18 @@ class Mapset(object):
                 if pattern:
                     return fnmatch.filter(elist, pattern)
                 return elist
+
+    def is_current(self):
+        return (self.name == libgis.G_getenv('MAPSET') and
+                self.location == libgis.G_getenv('LOCATION_NAME') and
+                self.gisdbase == libgis.G_getenv('GISDBASE'))
+
+    def current(self):
+        """Set the mapset as current"""
+        set_current_mapset(self.name, self.location, self.gisdbase)
+
+    def delete(self):
+        """Delete the mapset"""
+        if self.is_current():
+            raise GrassError('The mapset is in use.')
+        shutil.rmtree(join(self.gisdbase, self.location, self.name))