[GRASS-SVN] r56144 - in grass/trunk/lib/python/pygrass: raster vector

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 6 06:58:55 PDT 2013


Author: zarch
Date: 2013-05-06 06:58:55 -0700 (Mon, 06 May 2013)
New Revision: 56144

Modified:
   grass/trunk/lib/python/pygrass/raster/abstract.py
   grass/trunk/lib/python/pygrass/vector/abstract.py
Log:
Fix exist method of vector and raster classes and fix the with statement when try to open a map that does not exist

Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py	2013-05-06 12:33:11 UTC (rev 56143)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py	2013-05-06 13:58:55 UTC (rev 56144)
@@ -188,8 +188,11 @@
             self.info = Info(self.name, self.mapset)
 
     def __enter__(self):
-        self.open('r')
-        return self
+        if self.exist():
+            self.open('r')
+            return self
+        else:
+            raise ValueError("Raster not found.")
 
     def __exit__(self, exc_type, exc_value, traceback):
         self.close()
@@ -303,8 +306,8 @@
         if self.name:
             if self.mapset == '':
                 mapset = functions.get_mapset_raster(self.name, self.mapset)
-                self.mapset = mapset
-                return True
+                self.mapset = mapset if mapset else ''
+                return True if mapset else False
             return bool(functions.get_mapset_raster(self.name, self.mapset))
         else:
             return False

Modified: grass/trunk/lib/python/pygrass/vector/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/abstract.py	2013-05-06 12:33:11 UTC (rev 56143)
+++ grass/trunk/lib/python/pygrass/vector/abstract.py	2013-05-06 13:58:55 UTC (rev 56144)
@@ -91,8 +91,11 @@
         self.layer = layer
 
     def __enter__(self):
-        self.open('r')
-        return self
+        if self.exist():
+            self.open('r')
+            return self
+        else:
+            raise ValueError('Vector not found.')
 
     def __exit__(self, exc_type, exc_value, traceback):
         self.close()
@@ -263,14 +266,14 @@
 
     def exist(self):
         """Return if the Vector exists or not"""
-        if self._name:
-            self.mapset = functions.get_mapset_vector(self._name, self.mapset)
+        if self.name:
+            if self.mapset == '':
+                mapset = functions.get_mapset_vector(self.name, self.mapset)
+                self.mapset = mapset if mapset else ''
+                return True if mapset else False
+            return bool(functions.get_mapset_vector(self.name, self.mapset))
         else:
             return False
-        if self.mapset:
-            return True
-        else:
-            return False
 
     def is_open(self):
         """Return if the Vector is open"""



More information about the grass-commit mailing list