Compare commits

...

3 commits

View file

@ -27,6 +27,7 @@ import os.path
import json
import re
import sys
import xml.etree.ElementTree as et # I don't need anything powerful for this
PROFILE_DB = "profile_db.json"
HOME = os.getenv("HOME", os.getcwd()) # Use relative path in attmpt to salvage things
@ -227,7 +228,7 @@ def get_loaded_mods(cfg: Config) -> list:
if entry.is_symlink() and entry.is_dir():
# only
ret.append(entry.name)
return ret
return sorted(ret, key = lambda x: x.casefold())
def get_available_mods(cfg: Config) -> list:
ret = []
@ -235,7 +236,7 @@ def get_available_mods(cfg: Config) -> list:
for entry in it:
if entry.is_dir() and entry.name != "__pycache__":
ret.append(entry.name)
return ret
return sorted(ret, key = lambda x: x.casefold())
def enable_mod(cfg: Config, mod_name: str):
link_source = os.path.join(cfg.mods_dir, mod_name)
@ -262,24 +263,58 @@ def toggle_mod(cfg: Config, mod_name: str):
raise SevenModsError(f"Could not enable {mod_name}: mod not available.")
os.symlink(link_source, link_dest)
def get_mod_data(mod: str, cfg: Config) -> tuple:
mod_info_path = os.path.join(cfg.mods_dir, mod, "ModInfo.xml")
try:
tree = et.parse(mod_info_path)
except:
return "", "", ""
root = tree.getroot()
name = ""
author = ""
version = ""
for node in root[0]:
if node.tag == "Name":
name = node.attrib["value"]
elif node.tag == "Author":
author = node.attrib["value"]
elif node.tag == "Version":
version = node.attrib["value"]
return name, version, author
def command_list(args: list, cfg: Config, profiles: ModProfiles):
if len(args) == 2:
available_mods = get_available_mods(cfg)
loaded_mods = get_loaded_mods(cfg)
for mod in available_mods:
if mod in loaded_mods:
print(f"\x1b[32m{mod}\x1b[0m *")
else:
print(f"\x1b[31m{mod}\x1b[0m")
elif len(args) == 3 and args[2] == "profiles":
if len(args) == 3 and args[2] == "profiles":
for profile in profiles.profiles:
print(profile)
elif len(args) == 2 or len(args) == 3:
available_mods = get_available_mods(cfg)
loaded_mods = get_loaded_mods(cfg)
if len(args) == 3 and args[2] == "-l":
mod_info = [get_mod_data(i, cfg) for i in available_mods]
ts1 = len(max(available_mods, key = lambda x: len(x), default = 3)) + 1
pretty_names = [i[0] for i in mod_info]
ts2 = len(max(pretty_names, key = lambda x: len(x), default = 4)) + 1
versions = [i[1] for i in mod_info]
ts3 = len(max(versions, key = lambda x: len(x), default = 7)) + 1
authors = [i[2] for i in mod_info]
ts4 = len(max(authors, key = lambda x: len(x), default = 6)) + 1
print("".join(["mod".ljust(ts1), "name".ljust(ts2), "version".ljust(ts3), "author".ljust(ts4)]))
for mod, name, version, author in zip(available_mods, pretty_names, versions, authors):
if mod in loaded_mods:
print(f"\x1b[32m{mod.ljust(ts1)}{name.ljust(ts2)}{version.ljust(ts3)}{author.ljust(ts4)}\x1b[0m *")
else:
print(f"\x1b[31m{mod.ljust(ts1)}{name.ljust(ts2)}{version.ljust(ts3)}{author.ljust(ts4)}\x1b[0m")
else:
for mod in available_mods:
if mod in loaded_mods:
print(f"\x1b[32m{mod}\x1b[0m *")
else:
print(f"\x1b[31m{mod}\x1b[0m")
else:
print(f"Usage: {args[0]} {args[1]} [profiles]")
def command_enable(args: list, cfg: Config, profiles: ModProfiles):
args = [a.rstrip("/") for a in args]
for mod in args[2:]:
if not mod in ("-a", "-A", "*"):
enable_mod(cfg, mod)
@ -292,6 +327,7 @@ def command_enable(args: list, cfg: Config, profiles: ModProfiles):
break
def command_disable(args: list, cfg: Config, profiles: ModProfiles):
args = [a.rstrip("/") for a in args]
for mod in args[2:]:
if not mod in ("-a", "-A", "*"):
disable_mod(cfg, mod)
@ -302,6 +338,7 @@ def command_disable(args: list, cfg: Config, profiles: ModProfiles):
break
def command_toggle(args: list, cfg: Config, profiles: ModProfiles):
args = [a.rstrip("/") for a in args]
for mod in args[2:]:
toggle_mod(cfg, mod)
@ -320,6 +357,17 @@ def command_load(args: list, cfg: Config, profiles: ModProfiles):
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:
del profiles.profiles[prof]
profiles.save_mod_profiles()
else:
print(f"Cannot delete non-existant profile: {prof}")
else:
print(f"Usage: {args[0]} {args[1]} <profile-name>")
def command_configure(args: list, cfg: Config, profiles: ModProfiles):
prompt_configuration_cli()
@ -335,6 +383,7 @@ def parse_input(args: list, cfg: Config, profiles: ModProfiles):
"toggle" : command_toggle,
"save" : command_save,
"load" : command_load,
"delete" : command_delete,
"config" : command_configure,
"help" : command_help,
"--help" : command_help}