Opt: Reduce config writes

This commit is contained in:
LmeSzinc 2022-06-06 23:42:24 +08:00
parent 151ab28fae
commit 3a0b5d5536
4 changed files with 14 additions and 14 deletions

View File

@ -8,7 +8,6 @@ import inflection
from cached_property import cached_property
from module.config.config import AzurLaneConfig, TaskEnd
from module.config.config_updater import ConfigUpdater
from module.config.utils import deep_get, deep_set
from module.exception import *
from module.logger import logger
@ -20,7 +19,6 @@ class AzurLaneAutoScript:
def __init__(self, config_name='alas'):
logger.hr('Start', level=0)
self.config_name = config_name
ConfigUpdater().update_file(config_name)
@cached_property
def config(self):

View File

@ -228,7 +228,7 @@ class AzurLaneConfig(ConfigUpdater, ManualConfig, GeneratedConfig, ConfigWatcher
logger.info(f'Save config {filepath_config(self.config_name)}, {dict_to_kv(self.modified)}')
# Don't use self.modified = {}, that will create a new object.
self.modified.clear()
write_file(filepath_config(self.config_name), data=self.data)
self.write_file(self.config_name, data=self.data)
def update(self):
self.load()

View File

@ -545,18 +545,19 @@ class ConfigUpdater:
return new
def read_file(self, config_name):
def read_file(self, config_name, is_template=False):
"""
Read and update config file.
Args:
config_name (str): ./config/{file}.json
is_template (bool):
Returns:
dict:
"""
old = read_file(filepath_config(config_name))
return self.config_update(old, is_template=config_name == 'template')
return self.config_update(old, is_template=is_template)
@staticmethod
def write_file(config_name, data):
@ -570,17 +571,18 @@ class ConfigUpdater:
write_file(filepath_config(config_name), data)
@timer
def update_file(self, config_name):
def update_file(self, config_name, is_template=False):
"""
Read, update and write config file.
Args:
config_name (str): ./config/{file}.json
is_template (bool):
Returns:
dict:
"""
data = self.read_file(config_name)
data = self.read_file(config_name, is_template=is_template)
self.write_file(config_name, data)
return data
@ -602,4 +604,4 @@ if __name__ == '__main__':
os.chdir(os.path.join(os.path.dirname(__file__), '../../'))
ConfigGenerator().generate()
ConfigUpdater().update_file('template')
ConfigUpdater().update_file('template', is_template=True)

View File

@ -214,7 +214,7 @@ class AlasGUI(Frame):
self.set_title(t(f"Task.{task}.name"))
put_scope("_groups", [put_none(), put_scope("groups"), put_scope("navigator")])
config = State.config_updater.update_file(self.alas_name)
config = State.config_updater.read_file(self.alas_name)
for group, arg_dict in deep_iter(self.ALAS_ARGS[task], depth=1):
self.set_group(group, arg_dict, config, task)
self.set_navigator(group)
@ -405,7 +405,7 @@ class AlasGUI(Frame):
d = self.modified_config_queue.get(timeout=1)
modified[self.idx_to_path[d["name"]]] = d["value"]
except queue.Empty:
config = read_file(filepath_config(config_name))
config = State.config_updater.read_file(config_name)
for k, v in modified.copy().items():
valuetype = deep_get(self.ALAS_ARGS, k + ".valuetype")
v = parse_pin_value(v, valuetype)
@ -451,7 +451,7 @@ class AlasGUI(Frame):
logger.info(
f"Save config {filepath_config(config_name)}, {dict_to_kv(modified)}"
)
write_file(filepath_config(config_name), config)
State.config_updater.write_file(config_name, config)
modified.clear()
valid.clear()
invalid.clear()
@ -595,7 +595,7 @@ class AlasGUI(Frame):
scope="log_scroll_btn",
)
config = State.config_updater.update_file(self.alas_name)
config = State.config_updater.read_file(self.alas_name)
for group, arg_dict in deep_iter(self.ALAS_ARGS[task], depth=1):
self.set_group(group, arg_dict, config, task)
@ -854,8 +854,8 @@ class AlasGUI(Frame):
origin = pin["AddAlas_copyfrom"]
if name not in alas_instance():
r = read_file(filepath_config(origin))
write_file(filepath_config(name), r)
r = State.config_updater.read_file(origin)
State.config_updater.write_file(name, r)
self.set_aside()
self.active_button("aside", self.alas_name)
close_popup()