[GRASS-SVN] r65336 - grass/trunk/lib/python/pygrass/modules/interface

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 28 08:58:21 PDT 2015


Author: zarch
Date: 2015-05-28 08:58:21 -0700 (Thu, 28 May 2015)
New Revision: 65336

Added:
   grass/trunk/lib/python/pygrass/modules/interface/env.py
Modified:
   grass/trunk/lib/python/pygrass/modules/interface/Makefile
   grass/trunk/lib/python/pygrass/modules/interface/module.py
Log:
pygrass: rm get_msgr from modules to avoid the ctypes use

Modified: grass/trunk/lib/python/pygrass/modules/interface/Makefile
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/Makefile	2015-05-26 23:48:20 UTC (rev 65335)
+++ grass/trunk/lib/python/pygrass/modules/interface/Makefile	2015-05-28 15:58:21 UTC (rev 65336)
@@ -9,7 +9,7 @@
 PGDIR = $(GDIR)/pygrass
 DSTDIR= $(PGDIR)/modules/interface
 
-MODULES = docstring read typedict flag parameter module
+MODULES = docstring read typedict flag parameter module env
 
 PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
 PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)

Added: grass/trunk/lib/python/pygrass/modules/interface/env.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/env.py	                        (rev 0)
+++ grass/trunk/lib/python/pygrass/modules/interface/env.py	2015-05-28 15:58:21 UTC (rev 65336)
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu May 28 17:41:32 2015
+
+ at author: pietro
+"""
+from __future__ import print_function
+import os
+import sys
+
+
+def get_env():
+    """Parse the GISRC file and return the GRASS variales"""
+    gisrc = os.environ.get('GISRC')
+    if gisrc is None:
+        raise RuntimeError('You are not in a GRASS session, GISRC not found.')
+    with open(gisrc, mode='r') as grc:
+        env = {k.strip(): v.strip() for k, v in [row.split(':')
+                                                 for row in grc if row]}
+    return env
+
+
+def get_debug_level():
+    """Return the debug level"""
+    debug = get_env().get('DEBUG')
+    return int(debug) if debug else 0
+
+
+def G_debug(level, *msg):
+    """Print or write a debug message, this is a pure python implementation
+    of the G_debug function in the C API."""
+    debug_level = get_debug_level()
+    if debug_level >= level:
+        dfile = os.environ.get("GRASS_DEBUG_FILE")
+        fd = sys.stderr if dfile is None else open(dfile, mode='a')
+        print("D%d/%d: " % (level, debug_level), *msg, end='\n', file=fd)


Property changes on: grass/trunk/lib/python/pygrass/modules/interface/env.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Modified: grass/trunk/lib/python/pygrass/modules/interface/module.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/module.py	2015-05-26 23:48:20 UTC (rev 65335)
+++ grass/trunk/lib/python/pygrass/modules/interface/module.py	2015-05-28 15:58:21 UTC (rev 65336)
@@ -3,13 +3,8 @@
                         with_statement, print_function, unicode_literals)
 import sys
 from multiprocessing import cpu_count
-
-if sys.version_info[0] == 2:
-    from itertools import izip_longest as zip_longest
-else:
-    from itertools import zip_longest
+import time
 from xml.etree.ElementTree import fromstring
-import time
 
 from grass.exceptions import CalledModuleError, GrassError, ParameterError
 from grass.script.core import Popen, PIPE
@@ -18,9 +13,15 @@
 from .flag import Flag
 from .typedict import TypeDict
 from .read import GETFROMTAG, DOC
-from grass.pygrass.messages import get_msgr
+from .env import G_debug
 
 
+if sys.version_info[0] == 2:
+    from itertools import izip_longest as zip_longest
+else:
+    from itertools import zip_longest
+
+
 def _get_bash(self, *args, **kargs):
     return self.get_bash()
 
@@ -677,7 +678,7 @@
         termination. The handling of stdout and stderr must then be done
         outside of this function.
         """
-        get_msgr().debug(1, self.get_bash())
+        G_debug(1, self.get_bash())
         if self.inputs['stdin'].value:
             self.stdin = self.inputs['stdin'].value
             self.stdin_ = PIPE



More information about the grass-commit mailing list