def create_new_config_specify_file(filename, build_filepath, **kwargs): configs = SfoClusterConfigureMethod.query_by_filename(filename=filename) if configs: sections = SfoClusterConfigureMethod.query_group_by_config_group( filename) config_parser = ConfigParser() config_parser.default_section = 'null' no_section_dict = OrderedDict() # 由于默认编辑的时候DEFAULT的section是不合法的,所以将默认的DEFAULT改成 !DEFAULT字符串即可 for config in configs: section = config.config_group if section: if not config_parser.has_section(section): config_parser.add_section(section) if config.config_key: if kwargs.get(config.config_value): config_parser.set(section, config.config_key, kwargs[config.config_value]) else: config_parser.set(section, config.config_key, config.config_value) else: if kwargs.get(config.config_value): no_section_dict[config.config_key] = kwargs[ config.config_value] else: no_section_dict[config.config_key] = config.config_value if no_section_dict: with open(os.path.join(build_filepath, filename), "w") as fp: for key, value in no_section_dict.items(): fp.write('%s%s\n' % (key, value)) if config_parser.sections(): if len(sections) != len(config_parser.sections()): config_parser.write( open(os.path.join(build_filepath, filename), "a+")) else: config_parser.write( open(os.path.join(build_filepath, filename), "w")) return True else: return None
import os from configparser import ConfigParser config = ConfigParser() config.read(os.path.join(os.path.dirname(__file__), '..', 'config.ini')) config.default_section = 'MAIN'
try: will_create_files.remove('rsyncd.conf') except ValueError, error: try: will_create_files.remove('rsync.conf') except ValueError, error: access_logger.error(str(error)) access_logger.error(str(error)) else: will_create_files.extend(set(filenames) - set(_conf_files)) #根据角色的不同过滤发送的文件 for file in will_create_files: configs = SfoClusterConfigureMethod.query_by_filename(file) sections = SfoClusterConfigureMethod.query_group_by_config_group(file) config_parser = ConfigParser() config_parser.default_section = 'null' no_section_dict = OrderedDict() #由于默认编辑的时候DEFAULT的section是不合法的,所以将默认的DEFAULT改成 !DEFAULT字符串即可 for config in configs: section = config.config_group if section: if not config_parser.has_section(section): config_parser.add_section(section) if config.config_key: if kwargs.get(config.config_value): config_parser.set(section, config.config_key, kwargs[config.config_value]) else: config_parser.set(section, config.config_key, config.config_value) else: