env.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. Created on Thu May 28 17:41:32 2015
  3. @author: pietro
  4. """
  5. from __future__ import print_function
  6. import os
  7. import sys
  8. def get_env():
  9. """Parse the GISRC file and return the GRASS variales"""
  10. gisrc = os.environ.get("GISRC")
  11. if gisrc is None:
  12. raise RuntimeError("You are not in a GRASS session, GISRC not found.")
  13. with open(gisrc, mode="r") as grc:
  14. env = dict(
  15. [
  16. (k.strip(), v.strip())
  17. for k, v in [row.split(":", 1) for row in grc if row]
  18. ]
  19. )
  20. return env
  21. def get_debug_level():
  22. """Return the debug level"""
  23. debug = get_env().get("DEBUG")
  24. return int(debug) if debug else 0
  25. def G_debug(level, *msg):
  26. """Print or write a debug message, this is a pure python implementation
  27. of the G_debug function in the C API."""
  28. debug_level = get_debug_level()
  29. if debug_level >= level:
  30. dfile = os.environ.get("GRASS_DEBUG_FILE")
  31. fd = sys.stderr if dfile is None else open(dfile, mode="a")
  32. print("D%d/%d: " % (level, debug_level), *msg, end="\n", file=fd)