1
Fork 0
mirror of https://github.com/RGBCube/CSAssignments synced 2025-06-14 18:12:08 +00:00

Start working on new interactive runner

This commit is contained in:
RGBCube 2022-11-04 16:44:37 +03:00
parent 1080941194
commit 38ba3af643
23 changed files with 202 additions and 15 deletions

135
run.py Normal file
View file

@ -0,0 +1,135 @@
#!/usr/bin/python3
from __future__ import annotations
import tomllib
from dataclasses import dataclass
from functools import cached_property
from os import name as os_name, system as cmd
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from chalky import Chalk
ROOT = Path(__file__).parent
IS_WIN = os_name == "nt"
QUIET_SUFFIX = " > NUL" if IS_WIN else " > /dev/null"
OS_KEY = "windows" if IS_WIN else "linux"
class Sources:
def __init__(self) -> None:
self.__src_dir = ROOT / "src"
@cached_property
def languages(self) -> dict[str, Language]:
languages = {}
for language_dir in self.__src_dir.iterdir():
if not language_dir.is_dir():
continue
language_metadata = tomllib.loads((language_dir / "language.toml").read_text())
language_metadata["directory"] = language_dir
language = Language.from_raw(language_metadata)
languages[language.name] = language
return languages
@dataclass
class Colors:
fg: Chalk
bg: Chalk
class Language:
def __init__(
self,
*,
directory: Path,
name: str,
description: str,
colors: Colors,
build_command: str | None,
run_command: str,
) -> None:
self.__directory = directory
self.name = name
self.description = description
self.colors = colors
self.build_command = build_command
self.run_command = run_command
@classmethod
def from_raw(cls, raw: dict) -> Language:
colors = raw["colors"]
build = raw.get("build", {})
run = raw["run"]
return cls(
directory=raw["directory"],
name=raw["name"],
description=raw["description"],
colors=Colors(
fg=colors["foreground"],
bg=colors["background"],
),
build_command=build.get("common", build.get(OS_KEY)),
run_command=run.get("common", run.get(OS_KEY)),
)
@property
def assignments(self) -> dict[str, Assignment]:
for assignment_dir in self.__directory.iterdir():
if not assignment_dir.is_dir():
continue
assignment_metadata = tomllib.loads((assignment_dir / "assignment.toml").read_text())
assignment_metadata["directory"] = assignment_dir
assignment_metadata["language"] = self
yield Assignment.from_raw(assignment_metadata)
class Assignment:
def __init__(
self,
*,
language: Language,
directory: Path,
name: str,
date: str,
description: str,
directions: str,
) -> None:
self.language = language
self.__directory = directory
self.name = name
self.date = date
self.description = description
self.directions = directions
@classmethod
def from_raw(cls, raw: dict) -> Assignment:
return cls(
language=raw["language"],
directory=raw["directory"],
name=raw["name"],
date=raw["date"],
description=raw["description"],
directions=raw["directions"],
)
def compile(self, *, quiet: bool = False) -> bool | None:
"""None = not a compiled language"""
if self.language.build_command is None:
return None
return cmd(self.language.build_command + (QUIET_SUFFIX if quiet else "")) == 0

View file

@ -0,0 +1,9 @@
name = "example"
given-date = "3/11/2022"
description = "This is an example entry for C"
directions = """
Print "Example" 10 times.
"""
[compile]
main-file = "example.c"

View file

@ -0,0 +1,8 @@
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
printf("Example");
}
return 0;
}

16
sources/c/language.toml Normal file
View file

@ -0,0 +1,16 @@
name = "C"
description = "C (pronounced like the letter c) is a general-purpose computer programming language"
[colors]
foreground = 0x000000
background = 0x00599d
[build]
common = "gcc -o {out-file} {main-file}"
[run]
linux = "./{out-file}"
windows = "{out-file}"
[dependencies]
common = ["gcc"]

View file

@ -1,3 +1,7 @@
name = "star-rating"
given-date = "30/10/2022"
description = "A program that takes 21 reviews and calculates the average rating and prints some stars"
directions = """
1. Read in 21 user ratings for a movie. 1. Read in 21 user ratings for a movie.
Each rating is a number from 1 to 5 stars. Each rating is a number from 1 to 5 stars.
Store the ratings in an array. Store the ratings in an array.
@ -21,3 +25,7 @@ contains the percentage of ratings for each
star, e.g. 14.3% one star, 23.8% two star, etc. star, e.g. 14.3% one star, 23.8% two star, etc.
Display this to the screen in a similar manner Display this to the screen in a similar manner
to task 4. to task 4.
"""
[compile]
main-file = "star_rating.c"

View file

@ -0,0 +1,9 @@
name = "example"
given-date = "3/11/2022"
description = "This is an example entry for CPython"
directions = """
Print "Example" 10 times.
"""
[compile]
main-file = "example.py"

View file

@ -0,0 +1,2 @@
for _ in range(10):
print("Example")

View file

@ -0,0 +1,14 @@
name = "CPython"
description = "Python is a high-level, general-purpose programming language"
[colors]
foreground = 0xfed140
background = 0x3670a0
[run]
linux = "python3 {main-file}"
windows = "py {main-file}"
[dependencies]
linux = ["python3"]
windows = ["py"]

View file

@ -1 +0,0 @@
00599d

View file

@ -1 +0,0 @@
gcc -o {out} {main}

View file

@ -1 +0,0 @@
C (pronounced like the letter c) is a general-purpose computer programming language

View file

@ -1 +0,0 @@
ffffff

View file

@ -1 +0,0 @@
Does stuff with reviews TODO do desc

View file

@ -1 +0,0 @@
star_rating.c

View file

@ -1 +0,0 @@
3670a0

View file

@ -1 +0,0 @@
Python is a high-level, general-purpose programming language

View file

@ -1 +0,0 @@
fed140

View file

@ -1 +0,0 @@
python3 {main}

View file

@ -1 +0,0 @@
test directions

View file

@ -1 +0,0 @@
test desc

View file

@ -1 +0,0 @@
test.py

View file

@ -1 +0,0 @@
print("test print")