feat: convert joj1 cli

This commit is contained in:
张泊明518370910136 2024-10-22 06:29:33 -04:00
parent 806bb47a61
commit 3b0b36c539
GPG Key ID: D47306D7062CDA9D
2 changed files with 15 additions and 8 deletions

View File

@ -5,9 +5,11 @@ from pathlib import Path
import inquirer
import rtoml
import typer
import yaml
from joj3_config_generator.convert import convert as convert_conf
from joj3_config_generator.models import RepoConfig, TaskConfig
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.utils.logger import logger
app = typer.Typer(add_completion=False)
@ -31,11 +33,16 @@ def create(toml: typer.FileTextWrite) -> None:
@app.command()
def convert_joj1(yaml: typer.FileText, toml: typer.FileTextWrite) -> None:
def convert_joj1(yaml_file: typer.FileText, toml_file: typer.FileTextWrite) -> None:
"""
Convert a JOJ1 yaml config file to JOJ3 toml config file
"""
logger.info("Converting")
logger.info(f"Converting yaml file {yaml_file}")
joj1_obj = yaml.safe_load(yaml_file.read())
joj1_model = joj1.Config(**joj1_obj)
task_model = convert_joj1_conf(joj1_model)
result_dict = task_model.model_dump(by_alias=True)
toml_file.write(rtoml.dumps(result_dict))
@app.command()
@ -54,7 +61,7 @@ def convert(root: Path = Path(".")) -> None:
task_toml = task_file.read()
repo_obj = rtoml.loads(repo_toml)
task_obj = rtoml.loads(task_toml)
result_model = convert_conf(RepoConfig(**repo_obj), TaskConfig(**task_obj))
result_model = convert_conf(repo.Config(**repo_obj), task.Config(**task_obj))
result_dict = result_model.model_dump(by_alias=True)
with open(result_json_path, "w") as result_file:
json.dump(result_dict, result_file, ensure_ascii=False, indent=4)

View File

@ -11,10 +11,10 @@ def read_convert_joj1_files(case_name: str) -> Tuple[joj1.Config, Dict[str, Any]
root = os.path.dirname(os.path.realpath(__file__))
task_yaml_path = os.path.join(root, case_name, "task.yaml")
task_toml_path = os.path.join(root, case_name, "task.toml")
with open(task_yaml_path) as repo_file:
task_yaml = repo_file.read()
with open(task_toml_path) as task_file:
task_toml = task_file.read()
with open(task_yaml_path) as f:
task_yaml = f.read()
with open(task_toml_path) as f:
task_toml = f.read()
joj1_obj = yaml.safe_load(task_yaml)
task_obj = rtoml.loads(task_toml)
return joj1.Config(**joj1_obj), task_obj