[GRASS-SVN] r57489 - grass/trunk/lib/python/pygrass/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 23 04:36:25 PDT 2013


Author: zarch
Date: 2013-08-23 04:36:25 -0700 (Fri, 23 Aug 2013)
New Revision: 57489

Modified:
   grass/trunk/lib/python/pygrass/gis/__init__.py
Log:
Add visible mappsets

Modified: grass/trunk/lib/python/pygrass/gis/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/gis/__init__.py	2013-08-23 11:35:27 UTC (rev 57488)
+++ grass/trunk/lib/python/pygrass/gis/__init__.py	2013-08-23 11:36:25 UTC (rev 57489)
@@ -322,3 +322,51 @@
 
     def path(self):
         return join(self.gisdbase, self.location, self.name)
+
+
+class VisibleMapset(object):
+    def __init__(self, mapset, location='', gisdbase=''):
+        self.mapset = mapset
+        self.location = Location(location, gisdbase)
+        self._list = []
+        self.spath = join(self.location.path(), self.mapset, 'SEARCH_PATH')
+
+    def __repr__(self):
+        return repr(self.read())
+
+    def __iter__(self):
+        for mapset in self.read():
+            yield mapset
+
+    def read(self):
+        with open(self.spath, "a+") as f:
+            lines = f.readlines()
+            #lines.remove('')
+            if lines:
+                return [l.strip() for l in lines]
+        lns = ['PERMANENT', ]
+        self.write(lns)
+        return lns
+
+    def write(self, mapsets):
+        with open(self.spath, "a+") as f:
+            ms = self.location.mapsets()
+            f.write('%s' % '\n'.join([m for m in mapsets if m in ms]))
+
+    def add(self, mapset):
+        if mapset in self.location:
+            with open(self.spath, "a+") as f:
+                f.write('\n%s' % mapset)
+        else:
+            raise TypeError('Mapset not found')
+
+    def remove(self, mapset):
+        mapsets = self.read()
+        mapsets.remove(mapset)
+        self.write(mapsets)
+
+    def extend(self, mapsets):
+        ms = self.location.mapsets()
+        final = self.read()
+        final.extend([m for m in mapsets if m in ms and m not in final])
+        self.write(final)



More information about the grass-commit mailing list