Added delete button and warnings to GUI. Also some stability fixes.

This commit is contained in:
Patrick Marsee 2023-02-02 22:44:08 -05:00
parent 1dfe472a57
commit 86f6cc2659
2 changed files with 74 additions and 23 deletions

View file

@ -59,16 +59,37 @@ Valid commands are:
class Config:
defaults = {"mods_dir": MODS_DIR,
"seven_dir": "",
"warn_load": True,
"warn_save": True,
"warn_delete": True}
def __init__(self, **kwargs):
if "mods_dir" in kwargs:
self.mods_dir = kwargs["mods_dir"]
self.settings = Config.defaults.copy()
for key in kwargs:
self.set_val(key, kwargs[key])
def __getattr__(self, name: str):
if name in self.settings:
return self.settings[name]
else:
self.mods_dir = MODS_DIR
if "seven_dir" in kwargs:
self.seven_dir = kwargs["seven_dir"]
raise AttributeError(f"Config has no attribute named '{name}'.")
def set_val(self, name: str, val: str):
if name in ("warn_load", "warn_save", "warn_delete"):
# boolean
if val.casefold() in ("on", "true", "yes", "y"):
self.settings[name] = True
elif val.casefold() in ("off", "false", "no", "n"):
self.settings[name] = False
else:
raise ValueError(f"Input cannot be interpreted as boolean: '{val}'")
elif name in Config.defaults:
# siletly remove newlines, replace with spaces
# This is to prevent weird things.
self.settings[name] = val.replace("\n", " ")
else:
self.seven_dir = get_seven_days_install_path()
raise ValueError(f"No configuration setting: '{name}'")
class ModProfiles:
@ -203,7 +224,7 @@ def prompt_configuration_cli():
def save_config(cfg: Config) -> bool:
try:
with open(CONFIG_FILE, 'w') as config_file:
config_file.write(f"mods_dir={cfg.mods_dir}\nseven_dir={cfg.seven_dir}\n")
config_file.write("\n".join([f"{key}={cfg.settings[key]}" for key in cfg.settings]) + "\n")
return True
except RuntimeError:
return False
@ -345,22 +366,26 @@ def command_toggle(args: list, cfg: Config, profiles: ModProfiles):
def command_save(args: list, cfg: Config, profiles: ModProfiles):
if len(args) > 2:
for prof in args[2:]:
profiles.profiles[prof] = get_loaded_mods(cfg)
profiles.save_mod_profiles()
if len(prof) == 0 or prof.isspace():
raise seven_mods.SevenModsError("Profile must have a name.")
if not cfg.warn_save or prof not in profiles.profiles or input("Overwrite? y/N").casefold() == "y":
profiles.profiles[prof] = get_loaded_mods(cfg)
profiles.save_mod_profiles()
else:
print(f"Usage: {args[0]} {args[1]} <profile-name>")
def command_load(args: list, cfg: Config, profiles: ModProfiles):
if len(args) == 3 and args[2] in profiles.profiles:
command_disable(args[:2] + ["*"], cfg, profiles)
command_enable(args[:2] + profiles.profiles[args[2]], cfg, profiles)
if not cfg.warn_load or input("Load? y/N").casefold() == "y":
command_disable(args[:2] + ["*"], cfg, profiles)
command_enable(args[:2] + profiles.profiles[args[2]], cfg, profiles)
else:
print(f"Usage: {args[0]} {args[1]} <profile-name>")
def command_delete(args: list, cfg: Config, profiles: ModProfiles):
if len(args) > 2:
for prof in args[2:]:
if prof in profiles.profiles:
if prof in profiles.profiles and (not cfg.warn_delete or input("Delete? y/N").casefold() == "y"):
del profiles.profiles[prof]
profiles.save_mod_profiles()
else:
@ -369,7 +394,14 @@ def command_delete(args: list, cfg: Config, profiles: ModProfiles):
print(f"Usage: {args[0]} {args[1]} <profile-name>")
def command_configure(args: list, cfg: Config, profiles: ModProfiles):
prompt_configuration_cli()
if len(args) == 2:
prompt_configuration_cli()
elif len(args) == 4:
if args[2] in Config.defaults:
cfg.set_val(args[2], args[3])
save_config(cfg)
else:
print(f"Usage: {args[0]} {args[1]} [name value]")
def command_help(args: list, cfg: Config, profiles: ModProfiles):
print(HELP_TEXT)