Skip to content

map_test

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.map_test

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

map_to_fos

map_to_fos(filepath=None, map_name=None)
Source code in FoSpy/json/core.py
def map_to_fos(filepath=None, map_name=None):
    from .utils import map_data_to_properties, fill_properties
    from .. import cfg
    import json
    from tkinter import Tk, filedialog
    root = False

    maps = cfg.json_maps

    if filepath is None:
        root = Tk()
        root.withdraw()
        filepath = filedialog.askopenfilename(title="Select a .json input file to be mapped.", filetypes=[("JSON files", "*.json")])

    with open(filepath, "r") as f:
        input_data = json.load(f)

    if map_name is None:
        if maps.get("default") is None:
            map_name = input("\nNo default map found. Please enter an existing map name (Press Enter to load new map): ")

            if not map_name:
                if not root:
                    root = Tk()
                    root.withdraw()
                filepath = filedialog.askopenfilename(title="Select a new .json map file.", filetypes=[("JSON files", "*.json")])
                print(f"\nLoaded map file: {filepath}")

                get_missing = input("\nLoad missing values? (y/n): ").lower().strip() == "y"
                if get_missing:
                    missing_path = filedialog.askopenfilename(title="Select a .json missing values file.", filetypes=[("JSON files", "*.json")])
                    print(f"\nLoaded missing values file: {missing_path}")
                else:
                    missing_path = None

                map_name = input("\nEnter a name for the new map: ")

                new_map(filepath, map_name, missing_path)
        else:
            map_name = maps.default
            print(f"\nNo map specified. Loaded default map: {map_name}\n"
                  "- To use a different map, use the map_name argument.\n"
                  "- To create a new map or overwrite an existing one, "
                  "use the new_map() function. New maps can be set "
                  "immediately as default using "
                  "new_map(..., set_default=True)")

    map_dict = maps.get(map_name, {})
    if not map_dict:
        raise ValueError(f"Map {map_name} not found.")
    if root:
        root.destroy()
    property_dict = map_data_to_properties(input_data, **map_dict)
    return fill_properties({}, **property_dict)

new_map

new_map(
    filepath,
    map_name=None,
    missing_path=None,
    save=None,
    set_default=False,
)
Source code in FoSpy/json/core.py
def new_map(filepath, map_name=None, missing_path=None, save=None, set_default=False):
    from ..config import save_all as cfg_save, values as cfg
    import json
    import os
    maps = cfg.json_maps

    with open(os.path.abspath(filepath), 'r') as f:
        map_data = json.load(f)

    if missing_path is not None:
        with open(os.path.abspath(missing_path), 'r') as f:
            missing = json.load(f)
    else:
        missing = {}

    map_dict = {"map_dict": map_data, "missing_dict": missing}

    setattr(maps, map_name, map_dict)

    if maps.get("default") is None or set_default:
        maps.default = map_name

    if save is None:
        save = input("\nThe new map is saved to the current session only. "
                     "Saving now will also save any other changes within this session. "
                     "Save changes to config? (y/n): ").lower().strip() == "y"

    if save:
        cfg_save(prompt=False)

run

run(
    filepath=None,
    map_name=None,
    make_new=False,
    new_missing=False,
    new_default=False,
    open_result=False,
)
Source code in FoSpy/_dev/testing/map_test.py
def run(filepath=None, map_name=None, make_new=False, new_missing=False, new_default=False, open_result=False):
    if filepath is None:
        filepath = file_prompt(
            title="Select a .json input file to be mapped.",
            filetypes=[("JSON files", "*.json"), ("All files", "*.*")]
        )

    if make_new:
        map_path = file_prompt(
            title="Select a new .json map file.",
            filetypes=[("JSON files", "*.json"), ("All files", "*.*")])

        missing_path = None
        if new_missing:
            missing_path = file_prompt(
                title="Select a new .json missing values file.",
                filetypes=[("JSON files", "*.json"), ("All files", "*.*")])

        new_map(filepath=map_path, map_name=map_name, missing_path=missing_path, set_default=new_default)

    elif map_name not in (None, "default") and new_default:
        from ...config import values as cfg, save_all as cfg_save
        cfg.default = map_name
        cfg_save(prompt=False)

    mapped_dict = map_to_fos(filepath=filepath, map_name=map_name)

    filename = Path(filepath).stem

    with tempfile.TemporaryDirectory() as tmpdir:
        tmp_path = Path(tmpdir) / f"{filename}_maptest.json"
        with open(tmp_path, "w") as f:
            json.dump(mapped_dict, f, indent=4)

        load_test.run(str(tmp_path), open_result=open_result)

    return True