Browse Source

pygrass: set default open mode for vector to 'r' (sync with raster)

Now it's possible to type:

```
with Vector('streams') as data:
    data.is_open()
```

-> True


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@74397 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 6 years ago
parent
commit
b46d10ed86
1 changed files with 20 additions and 7 deletions
  1. 20 7
      lib/python/pygrass/vector/abstract.py

+ 20 - 7
lib/python/pygrass/vector/abstract.py

@@ -77,6 +77,7 @@ class Info(object):
         self.c_mapinfo = ctypes.pointer(libvect.Map_info())
         self.c_mapinfo = ctypes.pointer(libvect.Map_info())
         self._topo_level = 1
         self._topo_level = 1
         self._class_name = 'Vector'
         self._class_name = 'Vector'
+        self._mode = 'r'
         self.overwrite = False
         self.overwrite = False
         self.date_fmt = '%a %b  %d %H:%M:%S %Y'
         self.date_fmt = '%a %b  %d %H:%M:%S %Y'
 
 
@@ -87,6 +88,17 @@ class Info(object):
     def __exit__(self, exc_type, exc_value, traceback):
     def __exit__(self, exc_type, exc_value, traceback):
         self.close()
         self.close()
 
 
+    def _get_mode(self):
+        return self._mode
+
+    def _set_mode(self, mode):
+        if mode.upper() not in ('R', 'W'):
+            str_err = _("Mode type: {0} not supported ('r', 'w')")
+            raise ValueError(str_err.format(mode))
+        self._mode = mode
+
+    mode = property(fget=_get_mode, fset=_set_mode)
+
     def _get_name(self):
     def _get_name(self):
         """Private method to obtain the Vector name"""
         """Private method to obtain the Vector name"""
         return self._name
         return self._name
@@ -322,26 +334,27 @@ class Info(object):
         See more examples in the documentation of the ``read`` and ``write``
         See more examples in the documentation of the ``read`` and ``write``
         methods
         methods
         """
         """
+        self.mode = mode if mode else self.mode
         with_z = libvect.WITH_Z if with_z else libvect.WITHOUT_Z
         with_z = libvect.WITH_Z if with_z else libvect.WITHOUT_Z
         # check if map exists or not
         # check if map exists or not
-        if not self.exist() and mode != 'w':
+        if not self.exist() and self.mode != 'w':
             raise OpenError("Map <%s> not found." % self._name)
             raise OpenError("Map <%s> not found." % self._name)
         if libvect.Vect_set_open_level(self._topo_level) != 0:
         if libvect.Vect_set_open_level(self._topo_level) != 0:
             raise OpenError("Invalid access level.")
             raise OpenError("Invalid access level.")
         # update the overwrite attribute
         # update the overwrite attribute
         self.overwrite = overwrite if overwrite is not None else self.overwrite
         self.overwrite = overwrite if overwrite is not None else self.overwrite
         # check if the mode is valid
         # check if the mode is valid
-        if mode not in ('r', 'rw', 'w'):
+        if self.mode not in ('r', 'rw', 'w'):
             raise ValueError("Mode not supported. Use one of: 'r', 'rw', 'w'.")
             raise ValueError("Mode not supported. Use one of: 'r', 'rw', 'w'.")
 
 
         # check if the map exist
         # check if the map exist
-        if self.exist() and mode in ('r', 'rw'):
+        if self.exist() and self.mode in ('r', 'rw'):
             # open in READ mode
             # open in READ mode
-            if mode == 'r':
+            if self.mode == 'r':
                 openvect = libvect.Vect_open_old2(self.c_mapinfo, self.name,
                 openvect = libvect.Vect_open_old2(self.c_mapinfo, self.name,
                                                   self.mapset, str(layer))
                                                   self.mapset, str(layer))
             # open in READ and WRITE mode
             # open in READ and WRITE mode
-            elif mode == 'rw':
+            elif self.mode == 'rw':
                 openvect = libvect.Vect_open_update2(self.c_mapinfo, self.name,
                 openvect = libvect.Vect_open_update2(self.c_mapinfo, self.name,
                                                      self.mapset, str(layer))
                                                      self.mapset, str(layer))
 
 
@@ -349,11 +362,11 @@ class Info(object):
             self.dblinks = DBlinks(self.c_mapinfo)
             self.dblinks = DBlinks(self.c_mapinfo)
 
 
         # If it is opened in write mode
         # If it is opened in write mode
-        if mode == 'w':
+        if self.mode == 'w':
             openvect = libvect.Vect_open_new(self.c_mapinfo, self.name, with_z)
             openvect = libvect.Vect_open_new(self.c_mapinfo, self.name, with_z)
             self.dblinks = DBlinks(self.c_mapinfo)
             self.dblinks = DBlinks(self.c_mapinfo)
 
 
-        if mode in ('w', 'rw') and tab_cols:
+        if self.mode in ('w', 'rw') and tab_cols:
             # create a link
             # create a link
             link = Link(layer,
             link = Link(layer,
                         link_name if link_name else self.name,
                         link_name if link_name else self.name,