diff --git a/README.md b/README.md index f59a197..ca3a4ad 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ Updating only a single file? Use SyndiShanX's online tool instead: https://syndi - See section below for detailed info 3. Run the script - `cd ./ClassUpdate` - - `python ./replace.py` + - `python ./replace.py [config_profile|theme_dir]` +> use `-help` to see flags ### Example Files marked with `↻` will be updated by the script if using the default config. diff --git a/lib/input.py b/lib/input.py index 2e64b94..c3f3638 100644 --- a/lib/input.py +++ b/lib/input.py @@ -5,10 +5,28 @@ import configparser import glob import os.path +import sys from lib.format import ind, q +flags, args = {}, [] +for s in sys.argv[1:]: + if s.startswith('-'): # format to {key:value} from -key=value + flag = s.split('=') + flags[flag[0][1:]] = flag[1] if len(flag) > 1 else None + else: + args.append(s) + +if 'h' in flags or 'help' in flags: + print("""usage: [flags] +flags: + -y: skip confirmation dialog + -x=, -ext=: file extension to filter for + -diff=: diff source to use""") + sys.exit() + __all__ = ["get_params"] +__root__ = os.path.dirname(os.path.dirname(__file__)) def try_user(config, profile): @@ -24,16 +42,43 @@ def try_user(config, profile): def get_config(config_filename): """Get parameters from config file.""" raw_config = configparser.ConfigParser() + + config_filename = os.path.join(__root__, config_filename) if not os.path.exists(config_filename): raise FileNotFoundError("Config file not found.") raw_config.read(config_filename) profile = "DEFAULT" - if input(f"Use {q(profile)} profile? [Y/n]\t").lower() == "n": + is_path = False + if len(args) != 0: + is_path = os.path.isdir(args[0]) + + if not is_path: + profile = args[0] + elif input(f"Use {q(profile)} profile? [Y/n]\t").lower() == "n": profile = input("Config profile name:\t\t") profile = try_user(raw_config, profile) - config_user = raw_config[profile] + if profile in raw_config: + config_user = raw_config[profile] + else: + raise ValueError(f'unknown profile/directory: "{profile}"') + + if is_path: + config_user["ThemeDirectory"] = os.path.abspath(args[0]) + else: + # backwards compat. + config_user["ThemeDirectory"] = os.path.abspath(__root__+'/../'+config_user["ThemeDirectory"]) + + if "ext" in flags: + config_user["FileExtension"] = flags["ext"] + elif "x" in flags: + config_user["FileExtension"] = flags["x"] + + if "diff" in flags: + config_user["DiffLocation"] = flags["diff"] + config_user["UseLocalDiff"] = "no" if flags["diff"].startswith("http") else "yes" + config = { "dir": config_user["ThemeDirectory"], "ext": config_user["FileExtension"], @@ -41,6 +86,8 @@ def get_config(config_filename): "location": config_user["DiffLocation"], } + if config["uselocaldiff"] and not os.path.exists(config["location"]): + raise FileNotFoundError("Diff file not found.") return config @@ -53,10 +100,11 @@ def get_params(): print(ind(f"Diff file location:\t{config['location']}")) filenames = glob.glob(os.path.join( - "..", config["dir"], "**", "*." + config["ext"] + config["dir"], "**", "*." + config["ext"] ), recursive=True) print(f"\nFound {len(filenames)} {config['ext']} files in {config['dir']}.") - input("Press enter to continue or Ctrl+C to cancel.") + if 'y' not in flags: + input("Press enter to continue or Ctrl+C to cancel.") return config["uselocaldiff"], config['location'], filenames