""":organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""importsysfromcubicwebimportwarningdeflines(path,comments=None):result=[]forlineinopen(path,'U'):line=line.strip()iflineand(commentsisNoneornotline.startswith(comments)):result.append(line)returnresultdefread_config(config_file):"""read the instance 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:forlineinlines(config_file,comments='#'):try:option,value=line.split('=',1)exceptValueError:option=line.strip().lower()ifoption[0]=='[':# start a sectionsection=option[1:-1]assertnotconfig.has_key(section), \'Section %s is defined more than once'%sectionconfig[section]=current={}continueprint>>sys.stderr,'ignoring malformed line\n%r'%linecontinueoption=option.strip().replace(' ','_')value=value.strip()current[option]=valueorNoneexceptIOError,ex:warning('missing or non readable configuration file %s (%s)',config_file,ex)returnconfigdefenv_path(env_var,default,name):returndefaultdefcreate_dir(*args):raiseRuntimeError()