浏览代码

pygrass: rm get_msgr from modules to avoid the ctypes use

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65336 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 10 年之前
父节点
当前提交
52023e77ba

+ 1 - 1
lib/python/pygrass/modules/interface/Makefile

@@ -9,7 +9,7 @@ GDIR = $(PYDIR)/grass
 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__)

+ 36 - 0
lib/python/pygrass/modules/interface/env.py

@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu May 28 17:41:32 2015
+
+@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)

+ 9 - 8
lib/python/pygrass/modules/interface/module.py

@@ -3,13 +3,8 @@ from __future__ import (nested_scopes, generators, division, absolute_import,
                         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
-from xml.etree.ElementTree import fromstring
 import time
+from xml.etree.ElementTree import fromstring
 
 from grass.exceptions import CalledModuleError, GrassError, ParameterError
 from grass.script.core import Popen, PIPE
@@ -18,7 +13,13 @@ from .parameter import Parameter
 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):
@@ -677,7 +678,7 @@ class Module(object):
         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