def get_params(job_inis, **inputs): """ Parse one or more INI-style config files. :param job_inis: List of configuration files (or list containing a single zip archive) :param inputs: Optionally override some parameters in params['inputs'] :returns: A dictionary of parameters """ if len(job_inis) == 1 and job_inis[0].endswith('.zip'): job_inis = extract_from_zip( job_inis[0], ['job_hazard.ini', 'job_haz.ini', 'job.ini', 'job_risk.ini']) not_found = [ini for ini in job_inis if not os.path.exists(ini)] if not_found: # something was not found raise IOError('File not found: %s' % not_found[0]) cp = configparser.ConfigParser() cp.read(job_inis) # directory containing the config files we're parsing job_ini = os.path.abspath(job_inis[0]) base_path = decode(os.path.dirname(job_ini)) params = dict(base_path=base_path, inputs={'job_ini': job_ini}) for sect in cp.sections(): for key, value in cp.items(sect): if key.endswith(('_file', '_csv')): if os.path.isabs(value): raise InvalidFile('%s: %s must be a relative path' % (job_ini, value)) input_type, _ext = key.rsplit('_', 1) path = os.path.join(base_path, value) params['inputs'][input_type] = path else: params[key] = value params['inputs'].update(inputs) # override on demand # populate the 'source' list smlt = params['inputs'].get('source_model_logic_tree') if smlt: params['inputs']['source'] = sorted(_get_paths(smlt)) return params
def get_params(job_inis): """ Parse one or more INI-style config files. :param job_inis: List of configuration files (or list containing a single zip archive) :returns: A dictionary of parameters """ if len(job_inis) == 1 and job_inis[0].endswith('.zip'): job_inis = extract_from_zip( job_inis[0], ['job_hazard.ini', 'job_haz.ini', 'job.ini', 'job_risk.ini']) not_found = [ini for ini in job_inis if not os.path.exists(ini)] if not_found: # something was not found raise IOError('File not found: %s' % not_found[0]) cp = configparser.ConfigParser() cp.read(job_inis) # drectory containing the config files we're parsing job_ini = os.path.abspath(job_inis[0]) base_path = os.path.dirname(job_ini) params = dict(base_path=base_path, inputs={'job_ini': job_ini}) for sect in cp.sections(): for key, value in cp.items(sect): if key.endswith(('_file', '_csv')): input_type, _ext = key.rsplit('_', 1) path = value if os.path.isabs(value) else os.path.join( base_path, value) params['inputs'][input_type] = possibly_gunzip(path) else: params[key] = value # populate the 'source' list smlt = params['inputs'].get('source_model_logic_tree') if smlt: params['inputs']['source'] = [ os.path.join(base_path, src_path) for src_path in source.collect_source_model_paths(smlt) ] return params
def get_params(job_inis, **kw): """ Parse one or more INI-style config files. :param job_inis: List of configuration files (or list containing a single zip archive) :param kw: Optionally override some parameters :returns: A dictionary of parameters """ if len(job_inis) == 1 and job_inis[0].endswith('.zip'): job_inis = extract_from_zip( job_inis[0], ['job_hazard.ini', 'job_haz.ini', 'job.ini', 'job_risk.ini']) not_found = [ini for ini in job_inis if not os.path.exists(ini)] if not_found: # something was not found raise IOError('File not found: %s' % not_found[0]) cp = configparser.ConfigParser() cp.read(job_inis) # directory containing the config files we're parsing job_ini = os.path.abspath(job_inis[0]) base_path = decode(os.path.dirname(job_ini)) params = dict(base_path=base_path, inputs={'job_ini': job_ini}) for sect in cp.sections(): _update(params, cp.items(sect), base_path) _update(params, kw.items(), base_path) # override on demand # populate the 'source' list inputs = params['inputs'] smlt = inputs.get('source_model_logic_tree') if smlt: inputs['source'] = sorted(_get_paths(smlt)) elif 'source_model' in inputs: inputs['source'] = [inputs['source_model']] return params