diff --git a/run.py b/run.py new file mode 100644 index 0000000..f2ce820 --- /dev/null +++ b/run.py @@ -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 diff --git a/sources/c/example/assignment.toml b/sources/c/example/assignment.toml new file mode 100644 index 0000000..39acc28 --- /dev/null +++ b/sources/c/example/assignment.toml @@ -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" diff --git a/sources/c/example/example.c b/sources/c/example/example.c new file mode 100644 index 0000000..238dfb0 --- /dev/null +++ b/sources/c/example/example.c @@ -0,0 +1,8 @@ +#include + +int main() { + for (int i = 0; i < 10; i++) { + printf("Example"); + } + return 0; +} diff --git a/sources/c/language.toml b/sources/c/language.toml new file mode 100644 index 0000000..399cf37 --- /dev/null +++ b/sources/c/language.toml @@ -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"] diff --git a/src/C/star-rating:30.11.2022/ASSIGNMENT b/sources/c/star-rating/assignment.toml similarity index 76% rename from src/C/star-rating:30.11.2022/ASSIGNMENT rename to sources/c/star-rating/assignment.toml index d159ff0..87dbd88 100644 --- a/src/C/star-rating:30.11.2022/ASSIGNMENT +++ b/sources/c/star-rating/assignment.toml @@ -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. Each rating is a number from 1 to 5 stars. Store the ratings in an array. @@ -20,4 +24,8 @@ number. See screenshot. contains the percentage of ratings for each star, e.g. 14.3% one star, 23.8% two star, etc. Display this to the screen in a similar manner -to task 4. \ No newline at end of file +to task 4. +""" + +[compile] +main-file = "star_rating.c" diff --git a/src/C/star-rating:30.11.2022/star_rating.c b/sources/c/star-rating/star_rating.c similarity index 100% rename from src/C/star-rating:30.11.2022/star_rating.c rename to sources/c/star-rating/star_rating.c diff --git a/sources/cpython/example/assignment.toml b/sources/cpython/example/assignment.toml new file mode 100644 index 0000000..6c9fd3a --- /dev/null +++ b/sources/cpython/example/assignment.toml @@ -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" diff --git a/sources/cpython/example/example.py b/sources/cpython/example/example.py new file mode 100644 index 0000000..e84bf70 --- /dev/null +++ b/sources/cpython/example/example.py @@ -0,0 +1,2 @@ +for _ in range(10): + print("Example") diff --git a/sources/cpython/language.toml b/sources/cpython/language.toml new file mode 100644 index 0000000..79f328f --- /dev/null +++ b/sources/cpython/language.toml @@ -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"] diff --git a/src/C/BGCOLOR b/src/C/BGCOLOR deleted file mode 100644 index a66fa0d..0000000 --- a/src/C/BGCOLOR +++ /dev/null @@ -1 +0,0 @@ -00599d \ No newline at end of file diff --git a/src/C/COMPILECMD b/src/C/COMPILECMD deleted file mode 100644 index c3cba06..0000000 --- a/src/C/COMPILECMD +++ /dev/null @@ -1 +0,0 @@ -gcc -o {out} {main} \ No newline at end of file diff --git a/src/C/DESCRIPTION b/src/C/DESCRIPTION deleted file mode 100644 index b754c14..0000000 --- a/src/C/DESCRIPTION +++ /dev/null @@ -1 +0,0 @@ -C (pronounced like the letter c) is a general-purpose computer programming language \ No newline at end of file diff --git a/src/C/FGCOLOR b/src/C/FGCOLOR deleted file mode 100644 index de53aff..0000000 --- a/src/C/FGCOLOR +++ /dev/null @@ -1 +0,0 @@ -ffffff \ No newline at end of file diff --git a/src/C/star-rating:30.11.2022/DESCRIPTION b/src/C/star-rating:30.11.2022/DESCRIPTION deleted file mode 100644 index dcca539..0000000 --- a/src/C/star-rating:30.11.2022/DESCRIPTION +++ /dev/null @@ -1 +0,0 @@ -Does stuff with reviews TODO do desc \ No newline at end of file diff --git a/src/C/star-rating:30.11.2022/MAIN b/src/C/star-rating:30.11.2022/MAIN deleted file mode 100644 index 52efa0a..0000000 --- a/src/C/star-rating:30.11.2022/MAIN +++ /dev/null @@ -1 +0,0 @@ -star_rating.c \ No newline at end of file diff --git a/src/CPython/BGCOLOR b/src/CPython/BGCOLOR deleted file mode 100644 index 7761eea..0000000 --- a/src/CPython/BGCOLOR +++ /dev/null @@ -1 +0,0 @@ -3670a0 \ No newline at end of file diff --git a/src/CPython/DESCRIPTION b/src/CPython/DESCRIPTION deleted file mode 100644 index 73ec8b8..0000000 --- a/src/CPython/DESCRIPTION +++ /dev/null @@ -1 +0,0 @@ -Python is a high-level, general-purpose programming language \ No newline at end of file diff --git a/src/CPython/FGCOLOR b/src/CPython/FGCOLOR deleted file mode 100644 index 8f1bb31..0000000 --- a/src/CPython/FGCOLOR +++ /dev/null @@ -1 +0,0 @@ -fed140 \ No newline at end of file diff --git a/src/CPython/RUNCMD b/src/CPython/RUNCMD deleted file mode 100644 index c4a3c0b..0000000 --- a/src/CPython/RUNCMD +++ /dev/null @@ -1 +0,0 @@ -python3 {main} \ No newline at end of file diff --git a/src/CPython/test:1.1.1/ASSIGNMENT b/src/CPython/test:1.1.1/ASSIGNMENT deleted file mode 100644 index 4bad556..0000000 --- a/src/CPython/test:1.1.1/ASSIGNMENT +++ /dev/null @@ -1 +0,0 @@ -test directions \ No newline at end of file diff --git a/src/CPython/test:1.1.1/DESCRIPTION b/src/CPython/test:1.1.1/DESCRIPTION deleted file mode 100644 index 1b12b17..0000000 --- a/src/CPython/test:1.1.1/DESCRIPTION +++ /dev/null @@ -1 +0,0 @@ -test desc \ No newline at end of file diff --git a/src/CPython/test:1.1.1/MAIN b/src/CPython/test:1.1.1/MAIN deleted file mode 100644 index 9465664..0000000 --- a/src/CPython/test:1.1.1/MAIN +++ /dev/null @@ -1 +0,0 @@ -test.py \ No newline at end of file diff --git a/src/CPython/test:1.1.1/test.py b/src/CPython/test:1.1.1/test.py deleted file mode 100644 index 03797e7..0000000 --- a/src/CPython/test:1.1.1/test.py +++ /dev/null @@ -1 +0,0 @@ -print("test print")