refactor: move load logic to separate file
All checks were successful
build / build (push) Successful in 2m29s
build / build (pull_request) Successful in 2m27s

This commit is contained in:
张泊明518370910136 2025-03-04 15:43:56 -05:00
parent 3b40bee13c
commit f3ed1db5b2
GPG Key ID: D47306D7062CDA9D
3 changed files with 63 additions and 55 deletions

View File

@ -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

View File

@ -1,15 +1,17 @@
import json import json
from pathlib import Path from pathlib import Path
import inquirer
import rtoml import rtoml
import typer import typer
import yaml
from typing_extensions import Annotated from typing_extensions import Annotated
from joj3_config_generator.convert import convert as convert_conf 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.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.models.const import JOJ3_CONFIG_ROOT
from joj3_config_generator.utils.logger import logger from joj3_config_generator.utils.logger import logger
@ -17,33 +19,26 @@ app = typer.Typer(add_completion=False)
@app.command() @app.command()
def create(toml: typer.FileTextWrite) -> None: def create(toml_path: Path) -> None:
""" """
Create a new JOJ3 toml config file Create a new JOJ3 toml config file
""" """
logger.info("Creating") logger.info(f"Creating toml file {toml_path}")
questions = [ answers = load_joj3_toml_answers()
inquirer.List( logger.debug(f"Got answers: {answers}")
"size", toml_path.write_text(rtoml.dumps({}))
message="What size do you need?",
choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],
),
]
answers = inquirer.prompt(questions)
logger.info(answers)
@app.command() @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 Convert a JOJ1 yaml config file to JOJ3 toml config file
""" """
logger.info(f"Converting yaml file {yaml_file}") logger.info(f"Converting yaml file {yaml_path}")
joj1_obj = yaml.safe_load(yaml_file.read()) joj1_model = load_joj1_yaml(yaml_path)
joj1_model = joj1.Config(**joj1_obj)
task_model = convert_joj1_conf(joj1_model) task_model = convert_joj1_conf(joj1_model)
result_dict = task_model.model_dump(by_alias=True, exclude_none=True) 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() @app.command()
@ -60,9 +55,7 @@ def convert(
""" """
logger.info(f"Converting files in {root.absolute()}") logger.info(f"Converting files in {root.absolute()}")
for repo_toml_path in root.glob("**/repo.toml"): for repo_toml_path in root.glob("**/repo.toml"):
repo_path = repo_toml_path.parent for task_toml_path in repo_toml_path.parent.glob("**/*.toml"):
repo_obj = rtoml.loads(repo_toml_path.read_text())
for task_toml_path in repo_path.glob("**/*.toml"):
if repo_toml_path == task_toml_path: if repo_toml_path == task_toml_path:
continue continue
toml_name = task_toml_path.name.removesuffix(".toml") toml_name = task_toml_path.name.removesuffix(".toml")
@ -70,13 +63,7 @@ def convert(
logger.info( logger.info(
f"Converting {repo_toml_path} & {task_toml_path} to {result_json_path}" f"Converting {repo_toml_path} & {task_toml_path} to {result_json_path}"
) )
task_obj = rtoml.loads(task_toml_path.read_text()) repo_conf, task_conf = load_joj3_toml(root, repo_toml_path, task_toml_path)
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)
result_model = convert_conf(repo_conf, task_conf) result_model = convert_conf(repo_conf, task_conf)
result_dict = result_model.model_dump(by_alias=True, exclude_none=True) result_dict = result_model.model_dump(by_alias=True, exclude_none=True)
with result_json_path.open("w") as result_file: with result_json_path.open("w") as result_file:

View File

@ -1,36 +1,18 @@
import json import json
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Tuple
import rtoml
from joj3_config_generator.convert import convert from joj3_config_generator.convert import convert
from joj3_config_generator.models import repo, task from joj3_config_generator.load import load_joj3_toml
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,
)
def load_case(case_name: str) -> None: def load_case(case_name: str) -> None:
repo, task, expected_result = read_convert_files(case_name) root = Path(__file__).resolve().parent
result = convert(repo, task).model_dump( 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 mode="json", by_alias=True, exclude_none=True
) )
assert result == expected_result assert result == expected_result