mirror of
https://github.com/RGBCube/CSAssignments
synced 2025-06-21 13:32:10 +00:00
Refactor most of the interactive runner
This commit is contained in:
parent
d782a4d879
commit
58f5c52db5
9 changed files with 138 additions and 122 deletions
|
@ -3,35 +3,33 @@ from __future__ import annotations
|
|||
__all__ = ("Assignment",)
|
||||
|
||||
from functools import cached_property
|
||||
from os import system as cmd
|
||||
from .helpers import command
|
||||
from tomllib import loads as decode_toml
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
from .language import Language
|
||||
|
||||
AssignmentConfig = TypedDict(
|
||||
"AssignmentConfig",
|
||||
{
|
||||
"name": str,
|
||||
"given-date": str,
|
||||
"main-file": str,
|
||||
"description": str,
|
||||
"directions": str,
|
||||
}
|
||||
)
|
||||
|
||||
class AssignmentConfig(TypedDict):
|
||||
name: str
|
||||
date: str
|
||||
main: str
|
||||
description: str
|
||||
directions: str
|
||||
|
||||
|
||||
class Assignment:
|
||||
__config: AssignmentConfig
|
||||
__main: str
|
||||
language: Language
|
||||
name: str
|
||||
given_date: str
|
||||
__main_file: str
|
||||
date: str
|
||||
description: str
|
||||
directions: str
|
||||
|
||||
def __init__(self, directory: Path, language: Language) -> None:
|
||||
def __init__(self, directory: Path, language: Language, /) -> None:
|
||||
self.__directory = directory
|
||||
self.language = language
|
||||
|
||||
|
@ -40,19 +38,23 @@ class Assignment:
|
|||
return decode_toml((self.__directory / "assignment.toml").read_text())
|
||||
|
||||
def refresh(self) -> None:
|
||||
del self.__config
|
||||
"""TODO"""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self.__config["name"]
|
||||
|
||||
@property
|
||||
def given_date(self) -> str:
|
||||
return self.__config["given-date"]
|
||||
def date(self) -> str:
|
||||
return self.__config["date"]
|
||||
|
||||
@property
|
||||
def __main_file(self) -> Path:
|
||||
return self.__directory / self.__config["main-file"]
|
||||
def __main(self) -> Path:
|
||||
return (self.__directory / self.__config["main"]).absolute()
|
||||
|
||||
@property
|
||||
def __out(self) -> Path:
|
||||
return (self.__directory / "compiled.out").absolute()
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
|
@ -63,29 +65,29 @@ class Assignment:
|
|||
return self.__config["directions"]
|
||||
|
||||
def compile(self, *, quiet: bool) -> int | None:
|
||||
if missing := self.language.check_dependencies_installed():
|
||||
raise ValueError(f"Needed depencencies are not installed: {', '.join(missing)}")
|
||||
if missing := self.language.check_if_dependencies_are_installed():
|
||||
raise ValueError(
|
||||
f"Some dependencies that are needed are not installed: {', '.join(missing)}"
|
||||
)
|
||||
|
||||
if not self.language.is_compiled:
|
||||
return None
|
||||
|
||||
return cmd(
|
||||
return command(
|
||||
self.language._build_command.format(
|
||||
**{
|
||||
"out-file": self.__directory / "compiled.out",
|
||||
"main-file": self.__main_file.absolute(),
|
||||
}
|
||||
) +
|
||||
(QUIET_SUFFIX if quiet else "")
|
||||
)
|
||||
out=self.__out,
|
||||
main=self.__main
|
||||
),
|
||||
quiet=quiet
|
||||
)
|
||||
|
||||
def run(self) -> int:
|
||||
if self.language.is_compiled and not (self.__directory / "compiled.out").exists():
|
||||
self.compile(quiet=True)
|
||||
|
||||
return cmd(
|
||||
return command(
|
||||
self.language._run_command.format(
|
||||
(
|
||||
self.__directory / "compiled.out").absolute() if self.language.is_compiled else self.__main_file.absolute()
|
||||
)
|
||||
)
|
||||
self.__out if self.language.is_compiled else self.__main
|
||||
),
|
||||
quiet=False
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue