diff --git a/joj3_config_generator/load.py b/joj3_config_generator/load.py new file mode 100644 index 0000000..0139ff7 --- /dev/null +++ b/joj3_config_generator/load.py @@ -0,0 +1,39 @@ +from pathlib import Path +from typing import Dict, Tuple + +import inquirer +import rtoml +import yaml + +from joj3_config_generator.models import joj1, repo, task + + +def load_joj3_toml_answers() -> Dict[str, str]: + questions = [ + inquirer.List( + "size", + message="What size do you need?", + choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"], + ), + ] + answers = inquirer.prompt(questions) + return answers + + +def load_joj1_yaml(yaml_path: Path) -> joj1.Config: + joj1_obj = yaml.safe_load(yaml_path.read_text()) + return joj1.Config(**joj1_obj) + + +def load_joj3_toml( + root_path: Path, repo_toml_path: Path, task_toml_path: Path +) -> Tuple[repo.Config, task.Config]: + repo_obj = rtoml.loads(repo_toml_path.read_text()) + task_obj = rtoml.loads(task_toml_path.read_text()) + repo_conf = repo.Config(**repo_obj) + repo_conf.root = root_path + repo_conf.path = repo_toml_path.relative_to(root_path) + task_conf = task.Config(**task_obj) + task_conf.root = root_path + task_conf.path = task_toml_path.relative_to(root_path) + return repo_conf, task_conf diff --git a/joj3_config_generator/main.py b/joj3_config_generator/main.py index c8f0ccf..0e8cf95 100644 --- a/joj3_config_generator/main.py +++ b/joj3_config_generator/main.py @@ -1,15 +1,17 @@ import json from pathlib import Path -import inquirer import rtoml import typer -import yaml from typing_extensions import Annotated from joj3_config_generator.convert import convert as convert_conf from joj3_config_generator.convert import convert_joj1 as convert_joj1_conf -from joj3_config_generator.models import joj1, repo, task +from joj3_config_generator.load import ( + load_joj1_yaml, + load_joj3_toml, + load_joj3_toml_answers, +) from joj3_config_generator.models.const import JOJ3_CONFIG_ROOT from joj3_config_generator.utils.logger import logger @@ -17,33 +19,26 @@ app = typer.Typer(add_completion=False) @app.command() -def create(toml: typer.FileTextWrite) -> None: +def create(toml_path: Path) -> None: """ Create a new JOJ3 toml config file """ - logger.info("Creating") - questions = [ - inquirer.List( - "size", - message="What size do you need?", - choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"], - ), - ] - answers = inquirer.prompt(questions) - logger.info(answers) + logger.info(f"Creating toml file {toml_path}") + answers = load_joj3_toml_answers() + logger.debug(f"Got answers: {answers}") + toml_path.write_text(rtoml.dumps({})) @app.command() -def convert_joj1(yaml_file: typer.FileText, toml_file: typer.FileTextWrite) -> None: +def convert_joj1(yaml_path: Path, toml_path: Path) -> None: """ Convert a JOJ1 yaml config file to JOJ3 toml config file """ - logger.info(f"Converting yaml file {yaml_file}") - joj1_obj = yaml.safe_load(yaml_file.read()) - joj1_model = joj1.Config(**joj1_obj) + logger.info(f"Converting yaml file {yaml_path}") + joj1_model = load_joj1_yaml(yaml_path) task_model = convert_joj1_conf(joj1_model) result_dict = task_model.model_dump(by_alias=True, exclude_none=True) - toml_file.write(rtoml.dumps(result_dict)) + toml_path.write_text(rtoml.dumps(result_dict)) @app.command() @@ -60,9 +55,7 @@ def convert( """ logger.info(f"Converting files in {root.absolute()}") for repo_toml_path in root.glob("**/repo.toml"): - repo_path = repo_toml_path.parent - repo_obj = rtoml.loads(repo_toml_path.read_text()) - for task_toml_path in repo_path.glob("**/*.toml"): + for task_toml_path in repo_toml_path.parent.glob("**/*.toml"): if repo_toml_path == task_toml_path: continue toml_name = task_toml_path.name.removesuffix(".toml") @@ -70,13 +63,7 @@ def convert( logger.info( f"Converting {repo_toml_path} & {task_toml_path} to {result_json_path}" ) - task_obj = rtoml.loads(task_toml_path.read_text()) - repo_conf = repo.Config(**repo_obj) - repo_conf.root = root - repo_conf.path = repo_toml_path.relative_to(root) - task_conf = task.Config(**task_obj) - task_conf.root = root - task_conf.path = task_toml_path.relative_to(root) + repo_conf, task_conf = load_joj3_toml(root, repo_toml_path, task_toml_path) result_model = convert_conf(repo_conf, task_conf) result_dict = result_model.model_dump(by_alias=True, exclude_none=True) with result_json_path.open("w") as result_file: diff --git a/tests/convert/utils.py b/tests/convert/utils.py index e1a748b..79d6723 100644 --- a/tests/convert/utils.py +++ b/tests/convert/utils.py @@ -1,36 +1,18 @@ import json from pathlib import Path -from typing import Any, Dict, Tuple - -import rtoml from joj3_config_generator.convert import convert -from joj3_config_generator.models import repo, task - - -def read_convert_files( - case_name: str, -) -> Tuple[repo.Config, task.Config, Dict[str, Any]]: - root = Path(__file__).resolve().parent - repo_toml_path = root / case_name / "repo.toml" - repo_toml = repo_toml_path.read_text() if repo_toml_path.exists() else "" - task_toml_path = root / case_name / "task.toml" - task_toml = task_toml_path.read_text() if task_toml_path.exists() else "" - result = json.loads((root / case_name / "task.json").read_text()) - return ( - repo.Config( - root=root, path=repo_toml_path.relative_to(root), **rtoml.loads(repo_toml) - ), - task.Config( - root=root, path=task_toml_path.relative_to(root), **rtoml.loads(task_toml) - ), - result, - ) +from joj3_config_generator.load import load_joj3_toml def load_case(case_name: str) -> None: - repo, task, expected_result = read_convert_files(case_name) - result = convert(repo, task).model_dump( + root = Path(__file__).resolve().parent + repo_toml_path = root / case_name / "repo.toml" + task_toml_path = root / case_name / "task.toml" + repo_conf, task_conf = load_joj3_toml(root, repo_toml_path, task_toml_path) + result_json_path = root / case_name / "task.json" + expected_result = json.loads(result_json_path.read_text()) + result = convert(repo_conf, task_conf).model_dump( mode="json", by_alias=True, exclude_none=True ) assert result == expected_result