[GRASS-SVN] r54052 - grass/trunk/lib/python/pygrass/vector

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 26 08:09:07 PST 2012


Author: zarch
Date: 2012-11-26 08:09:06 -0800 (Mon, 26 Nov 2012)
New Revision: 54052

Modified:
   grass/trunk/lib/python/pygrass/vector/__init__.py
   grass/trunk/lib/python/pygrass/vector/abstract.py
   grass/trunk/lib/python/pygrass/vector/geometry.py
Log:
Fix, before to modify the attribute table check if we are in the current mapset.

Modified: grass/trunk/lib/python/pygrass/vector/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/__init__.py	2012-11-26 16:08:51 UTC (rev 54051)
+++ grass/trunk/lib/python/pygrass/vector/__init__.py	2012-11-26 16:09:06 UTC (rev 54052)
@@ -13,6 +13,7 @@
 # import pygrass modules
 #
 from pygrass.errors import GrassError
+from pygrass.functions import getenv
 
 import geometry
 from abstract import Info
@@ -296,7 +297,8 @@
         if vtype in _GEOOBJ.keys():
             if _GEOOBJ[vtype] is not None:
                 return (_GEOOBJ[vtype](v_id=indx, c_mapinfo=self.c_mapinfo,
-                                       table=self.table)
+                                       table=self.table,
+                                       write=self.write)
                         for indx in xrange(1, self.number_of(vtype) + 1))
         else:
             keys = "', '".join(sorted(_GEOOBJ.keys()))
@@ -364,7 +366,8 @@
                                              c_mapinfo=self.c_mapinfo,
                                              c_points=c_points,
                                              c_cats=c_cats,
-                                             table=self.table)
+                                             table=self.table,
+                                             write=self.write)
         else:
             raise ValueError('The index must be >0, %r given.' % feature_id)
 

Modified: grass/trunk/lib/python/pygrass/vector/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/abstract.py	2012-11-26 16:08:51 UTC (rev 54051)
+++ grass/trunk/lib/python/pygrass/vector/abstract.py	2012-11-26 16:09:06 UTC (rev 54052)
@@ -262,6 +262,7 @@
             raise OpenError(str_err % openvect)
         # istantiate the table
         self.table = self.get_table(link_id=self.link_id)
+        self.write = self.mapset == functions.getenv("MAPSET")
 
     def get_table(self, link_id=None, link_name=None,):
         if link_id is None and link_name is None and len(self.dblinks) == 0:

Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py	2012-11-26 16:08:51 UTC (rev 54051)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py	2012-11-26 16:09:06 UTC (rev 54052)
@@ -13,6 +13,8 @@
 import grass.lib.gis as libgis
 import grass.lib.vector as libvect
 
+from pygrass.errors import GrassError
+
 from basic import Ilist, Bbox, Cats
 import sql
 
@@ -110,10 +112,11 @@
 
 
 class Attrs(object):
-    def __init__(self, v_id, table):
+    def __init__(self, v_id, table, write=False):
         self.id = v_id
         self.table = table
         self.cond = "%s=%d" % (self.table.key, self.id)
+        self.write = write
 
     def __getitem__(self, key):
         """Return the value stored in the attribute table. ::
@@ -136,12 +139,17 @@
             >>> attrs['LABEL'] = 'New Label'
 
         .."""
-        #UPDATE {tname} SET {new_col} = {old_col} WHERE {condition}
-        self.table.execute(sql.UPDATE_WHERE.format(tname=self.table.name,
-                                                   new_col=key,
-                                                   old_col=repr(value),
-                                                   condition=self.cond))
-        #self.table.conn.commit()
+        if self.write:
+            #UPDATE {tname} SET {new_col} = {old_col} WHERE {condition}
+            self.table.execute(sql.UPDATE_WHERE.format(tname=self.table.name,
+                                                       new_col=key,
+                                                       old_col=repr(value),
+                                                       condition=self.cond))
+            #self.table.conn.commit()
+        else:
+            str_err = "You can only read the attributes if the map is \
+in another mapset"
+            raise GrassError(str_err)
 
     def __dict__(self):
         """Reurn a dict of the attribute table row."""
@@ -175,7 +183,7 @@
     >>> geo1 = Geo(c_points=points, c_cats=cats)
     """
     def __init__(self, v_id=None, c_mapinfo=None, c_points=None, c_cats=None,
-                 table=None):
+                 table=None, write=False):
         self.id = v_id  # vector id
         self.c_mapinfo = c_mapinfo
 
@@ -193,7 +201,7 @@
 
         # set the attributes
         if table:
-            self.attrs = Attrs(self.id, table)
+            self.attrs = Attrs(self.id, table, write)
 
     def is_with_topology(self):
         if self.c_mapinfo is not None:



More information about the grass-commit mailing list