Skip to content

_utils

Full documentation pages are generated for docstring reference only and may contain symbols imported from other modules. Imported symbols are not distinguished from locally defined symbols and will appear in any module that they are imported into. For better information on where symbols should be imported from, review the sourcecode on the github.

FoSpy._dev.testing._utils

LOGO = "\n        %@@@@@@@@.             .@@@@@@@@%                \n     @@@@  @@@@@@@@@@@@@@@@@@@@@@@@@@@  @@@@             \n     @@ @@@@%#######*@@@@@@@*#######%@@@@ @@             \n      @@@%###########@=   :@############@@@              \n       @%###########@@     @@############@               \n       @@@########@@@       @@@########@@@               \n         @@@@@@@@@@*         *@@@@@@@@@@                 \n                                                         \n               @                                         \n               @                                         \n     @         @           #                             \n     @         @           @                             \n     @         @           @                             \n     @         @           @         %-                  \n     @         @=     =   =@         @@                  \n    @@         %@     @   @@    %@   @@    @             \n ::    :::::::    ::.   :    ::    :    ::   :::         \n                                                         \n @@@@@@@@@          @@@@@@@@@+                           \n @@        @@@@@@@# @@         @@@@@@@@  @@   @@:        \n @@@@@@@#  @*    @+ @@@@@@@@@  @@    @@  @@   @@         \n @@        @*    @* -       @- @@    @@  @@   -@         \n @@        @@@@@@@  @@@@@@@@@  @@@@@@@@  @@@@@@@         \n                               @@             *@         \n                               @@        @@@@@@          \n"

PREAMBLE module-attribute

PREAMBLE = "\n----------------------------------------------\nFoSpy Development Testing Suite\n----------------------------------------------\n"

REPO_PATH module-attribute

REPO_PATH = os.path.abspath(cfg.DEV.repo)

cfg module-attribute

cfg = None

choice_prompt

choice_prompt(message)

Replicates choice /m behavior: Y/N single keypress.

Source code in FoSpy/_dev/testing/_utils.py
def choice_prompt(message):
    """Replicates `choice /m` behavior: Y/N single keypress."""
    print(f"{message} [Y,N]? ", end="", flush=True)
    while True:
        ch = msvcrt.getch().decode("utf-8").lower()
        if ch in ("y", "n"):
            print(ch.upper())
            return ch == "y"

file_prompt

file_prompt(
    title="Select an input file",
    filetypes=[
        ("FOS files", "*.fos"),
        ("JSON files", "*.json"),
        ("All files", "*.*"),
    ],
)
Source code in FoSpy/_dev/testing/_utils.py
def file_prompt(title="Select an input file", filetypes=[("FOS files", "*.fos"), ("JSON files", "*.json"), ("All files", "*.*")]):
    root = Tk()
    root.withdraw()  # hide root window
    filepath = filedialog.askopenfilename(
        title=title,
        filetypes=filetypes
    )
    if not filepath:
        raise Exception("No file selected.")
    return filepath

get_current_branch

get_current_branch()
Source code in FoSpy/_dev/testing/_utils.py
def get_current_branch():
    return subprocess.check_output(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"],
        cwd=REPO_PATH,
        text=True
    ).strip()

open_file

open_file(path)
Source code in FoSpy/_dev/testing/_utils.py
def open_file(path):
    system = platform.system()
    if system == "Windows":
        os.startfile(path)
    elif system == "Darwin":
        subprocess.call(["open", path])
    else:
        subprocess.call(["xdg-open", path])
print_logo()
Source code in FoSpy/_dev/testing/_utils.py
def print_logo():
    print(LOGO)
    print(PREAMBLE)

run_batch

run_batch(path=None)
Source code in FoSpy/_dev/testing/_utils.py
def run_batch(path=None):
    if path is None or not os.path.exists(path):
        print("No batch file provided.")
        return

    if not platform.system() == "Windows":
        print("Only Windows is supported for batch file execution.")
        return
    # Ensure Windows uses cmd.exe to run the batch file
    subprocess.run([os.environ["COMSPEC"], "/c", path], shell=True)

run_git

run_git(*args)

Run a git command and stream output live.

Source code in FoSpy/_dev/testing/_utils.py
def run_git(*args):
    """Run a git command and stream output live."""
    cmd = ["git"] + list(args)
    proc = subprocess.Popen(
        cmd,
        cwd=REPO_PATH,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1
    )
    for line in proc.stdout:
        print(line, end="")
    proc.wait()
    return proc.returncode

toggle_branches

toggle_branches()
Source code in FoSpy/_dev/testing/_utils.py
def toggle_branches():
    from ...config import values as cfg, save_all as cfg_save

    # Detect current branch
    current = get_current_branch()
    print(f"Current branch: {current}\n")

    # Determine target branch
    if current.lower() == "main":
        target = "dev"
    elif current.lower() == "dev":
        target = "main"
    else:
        print(f'You are on branch "{current}", which is not main or dev.')
        print("Aborting to avoid accidental switching.")
        input("Press any key to continue...")
        return True

    print(f"Switching from {current} to {target}\n")

    # Confirm
    if not choice_prompt("Proceed with branch toggle"):
        print("Aborted by user.")
        return True

    print("\nRestoring working directory...")
    run_git("restore", ".")

    print("Switching branches...")
    if run_git("switch", target) != 0:
        print("Failed to switch branches.")
        input("Press any key to continue...")
        return True

    print("Pulling latest changes...")
    run_git("pull")

    cfg.DEV.branch = target
    cfg_save(prompt=False)

    print("\nBranch toggle complete.")
    input("Press any key to continue...")

    return True