JOJ3-config-generator/joj3_config_generator/loader.py
张泊明518370910136 d5780c5837
All checks were successful
build / build (push) Successful in 1m48s
chore: use pure python tomli & tomlkit
2025-03-18 04:03:36 -04:00

44 lines
1.4 KiB
Python

from pathlib import Path
from typing import Tuple
import inquirer
import tomli
import yaml
from joj3_config_generator.models import answer, joj1, repo, task
def load_joj3_task_toml_answers() -> answer.Answers:
name = inquirer.text("What's the task name?", default="hw0")
language: answer.LanguageInterface = inquirer.list_input(
"What's the language?", choices=answer.LANGUAGES
)
stages = inquirer.checkbox(
"What's the stages?",
choices=[member.value for member in language.Stage],
default=[member.value for member in language.Stage],
)
language.set_stages(stages)
attribute = inquirer.prompt(language.get_attribute_questions())
language.set_attribute(attribute)
return answer.Answers(name=name, language=language)
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 = tomli.loads(repo_toml_path.read_text())
task_obj = tomli.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