autoeagle.ulp_generator

 1import argparse
 2import sys
 3from pathlib import Path
 4
 5from autoeagle import autoeagle_config
 6
 7root = Path(__file__).parent
 8
 9
10def get_args() -> argparse.Namespace:
11    parser = argparse.ArgumentParser()
12
13    parser.add_argument(
14        "pyscript_path",
15        type=str,
16        help=""" The path to the Python script to generate a Ulp file for. """,
17    )
18
19    args = parser.parse_args()
20
21    return args
22
23
24def load_template(script_template: bool = False) -> str:
25    """Return a ulp template.
26
27    :param script_template: If True,
28    return ulp_script_template.
29    If False, return ulp_template."""
30    if script_template:
31        return (root / "ulp_script_template.txt").read_text()
32    else:
33        return (root / "ulp_template.txt").read_text()
34
35
36def create_ulp(pyscript_path: str):
37    pyscript = Path(pyscript_path).absolute()
38    pyname = pyscript.stem
39    ulpdir = Path(autoeagle_config.load_config()["ulpdir"])
40    savepath = (ulpdir / pyname).with_suffix(".ulp")
41    template = load_template(
42        script_template=True if "ScriptWriter" in pyscript.read_text() else False
43    )
44    for replacer in [
45        ("$executable", sys.executable.replace("\\", "/")),
46        ("$script_path", str(pyscript).replace("\\", "/")),
47        (
48            "$script_file",
49            pyname,
50        ),  # There is no "$script_file" in the ulp_template so this just does nothing for non ScriptWriter scripts
51    ]:
52        template = template.replace(replacer[0], replacer[1])
53    savepath.write_text(template)
54
55
56def main(args: argparse.Namespace = None):
57    if not args:
58        args = get_args()
59    if not autoeagle_config.is_configured():
60        autoeagle_config.prompt_to_configure()
61    create_ulp(args.pyscript_path)
62
63
64if __name__ == "__main__":
65    main(get_args())
def get_args() -> argparse.Namespace:
11def get_args() -> argparse.Namespace:
12    parser = argparse.ArgumentParser()
13
14    parser.add_argument(
15        "pyscript_path",
16        type=str,
17        help=""" The path to the Python script to generate a Ulp file for. """,
18    )
19
20    args = parser.parse_args()
21
22    return args
def load_template(script_template: bool = False) -> str:
25def load_template(script_template: bool = False) -> str:
26    """Return a ulp template.
27
28    :param script_template: If True,
29    return ulp_script_template.
30    If False, return ulp_template."""
31    if script_template:
32        return (root / "ulp_script_template.txt").read_text()
33    else:
34        return (root / "ulp_template.txt").read_text()

Return a ulp template.

Parameters
  • script_template: If True, return ulp_script_template. If False, return ulp_template.
def create_ulp(pyscript_path: str):
37def create_ulp(pyscript_path: str):
38    pyscript = Path(pyscript_path).absolute()
39    pyname = pyscript.stem
40    ulpdir = Path(autoeagle_config.load_config()["ulpdir"])
41    savepath = (ulpdir / pyname).with_suffix(".ulp")
42    template = load_template(
43        script_template=True if "ScriptWriter" in pyscript.read_text() else False
44    )
45    for replacer in [
46        ("$executable", sys.executable.replace("\\", "/")),
47        ("$script_path", str(pyscript).replace("\\", "/")),
48        (
49            "$script_file",
50            pyname,
51        ),  # There is no "$script_file" in the ulp_template so this just does nothing for non ScriptWriter scripts
52    ]:
53        template = template.replace(replacer[0], replacer[1])
54    savepath.write_text(template)
def main(args: argparse.Namespace = None):
57def main(args: argparse.Namespace = None):
58    if not args:
59        args = get_args()
60    if not autoeagle_config.is_configured():
61        autoeagle_config.prompt_to_configure()
62    create_ulp(args.pyscript_path)