def write_config(main_module, config_path=None, **extra): ''' Write a configuration file This function collects all options and option groups then updates their default values, and then write a configuration file .. seealso:: Function :py:func:`setup_parser` Peforms option collection Function :py:func:`map_module_to_program` Determines script name for module :Parameters: main_module : module Reference to main module config_path : str, optional Path to write configuration file extra : dict New default values for the options :Returns: output : str Output filename of the configuration file ''' #_logger.addHandler(logging.StreamHandler()) main_template = file_processor if file_processor.supports(main_module) else None if hasattr(main_module, 'flags'): extra.update(main_module.flags()) external_prog=None if 'description' in extra: external_prog = map_module_to_program(main_module.__name__) description = extra['description'] description=[" "+s.strip()[1:] if len(s.strip())>0 and s.strip()[0] == '-' else '# '+s.strip() for s in description.split("\n")] description="\n".join([s for s in description]) description = description.replace('%prog', '%(prog)s') extra['description'] = description%dict(prog=external_prog) parser = setup_parser(main_module, main_template, external_prog=external_prog, **extra)[0] parser.change_default(**extra) name = main_module.__name__ off = name.rfind('.') if off != -1: name = name[off+1:] if config_path is not None: output = os.path.join(config_path, name+".cfg") else: output = name+".cfg" parser.write(output) #, options) return output
def generate_settings_tree(main_module, config_path=None, **extra): ''' Collect options, groups and values This function collects all options and option groups then updates their default values, and then returns the options, groups and values. .. seealso:: Function :py:func:`setup_parser` Peforms option collection Function :py:func:`map_module_to_program` Determines script name for module :Parameters: main_module : module Reference to main module extra : dict New default values for the options :Returns: options : list List of options that do not belong to a group option_groups : list List of option groups values : object Object where each field is named for an option and holds is corresponding value ''' main_template = file_processor if file_processor.supports(main_module) else None if hasattr(main_module, 'flags'): extra.update(main_module.flags()) external_prog = map_module_to_program(main_module.__name__) if 'description' in extra: extra['description'] = extra['description'].replace('%prog', '%(prog)s')%dict(prog=external_prog) parser, dependents = setup_parser(main_module, main_template, external_prog=external_prog, **extra) parser.change_default(**extra) name = main_module.__name__ off = name.rfind('.') if off != -1: name = name[off+1:] if config_path is not None: output = os.path.join(config_path, name+".cfg") elif hasattr(parser.get_default_values(), 'config_path'): output = os.path.join(parser.get_default_values().config_path, name+".cfg") else: output = name+".cfg" options = parser.parse_file(fin=output) if os.path.exists(output) else None return program(main_module, main_template, dependents, parser, output, options, external_prog)
def read_config(main_module, config_path=None, **extra): ''' Read in option values from a configuration file This function collects all options and option groups, and then reads in their values from a configuration file .. seealso:: Function :py:func:`setup_parser` Peforms option collection Function :py:func:`map_module_to_program` Determines script name for module :Parameters: main_module : module Reference to main module config_path : str, optional Path to write configuration file extra : dict Unused keyword arguments :Returns: out : dict Option/value pairs ''' main_template = file_processor if file_processor.supports(main_module) else None if hasattr(main_module, 'flags'): extra.update(main_module.flags()) external_prog=None if 'description' in extra: external_prog = map_module_to_program(main_module.__name__) description = extra['description'].replace('%prog', '%(prog)s') extra['description'] = description%dict(prog=external_prog) parser = setup_parser(main_module, main_template, external_prog=external_prog, **extra)[0] parser.change_default(**extra) name = main_module.__name__ off = name.rfind('.') if off != -1: name = name[off+1:] if config_path is not None: output = os.path.join(config_path, name+".cfg") else: output = name+".cfg" param = {} if os.path.exists(output): param = vars(parser.parse_file(fin=output)) return param
def collect_file_dependents(main_module, config_path=None, **extra): ''' Collect all filename options into input and output dependents This function collects all filename options and divides them into two groups: input and output. .. seealso:: Function :py:func:`setup_parser` Peforms option collection :Parameters: main_module : module Reference to main module config_path : str, optional Path to write configuration file extra : dict New default values for the options :Returns: config_file : str Name of the configuration file input_files : list List of options that do not belong to a group output_files : list List of option groups ''' main_template = file_processor if file_processor.supports(main_module) else None if hasattr(main_module, 'flags'): extra.update(main_module.flags()) parser = setup_parser(main_module, main_template, **extra)[0] parser.change_default(**extra) name = main_module.__name__ off = name.rfind('.') if off != -1: name = name[off+1:] if config_path is not None: output = os.path.join(config_path, name+".cfg") else: output = name+".cfg" return output, parser.collect_dependent_file_options(type_obj='open', required=True, key='_long_opts'), parser.collect_dependent_file_options(type_obj='save', key='_long_opts')
def run_hybrid_program(name, **extra): ''' Main entry point for the program architecture This function proceeds as follows: #. Determine module from name #. Determine if module supports file/group or batch processing #. Collect options, match to values and test validity using :py:func:`parse_and_check_options` #. Set number of threads for OpenMP (optional) #. Invoke entry point to file/group or batch processor :Parameters: name : str Name of calling module (usually __main__) extra : dict Pass on keyword arguments ''' #_logger.addHandler(logging.StreamHandler()) main_module = determine_main(name) main_template = file_processor if file_processor.supports(main_module) else None if hasattr(main_module, 'flags'): extra.update(main_module.flags()) use_version = extra.get('use_version', False) if not use_version and main_template == file_processor: extra['use_version']=True #supports_OMP = extra.get('supports_OMP', False) _logger.debug("Checking options ...") try: args, param, parser = parse_and_check_options(main_module, main_template, **extra) except SystemExit: return except VersionChange: main_module.main() return except settings.OptionValueError: sys.exit(10) except: _logger.exception("Unexpected error occurred") raise _logger.debug("Checking options ... finished.") launch_program(main_module, main_template, args, param, parser, **extra)