def __expandGangaSystemVars__(self, value): return expandgangasystemvars(None,value)
def read_ini_files(filenames, system_vars): """ Return a ConfigParser object which contains all options from the sequence of files (which are parsed from left-to-right). Apply special rules for PATH-like variables - see transform_PATH_option() """ import Ganga.Utility.logging logger = getLogger() logger.debug('reading ini files: %s', filenames) main = make_config_parser(system_vars) # load all config files and apply special rules for PATH-like variables # note: main.read(filenames) cannot be used because of that if isinstance(filenames, str): filenames = [filenames] for f in filenames: if f is None or f == '': continue cc = make_config_parser(system_vars) logger.info('reading config file %s', f) try: with open(f) as file_f: cc.readfp(file_f) except Exception as x: logger.warning('Exception reading config file %s', x) for sec in cc.sections(): if not main.has_section(sec): main.add_section(sec) for name in cc.options(sec): try: value = cc.get(sec, name) except (ConfigParser.InterpolationMissingOptionError, ConfigParser.InterpolationSyntaxError) as err: logger.debug("Parse Error!:\n %s" % err) value = cc.get(sec, name, raw=True) #raise err for localvar in re.finditer('\$\{[^${}]*\}', value): localvarstripped = re.sub(r'[^\w]', '', localvar.group(0)) try: value = value.replace(localvar.group(0), cc.get(sec, localvarstripped)) except Exception as err: Ganga.Utility.logging.log_unknown_exception() logger.debug('The variable \"' + localvarstripped + '\" is referenced but not defined in the ') logger.debug('[' + sec + '] configuration section of ' + f) logger.debug("err: %s" % err) # do not put the DEFAULTS into the sections (no need) if name in cc.defaults(): continue # special rules (NOT APPLIED IN DEFAULT SECTION): try: current_value = main.get(sec, name) except ConfigParser.NoOptionError: current_value = None except (ConfigParser.InterpolationMissingOptionError, ConfigParser.InterpolationSyntaxError) as err: logger.debug("Parse Error!:\n %s" % err) logger.debug("Failed to expand, Importing value %s:%s as raw" % (sec, name)) current_value = main.get(sec, name, raw=True) current_value = current_value.replace('%', '%%') #raise err value = transform_PATH_option(name, value, current_value) from Ganga.Utility.Config import expandgangasystemvars value = expandgangasystemvars(None, value) # check for the use of environment vars re.search('\$\{[^${}]*\}', value) # matches on ${...} for envvar in re.finditer('\$\$[^${}]*\$\$', value): # yeah, if the same variable appears more than once, we'll look it up in the # environment more than once too...but that's not too # arduous. envvar = envvar.group(0) envvarclean = envvar.strip('$') # is env variable logger.debug('looking for ' + str(envvarclean) + ' in the shell environment') if envvarclean in os.environ: envval = os.environ[envvarclean] logger.debug(str(envvarclean) + ' is set as ' + envval + ' in the shell environment') value = value.replace(envvar, envval) else: logger.debug('The configuration file ' + f + ' references an unset environment variable: ' + str(envvarclean)) # FIXME: strip trailing whitespaces -- SHOULD BE DONE BEFORE IF # AT ALL? value = value.rstrip() value = value.replace('%', '%%') try: main.set(sec, name, value) except Exception as err: value = value.replace('%', '%%') logger.debug("Error Setting %s" % err) try: main.set(sec, name, value) except Exception as err2: logger.debug("Error setting #2: %s" % err2) raise err return main