Source code for inicheck.output

import os
import sys
from datetime import date

from .utilities import mk_lst


[docs]def generate_config(config_obj, fname, cli=False): """ Generates a list of strings using the config data then its written to an .ini file Args: config_obj: config object containing data to be outputted fname: String path to the output location for the new config file cli : Boolean value that adds the line "file generated using inicheck.cli", Default = False """ header_len = 80 pg_sep = '#' * header_len # Header surround each commented titles in the ini file section_header = pg_sep + '\n' + ('# {0}\n') + pg_sep # Construct the section strings config_str = "" config_str += pg_sep # File header with specific package option if config_obj.mcfg.header is not None: header = config_obj.mcfg.header.split('\n') for line in header: config_str += ('\n# ' + line) else: config_str += "\n# Configuration File " # Add in the date generated config_str += "\n#\n# Date generated: {0}".format(date.today()) # Generated with inicheck if cli: config_str += "\n#\n# Generated using: inicheck <filename> -w" config_str += "\n#\n# For more inicheck help see:" + \ "\n# http://inicheck.readthedocs.io/en/latest/\n" config = config_obj.cfg mcfg = config_obj.mcfg.cfg # Check to see if section titles were provided has_section_titles = hasattr(config_obj.mcfg, 'titles') # Generate the string for the file, creating them in order. for section in mcfg.keys(): if section in config.keys(): config_str += '\n' * 2 # Add a section header s_hdr = pg_sep if has_section_titles: if section in config_obj.mcfg.titles.keys(): # Add the header s_hdr = section_header.format( config_obj.mcfg.titles[section]) else: config_str += s_hdr config_str += s_hdr config_str += '\n' config_str += '\n[{0}]\n'.format(section) # Add section items and values for k in config[section].keys(): v = config[section][k] if type(v) == list: astr = ", ".join(str(c).strip() for c in v) else: astr = str(v) config_str += "{0:<30} {1:<10}\n".format((k + ':'), astr) # Write out the string generated with open(os.path.abspath(fname), 'w') as f: f.writelines(config_str) f.close()