[GRASS-SVN] r63472 - grass/trunk/lib/python/pygrass

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 10 08:54:07 PST 2014


Author: zarch
Date: 2014-12-10 08:54:06 -0800 (Wed, 10 Dec 2014)
New Revision: 63472

Modified:
   grass/trunk/lib/python/pygrass/utils.py
Log:
pygrass: add function to handle local and system path in modules

Modified: grass/trunk/lib/python/pygrass/utils.py
===================================================================
--- grass/trunk/lib/python/pygrass/utils.py	2014-12-10 11:21:38 UTC (rev 63471)
+++ grass/trunk/lib/python/pygrass/utils.py	2014-12-10 16:54:06 UTC (rev 63472)
@@ -286,6 +286,22 @@
     return path
 
 
+def set_path(modulename, dirname, path='.'):
+    import sys
+    """Set sys.path looking in the the local directory GRASS directories."""
+    pathlib = os.path.join(path, dirname)
+    if os.path.exists(pathlib):
+        # we are running the script from the script directory
+        sys.path.append(os.path.abspath(path))
+    else:
+        # running from GRASS GIS session
+        from grass.pygrass.utils import get_lib_path
+        path = get_lib_path(modulename, dirname)
+        if path is None:
+            raise ImportError("Not able to find the path %s directory." % path)
+        sys.path.append(path)
+
+
 def split_in_chunk(iterable, lenght=10):
     """Split a list in chunk.
 



More information about the grass-commit mailing list