[GRASS-SVN] r67669 - in grass/trunk/lib/python: pygrass script

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 25 11:57:01 PST 2016


Author: wenzeslaus
Date: 2016-01-25 11:57:01 -0800 (Mon, 25 Jan 2016)
New Revision: 67669

Modified:
   grass/trunk/lib/python/pygrass/utils.py
   grass/trunk/lib/python/script/utils.py
Log:
pythonlib: move the package path functions for modules from pygrass where influenced by ctypes requirement to grass.script

Modified: grass/trunk/lib/python/pygrass/utils.py
===================================================================
--- grass/trunk/lib/python/pygrass/utils.py	2016-01-25 16:20:33 UTC (rev 67668)
+++ grass/trunk/lib/python/pygrass/utils.py	2016-01-25 19:57:01 UTC (rev 67669)
@@ -285,33 +285,13 @@
 
 def get_lib_path(modname, libname=None):
     """Return the path of the libname contained in the module.
+
+    .. deprecated:: 7.1
+        Use :func:`grass.script.core.get_lib_path` instead.
     """
-    from os.path import isdir, join
-    from os import getenv
+    return grasscore.get_lib_path(modname=modname, libname=libname)
 
-    if isdir(join(getenv('GISBASE'), 'etc', modname)):
-        path = join(os.getenv('GISBASE'), 'etc', modname)
-    elif getenv('GRASS_ADDON_BASE') and libname and \
-            isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)):
-        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)
-    elif getenv('GRASS_ADDON_BASE') and \
-            isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname)):
-        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
-    elif getenv('GRASS_ADDON_BASE') and \
-            isdir(join(getenv('GRASS_ADDON_BASE'), modname, modname)):
-        path = join(os.getenv('GRASS_ADDON_BASE'), modname, modname)
-    elif libname and isdir(join('..', libname)): # used by g.extension compilation process
-        path = join('..', libname)
-    elif isdir(join('..', 'etc', modname)):      # used by g.extension compilation process
-        path = join('..', 'etc', modname)
-    elif isdir(join('etc', modname)):            # used by g.extension compilation process
-        path = join('etc', modname)
-    else:
-        path = None
 
-    return path
-
-
 def set_path(modulename, dirname=None, path='.'):
     """Set sys.path looking in the the local directory GRASS directories.
 
@@ -320,73 +300,11 @@
                     libraries, default None
     :param path: string with the path to reach the dirname locally.
 
-    Example
-    --------
-
-    "set_path" example working locally with the source code of a module
-    (r.green) calling the function with all the parameters. Below it is
-    reported the directory structure on the r.green module.
-
-    ::
-
-        grass_prompt> pwd
-        ~/Download/r.green/r.green.hydro/r.green.hydro.financial
-
-        grass_prompt> tree ../../../r.green
-        ../../../r.green
-        ├── ...
-        ├── libgreen
-        │   ├── pyfile1.py
-        │   └── pyfile2.py
-        └── r.green.hydro
-           ├── Makefile
-           ├── libhydro
-           │   ├── pyfile1.py
-           │   └── pyfile2.py
-           ├── r.green.hydro.*
-           └── r.green.hydro.financial
-               ├── Makefile
-               ├── ...
-               └── r.green.hydro.financial.py
-
-        21 directories, 125 files
-
-    in the source code the function is called with the following parameters: ::
-
-        set_path('r.green', 'libhydro', '..')
-        set_path('r.green', 'libgreen', os.path.join('..', '..'))
-
-    when we are executing the module: r.green.hydro.financial locally from
-    the command line:  ::
-
-        grass_prompt> python r.green.hydro.financial.py --ui
-
-    In this way we are executing the local code even if the module was already
-    installed as grass-addons and it is available in GRASS standards path.
-
-    The function is cheching if the dirname is provided and if the
-    directory exists and it is available using the path
-    provided as third parameter, if yes add the path to sys.path to be
-    importable, otherwise it will check on GRASS GIS standard paths.
-
+    .. deprecated:: 7.1
+        Use :func:`grass.script.core.set_path` instead.
     """
-    import sys
-    # TODO: why dirname is checked first - the logic should be revised
-    pathlib = None
-    if dirname:
-        pathlib = os.path.join(path, dirname)
-    if pathlib and os.path.exists(pathlib):
-        # we are running the script from the script directory, therefore
-        # we add the path to sys.path to reach the directory (dirname)
-        sys.path.append(os.path.abspath(path))
-    else:
-        # running from GRASS GIS session
-        path = get_lib_path(modulename, dirname)
-        if path is None:
-            pathname = os.path.join(modulename, dirname) if dirname else modulename
-            raise ImportError("Not able to find the path '%s' directory "
-                              "(current dir '%s')." % (pathname, os.getcwd()))
-        sys.path.append(path)
+    return grasscore.set_path(modulename=modulename, dirname=dirname,
+                              path=path)
 
 
 def split_in_chunk(iterable, length=10):

Modified: grass/trunk/lib/python/script/utils.py
===================================================================
--- grass/trunk/lib/python/script/utils.py	2016-01-25 16:20:33 UTC (rev 67668)
+++ grass/trunk/lib/python/script/utils.py	2016-01-25 19:57:01 UTC (rev 67669)
@@ -263,3 +263,109 @@
     convert = lambda text: int(text) if text.isdigit() else text.lower()
     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
     return sorted(l, key=alphanum_key)
+
+
+def get_lib_path(modname, libname=None):
+    """Return the path of the libname contained in the module.
+    """
+    from os.path import isdir, join
+    from os import getenv
+
+    if isdir(join(getenv('GISBASE'), 'etc', modname)):
+        path = join(os.getenv('GISBASE'), 'etc', modname)
+    elif getenv('GRASS_ADDON_BASE') and libname and \
+            isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)):
+        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)
+    elif getenv('GRASS_ADDON_BASE') and \
+            isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname)):
+        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
+    elif getenv('GRASS_ADDON_BASE') and \
+            isdir(join(getenv('GRASS_ADDON_BASE'), modname, modname)):
+        path = join(os.getenv('GRASS_ADDON_BASE'), modname, modname)
+    elif libname and isdir(join('..', libname)): # used by g.extension compilation process
+        path = join('..', libname)
+    elif isdir(join('..', 'etc', modname)):      # used by g.extension compilation process
+        path = join('..', 'etc', modname)
+    elif isdir(join('etc', modname)):            # used by g.extension compilation process
+        path = join('etc', modname)
+    else:
+        path = None
+
+    return path
+
+
+def set_path(modulename, dirname=None, path='.'):
+    """Set sys.path looking in the the local directory GRASS directories.
+
+    :param modulename: string with the name of the GRASS module
+    :param dirname: string with the directory name containing the python
+                    libraries, default None
+    :param path: string with the path to reach the dirname locally.
+
+    Example
+    --------
+
+    "set_path" example working locally with the source code of a module
+    (r.green) calling the function with all the parameters. Below it is
+    reported the directory structure on the r.green module.
+
+    ::
+
+        grass_prompt> pwd
+        ~/Download/r.green/r.green.hydro/r.green.hydro.financial
+
+        grass_prompt> tree ../../../r.green
+        ../../../r.green
+        ├── ...
+        ├── libgreen
+        │   ├── pyfile1.py
+        │   └── pyfile2.py
+        └── r.green.hydro
+           ├── Makefile
+           ├── libhydro
+           │   ├── pyfile1.py
+           │   └── pyfile2.py
+           ├── r.green.hydro.*
+           └── r.green.hydro.financial
+               ├── Makefile
+               ├── ...
+               └── r.green.hydro.financial.py
+
+        21 directories, 125 files
+
+    in the source code the function is called with the following parameters: ::
+
+        set_path('r.green', 'libhydro', '..')
+        set_path('r.green', 'libgreen', os.path.join('..', '..'))
+
+    when we are executing the module: r.green.hydro.financial locally from
+    the command line:  ::
+
+        grass_prompt> python r.green.hydro.financial.py --ui
+
+    In this way we are executing the local code even if the module was already
+    installed as grass-addons and it is available in GRASS standards path.
+
+    The function is cheching if the dirname is provided and if the
+    directory exists and it is available using the path
+    provided as third parameter, if yes add the path to sys.path to be
+    importable, otherwise it will check on GRASS GIS standard paths.
+
+    """
+    import sys
+    # TODO: why dirname is checked first - the logic should be revised
+    pathlib = None
+    if dirname:
+        pathlib = os.path.join(path, dirname)
+    if pathlib and os.path.exists(pathlib):
+        # we are running the script from the script directory, therefore
+        # we add the path to sys.path to reach the directory (dirname)
+        sys.path.append(os.path.abspath(path))
+    else:
+        # running from GRASS GIS session
+        path = get_lib_path(modulename, dirname)
+        if path is None:
+            pathname = os.path.join(modulename, dirname) if dirname else modulename
+            raise ImportError("Not able to find the path '%s' directory "
+                              "(current dir '%s')." % (pathname, os.getcwd()))
+        sys.path.append(path)



More information about the grass-commit mailing list