From c5f1cc4ffb05ca9f9a1f5413f5f11830b224a2d4 Mon Sep 17 00:00:00 2001 From: LmeSzinc Date: Tue, 28 Apr 2020 13:48:32 +0800 Subject: [PATCH] =?UTF-8?q?Add:=20=E5=A2=9E=E5=8A=A0=E4=BB=8Etemplate.ini?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BD=93=E5=89=8D=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 每次增加新功能后, 不用手动修改ini了 --- module/config/argparser.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/module/config/argparser.py b/module/config/argparser.py index 6efcc3167..5f34ff922 100644 --- a/module/config/argparser.py +++ b/module/config/argparser.py @@ -12,6 +12,43 @@ from module.logger import logger, pyw_name 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( optional_cols=2, program_name=pyw_name.capitalize(), @@ -42,6 +79,8 @@ def main(ini_name=''): shutil.copy('./config/template.ini', config_file) 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] saved_config = {}