JOJ3-config-generator/joj3_config_generator/models/answer.py
张泊明518370910136 48c04d2830
All checks were successful
build / build (push) Successful in 2m1s
feat: support create from template files
2025-03-19 01:53:48 -04:00

148 lines
3.4 KiB
Python

from abc import ABC, abstractmethod
from enum import Enum
from importlib import resources
from typing import Any, ClassVar, Dict, List
import inquirer
from pydantic import BaseModel, ConfigDict
class LanguageInterface(ABC):
@classmethod
@abstractmethod
def __str__(cls) -> str: ...
@abstractmethod
class Stage(str, Enum): ...
@abstractmethod
class Attribute(BaseModel): ...
stages: ClassVar[List[Enum]]
attribute: ClassVar[BaseModel]
@classmethod
def set_stages(cls, stages: List[str]) -> None:
cls.stages = [cls.Stage(stage) for stage in stages]
@classmethod
def set_attribute(cls, attribute_dict: Dict[str, Any]) -> None:
cls.attribute = cls.Attribute(**attribute_dict)
@classmethod
@abstractmethod
def get_attribute_questions(cls) -> List[Any]: ...
@classmethod
def get_template_questions(cls) -> List[Any]:
templates_dir = resources.files(f"joj3_config_generator.templates").joinpath(
cls.__str__()
)
choices = []
for entry in templates_dir.iterdir():
if entry.is_file() and entry.name.endswith(".toml"):
choices.append(entry.name)
return [
inquirer.List(
"template_file",
message="Which template file do you want?",
choices=choices,
),
]
class Cpp(LanguageInterface):
@classmethod
def __str__(cls) -> str:
return "C++"
class Stage(str, Enum):
COMPILATION = "Compilation"
CPPCHECK = "Cppcheck"
CPPLINT = "Cpplint"
CLANG_TIDY = "Clang-Tidy"
RUN = "Run"
class Attribute(BaseModel):
compile_command: str = "make"
run_command: str = "./a.out"
stages = []
attribute = Attribute()
@classmethod
def get_attribute_questions(cls) -> List[Any]:
return [
inquirer.Text(
name="compile_command",
message="Compile command",
default=cls.attribute.compile_command,
),
inquirer.Text(
name="run_command",
message="Run command",
default=cls.attribute.run_command,
),
]
class Python(LanguageInterface):
@classmethod
def __str__(cls) -> str:
return "Python"
class Stage(str, Enum):
RUN = "Run"
class Attribute(BaseModel):
run_command: str = "python3 main.py"
stages = []
attribute = Attribute()
@classmethod
def get_attribute_questions(cls) -> List[Any]:
return [
inquirer.Text(
name="run_command",
message="Run command",
default=cls.attribute.run_command,
),
]
class Rust(LanguageInterface):
@classmethod
def __str__(cls) -> str:
return "Rust"
class Stage(str, Enum):
COMPILATION = "Compilation"
CLIPPY = "Clippy"
RUN = "Run"
class Attribute(BaseModel):
pass
stages = []
attribute = Attribute()
@classmethod
def get_attribute_questions(cls) -> List[Any]:
return []
LANGUAGES = [
Cpp(),
Python(),
Rust(),
]
class Answers(BaseModel):
name: str
language: LanguageInterface
template_file_content: str = ""
model_config = ConfigDict(arbitrary_types_allowed=True)