Add: 增加从template.ini更新当前配置的功能

每次增加新功能后, 不用手动修改ini了
This commit is contained in:
LmeSzinc 2020-04-28 13:48:32 +08:00
parent ef5885767e
commit c5f1cc4ffb

View File

@ -12,6 +12,43 @@ from module.logger import logger, pyw_name
running = True running = True
def update_config_from_template(config, file):
"""
Args:
config (configparser.ConfigParser):
Returns:
configparser.ConfigParser:
"""
template = configparser.ConfigParser(interpolation=None)
template.read_file(codecs.open(f'./config/template.ini', "r", "utf8"))
changed = False
# Update section.
for section in template.sections():
if not config.has_section(section):
config.add_section(section)
changed = True
for section in config.sections():
if not template.has_section(section):
config.remove_section(section)
changed = True
# Update option
for section in template.sections():
for option in template.options(section):
if not config.has_option(section, option):
config.set(section, option, value=template.get(section, option))
changed = True
for section in config.sections():
for option in config.options(section):
if not template.has_option(section, option):
config.remove_option(section, option)
changed = True
# Save
if changed:
config.write(codecs.open(file, "w+", "utf8"))
return config
@Gooey( @Gooey(
optional_cols=2, optional_cols=2,
program_name=pyw_name.capitalize(), program_name=pyw_name.capitalize(),
@ -42,6 +79,8 @@ def main(ini_name=''):
shutil.copy('./config/template.ini', config_file) shutil.copy('./config/template.ini', config_file)
config.read_file(codecs.open(config_file, "r", "utf8")) config.read_file(codecs.open(config_file, "r", "utf8"))
config = update_config_from_template(config, file=config_file)
event_folder = [dic_eng_to_chi.get(f, f) for f in os.listdir('./campaign') if f.startswith('event_')][::-1] event_folder = [dic_eng_to_chi.get(f, f) for f in os.listdir('./campaign') if f.startswith('event_')][::-1]
saved_config = {} saved_config = {}