env.py 1.1 KB

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