feat: sample create command
All checks were successful
build / build (push) Successful in 1m53s

This commit is contained in:
张泊明518370910136 2025-03-13 20:57:05 -04:00
parent a4a45fbc07
commit b43a25285e
GPG Key ID: D47306D7062CDA9D
9 changed files with 98 additions and 18 deletions

View File

@ -0,0 +1,5 @@
from joj3_config_generator.models import answer, task
def create(answers: answer.Answers) -> task.Config:
return task.Config(task=task.Task(name=answers.name, type_=answers.type_))

View File

@ -1,23 +1,26 @@
from pathlib import Path from pathlib import Path
from typing import Dict, Tuple from typing import Tuple
import inquirer import inquirer
import rtoml import rtoml
import yaml import yaml
from joj3_config_generator.models import joj1, repo, task from joj3_config_generator.models import answer, joj1, repo, task
def load_joj3_toml_answers() -> Dict[str, str]: def load_joj3_task_toml_answers() -> answer.Answers:
questions = [ questions = [
inquirer.List( inquirer.Text(name="name", message="What's the task name?"),
"size", inquirer.Text(name="type", message="What's the task type?"),
message="What size do you need?", inquirer.Checkbox(
choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"], "stages",
message="What kind of stages do you need?",
choices=[member.value for member in answer.StageEnum],
default=[answer.StageEnum.COMPILATION],
), ),
] ]
answers = inquirer.prompt(questions) answers = inquirer.prompt(questions)
return answers return answer.Answers(**answers)
def load_joj1_yaml(yaml_path: Path) -> joj1.Config: def load_joj1_yaml(yaml_path: Path) -> joj1.Config:

View File

@ -7,10 +7,11 @@ 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.create import create as create_joj3_task_conf
from joj3_config_generator.load import ( from joj3_config_generator.load import (
load_joj1_yaml, load_joj1_yaml,
load_joj3_task_toml_answers,
load_joj3_toml, 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
@ -21,18 +22,23 @@ app = typer.Typer(add_completion=False)
@app.command() @app.command()
def create(toml_path: Path) -> None: def create(toml_path: Path) -> None:
""" """
Create a new JOJ3 toml config file Create a new JOJ3 task toml config file
""" """
logger.info(f"Creating toml file {toml_path}") logger.info(f"Creating task toml file {toml_path}")
answers = load_joj3_toml_answers() answers = load_joj3_task_toml_answers()
logger.debug(f"Got answers: {answers}") answers_dict = answers.model_dump(mode="json", by_alias=True)
toml_path.write_text(rtoml.dumps({})) logger.debug(f"Got answers: {answers_dict}")
task_model = create_joj3_task_conf(answers)
result_dict = task_model.model_dump(
mode="json", by_alias=True, exclude_none=True, exclude_unset=True
)
toml_path.write_text(rtoml.dumps(result_dict))
@app.command() @app.command()
def convert_joj1(yaml_path: Path, toml_path: Path) -> 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 task toml config file
""" """
logger.info(f"Converting yaml file {yaml_path}") logger.info(f"Converting yaml file {yaml_path}")
joj1_model = load_joj1_yaml(yaml_path) joj1_model = load_joj1_yaml(yaml_path)
@ -48,7 +54,7 @@ def convert(
typer.Argument( typer.Argument(
help=f"root directory of config files, located at {JOJ3_CONFIG_ROOT} in JTC" help=f"root directory of config files, located at {JOJ3_CONFIG_ROOT} in JTC"
), ),
] = Path(".") ] = Path("."),
) -> None: ) -> None:
""" """
Convert given dir of JOJ3 toml config files to JOJ3 json config files Convert given dir of JOJ3 toml config files to JOJ3 json config files

View File

@ -0,0 +1,20 @@
from enum import Enum
from typing import List
from pydantic import AliasChoices, BaseModel, Field
class StageEnum(str, Enum):
COMPILATION = "Compilation"
CPPCHECK = "Cppcheck"
CPPLINT = "Cpplint"
CLANG_TIDY = "Clang-Tidy"
class Answers(BaseModel):
name: str
type_: str = Field(
serialization_alias="type",
validation_alias=AliasChoices("type_", "type"),
)
stages: List[str]

View File

@ -3,7 +3,14 @@ from enum import Enum
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Type from typing import Any, Dict, List, Type
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator from pydantic import (
AliasChoices,
BaseModel,
ConfigDict,
Field,
field_validator,
model_validator,
)
from joj3_config_generator.models.common import Memory, Time from joj3_config_generator.models.common import Memory, Time
from joj3_config_generator.models.const import ( from joj3_config_generator.models.const import (
@ -141,7 +148,11 @@ class Release(BaseModel):
class Task(BaseModel): class Task(BaseModel):
type_: str = Field("unknown", serialization_alias="type", validation_alias="type") type_: str = Field(
"unknown",
serialization_alias="type",
validation_alias=AliasChoices("type_", "type"),
)
name: str = "unknown" name: str = "unknown"

View File

@ -0,0 +1,5 @@
{
"name": "hw7 ex2",
"type": "homework/h7/e2",
"stages": ["Compilation"]
}

View File

@ -0,0 +1,3 @@
[task]
name = "hw7 ex2"
type = "homework/h7/e2"

View File

@ -0,0 +1,5 @@
from tests.create.utils import load_case
def test_basic() -> None:
load_case("basic")

22
tests/create/utils.py Normal file
View File

@ -0,0 +1,22 @@
import json
from pathlib import Path
import rtoml
from joj3_config_generator.create import create
from joj3_config_generator.models import answer
def load_case(case_name: str) -> None:
root = Path(__file__).resolve().parent
answers_json_path = root / case_name / "answers.json"
task_toml_path = root / case_name / "task.toml"
answers = answer.Answers(**json.loads(answers_json_path.read_text()))
print(answers)
expected_result = rtoml.loads(task_toml_path.read_text())
result = create(answers).model_dump(
mode="json", by_alias=True, exclude_none=True, exclude_unset=True
)
print(result)
print(expected_result)
assert result == expected_result