env.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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([(k.strip(), v.strip())
  16. for k, v in [row.split(':',1) for row in grc if row]])
  17. return env
  18. def get_debug_level():
  19. """Return the debug level"""
  20. debug = get_env().get('DEBUG')
  21. return int(debug) if debug else 0
  22. def G_debug(level, *msg):
  23. """Print or write a debug message, this is a pure python implementation
  24. of the G_debug function in the C API."""
  25. debug_level = get_debug_level()
  26. if debug_level >= level:
  27. dfile = os.environ.get("GRASS_DEBUG_FILE")
  28. fd = sys.stderr if dfile is None else open(dfile, mode='a')
  29. print("D%d/%d: " % (level, debug_level), *msg, end='\n', file=fd)