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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Mar 16 00:21:08 PDT 2018


Author: zarch
Date: 2018-03-16 00:21:08 -0700 (Fri, 16 Mar 2018)
New Revision: 72369

Modified:
   grass/trunk/lib/python/pygrass/gis/__init__.py
Log:
pygrass: Avoid to pass unicode to C functions in mapsets and location

Modified: grass/trunk/lib/python/pygrass/gis/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/gis/__init__.py	2018-03-16 07:20:58 UTC (rev 72368)
+++ grass/trunk/lib/python/pygrass/gis/__init__.py	2018-03-16 07:21:08 UTC (rev 72369)
@@ -12,6 +12,7 @@
 
 import grass.lib.gis as libgis
 from grass.pygrass.errors import GrassError
+from grass.script.utils import encode, decode
 
 libgis.G_gisinit('')
 
@@ -46,7 +47,7 @@
     :return: True if valid else False
     :rtype: str
     """
-    return bool(CHECK_IS[type](join(path, value)))
+    return bool(CHECK_IS[type](encode(join(path, value))))
 
 
 def _check_raise(value, path, type):
@@ -175,7 +176,7 @@
         ..
         """
         return sorted([loc for loc in listdir(self.name)
-                       if libgis.G_is_location(join(self.name, loc))])
+                       if libgis.G_is_location(encode(join(self.name, loc)))])
 
 
 class Location(object):
@@ -254,7 +255,7 @@
         mapsets = [mapset for mapset in self]
         if permissions:
             mapsets = [mapset for mapset in mapsets
-                       if libgis.G_mapset_permissions(mapset)]
+                       if libgis.G_mapset_permissions(encode(mapset))]
         if pattern:
             return fnmatch.filter(mapsets, pattern)
         return mapsets
@@ -402,11 +403,11 @@
 
     def read(self):
         """Return the mapsets in the search path"""
-        with open(self.spath, "a+") as f:
+        with open(self.spath, "ab+") as f:
             lines = f.readlines()
             if lines:
-                return [l.strip() for l in lines]
-        lns = ['PERMANENT', ]
+                return [decode(l.strip()) for l in lines]
+        lns = [u'PERMANENT', ]
         self._write(lns)
         return lns
 
@@ -416,9 +417,9 @@
         :param mapsets: a list of mapset's names
         :type mapsets: list
         """
-        with open(self.spath, "w+") as f:
-            ms = self.location.mapsets()
-            f.write('%s' % '\n'.join([m for m in mapsets if m in ms]))
+        with open(self.spath, "wb+") as f:
+            ms = [decode(m) for m in self.location.mapsets()]
+            f.write(b'\n'.join([encode(m) for m in mapsets if m in ms]))
 
     def add(self, mapset):
         """Add a mapset to the search path
@@ -448,8 +449,9 @@
         :param mapsets: a list of mapset's names
         :type mapsets: list
         """
-        ms = self.location.mapsets()
-        final = self.read()
+        ms = [decode(m) for m in self.location.mapsets()]
+        final = [decode(m) for m in self.read()]
+        mapsets = [decode(m) for m in mapsets]
         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