File: env.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (36 lines) | stat: -rw-r--r-- 1,069 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 = dict([(k.strip(), v.strip())
                    for k, v in [row.split(':',1) 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)