goa/overrides/toolsutils.py
author sylvain.thenault@logilab.fr
Tue, 17 Feb 2009 16:41:46 +0100
branchtls-sprint
changeset 664 04154a2b238d
parent 0 b97547f5f1fa
child 1977 606923dff11b
permissions -rw-r--r--
drop unused support of .required, no more necessary .accepts check, and extresources_registerer

import sys
from cubicweb import warning

def lines(path, comments=None):
    result = []
    for line in open(path, 'U'):
        line = line.strip()
        if line and (comments is None or not line.startswith(comments)):
            result.append(line)
    return result

def read_config(config_file):
    """read the application configuration from a file and return it as a
    dictionnary

    :type config_file: str
    :param config_file: path to the configuration file

    :rtype: dict
    :return: a dictionary with specified values associated to option names 
    """
    config = current = {}
    try:
        for line in lines(config_file, comments='#'):
            try:
                option, value = line.split('=', 1)
            except ValueError:
                option = line.strip().lower()
                if option[0] == '[':
                    # start a section
                    section = option[1:-1]
                    assert not config.has_key(section), \
                           'Section %s is defined more than once' % section
                    config[section] = current = {}
                    continue
                print >> sys.stderr, 'ignoring malformed line\n%r' % line
                continue
            option = option.strip().replace(' ', '_')
            value = value.strip()
            current[option] = value or None
    except IOError, ex:
        warning('missing or non readable configuration file %s (%s)',
                config_file, ex)
    return config

def env_path(env_var, default, name):
    return default

def create_dir(*args):
    raise RuntimeError()