mogs 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. mogs/__init__.py +17 -0
  2. mogs/_assertrewrite.py +44 -0
  3. mogs/_env.py +26 -0
  4. mogs/_run.py +111 -0
  5. mogs/_typst/preview/cmarker/0.1.10/LICENSE +7 -0
  6. mogs/_typst/preview/cmarker/0.1.10/lib.typ +462 -0
  7. mogs/_typst/preview/cmarker/0.1.10/plugin.wasm +0 -0
  8. mogs/_typst/preview/cmarker/0.1.10/typst.toml +11 -0
  9. mogs/_typst/preview/mitex/0.2.7/LICENSE +176 -0
  10. mogs/_typst/preview/mitex/0.2.7/lib.typ +1 -0
  11. mogs/_typst/preview/mitex/0.2.7/mitex.typ +45 -0
  12. mogs/_typst/preview/mitex/0.2.7/mitex.wasm +0 -0
  13. mogs/_typst/preview/mitex/0.2.7/specs/latex/standard.typ +1187 -0
  14. mogs/_typst/preview/mitex/0.2.7/specs/mod.typ +12 -0
  15. mogs/_typst/preview/mitex/0.2.7/specs/prelude.typ +230 -0
  16. mogs/_typst/preview/mitex/0.2.7/typst.toml +12 -0
  17. mogs/assignment.py +172 -0
  18. mogs/build.py +605 -0
  19. mogs/check.py +410 -0
  20. mogs/cli.py +272 -0
  21. mogs/config.py +619 -0
  22. mogs/environment.py +118 -0
  23. mogs/export.py +556 -0
  24. mogs/grader/__init__.py +1 -0
  25. mogs/grader/_process.py +383 -0
  26. mogs/grader/_user.py +7 -0
  27. mogs/grader/bundle.py +320 -0
  28. mogs/grader/child.py +317 -0
  29. mogs/grader/datafiles.py +54 -0
  30. mogs/grader/main.py +132 -0
  31. mogs/grader/protocol.py +175 -0
  32. mogs/grader/results.py +337 -0
  33. mogs/grader/sandbox.py +462 -0
  34. mogs/grader/templates/__init__.py +38 -0
  35. mogs/grader/templates/run_autograder +3 -0
  36. mogs/grader/templates/setup.sh +28 -0
  37. mogs/loader.py +260 -0
  38. mogs/pdf.py +676 -0
  39. mogs/regrade.py +223 -0
  40. mogs/requirements.py +232 -0
  41. mogs/runner.py +281 -0
  42. mogs/scaffold.py +155 -0
  43. mogs/selfcheck.py +173 -0
  44. mogs/source.py +1438 -0
  45. mogs/transform.py +769 -0
  46. mogs-0.7.0.dist-info/METADATA +17 -0
  47. mogs-0.7.0.dist-info/RECORD +49 -0
  48. mogs-0.7.0.dist-info/WHEEL +4 -0
  49. mogs-0.7.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,12 @@
1
+
2
+ #import "prelude.typ": *
3
+ #import "latex/standard.typ": package as latex-std
4
+
5
+ // 1. import all the packages and form a mitex-scope for mitex to use
6
+ #let packages = (latex-std,)
7
+ #let mitex-scope = packages.map(pkg => pkg.scope).sum()
8
+
9
+ // 2. export all packages with specs by metadata and <mitex-packages> label,
10
+ // mitex-cli can fetch them by
11
+ // `typst query --root . ./packages/mitex/specs/mod.typ "<mitex-packages>"`
12
+ #metadata(packages) <mitex-packages>
@@ -0,0 +1,230 @@
1
+ /// Define a normal symbol, as no-argument commands like \alpha
2
+ ///
3
+ /// Arguments:
4
+ /// - s (str): Alias command for typst handler.
5
+ /// For example, alias `\prod` to typst's `product`.
6
+ /// - sym (content): The specific content, as the value of alias in mitex-scope.
7
+ /// For example, there is no direct alias for \negthinspace symbol in typst,
8
+ /// but we can add `h(-(3/18) * 1em)` ourselves
9
+ ///
10
+ /// Return: A spec item and a scope item (none for no scope item)
11
+ #let define-sym(s, sym: none) = {
12
+ (
13
+ (kind: "alias-sym", alias: s),
14
+ if sym != none {
15
+ (alias: s, handle: sym)
16
+ } else {
17
+ none
18
+ },
19
+ )
20
+ }
21
+
22
+ /// Define a greedy command, like \displaystyle
23
+ ///
24
+ /// Arguments:
25
+ /// - s (str): Alias command for typst handler.
26
+ /// For example, alias `\displaystyle` to typst's `mitexdisplay`, as the key in mitex-scope.
27
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
28
+ /// It receives a content argument as all greedy matches to the content
29
+ /// For example, we define `mitexdisplay` to `math.display`
30
+ ///
31
+ /// Return: A spec item and a scope item (none for no scope item)
32
+ #let define-greedy-cmd(s, handle: none) = {
33
+ (
34
+ (kind: "greedy-cmd", alias: s),
35
+ if handle != none {
36
+ (alias: s, handle: handle)
37
+ } else {
38
+ none
39
+ },
40
+ )
41
+ }
42
+
43
+ /// Define an infix command, like \over
44
+ ///
45
+ /// Arguments:
46
+ /// - s (str): Alias command for typst handler.
47
+ /// For example, alias `\over` to typst's `frac`, as the key in mitex-scope.
48
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
49
+ /// It receives two content arguments, as (prev, after) arguments.
50
+ /// For example, we define `\over` to `frac: (num, den) => $(num)/(den)$`
51
+ ///
52
+ /// Return: A spec item and a scope item (none for no scope item)
53
+ #let define-infix-cmd(s, handle: none) = {
54
+ (
55
+ (kind: "infix-cmd", alias: s),
56
+ if handle != none {
57
+ (alias: s, handle: handle)
58
+ } else {
59
+ none
60
+ },
61
+ )
62
+ }
63
+
64
+ /// Define a glob (Global Wildcard) match command with a specified pattern for matching args
65
+ /// Kind of item to match:
66
+ /// - Bracket/b: []
67
+ /// - Parenthesis/p: ()
68
+ /// - Term/t: any rest of terms, typically {} or single char
69
+ ///
70
+ /// Arguments:
71
+ /// - pat (pattern): The pattern for glob-cmd
72
+ /// For example, `{,b}t` for `\sqrt` to support `\sqrt{2}` and `\sqrt[3]{2}`
73
+ /// - s (str): Alias command for typst handler.
74
+ /// For example, alias `\sqrt` to typst's `mitexsqrt`, as the key in mitex-scope.
75
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
76
+ /// It receives variable length arguments, for example `(2,)` or `([3], 2)` for sqrt.
77
+ /// Therefore you need to use `(.. arg) = > {..}` to receive them.
78
+ ///
79
+ /// Return: A spec item and a scope item (none for no scope item)
80
+ #let define-glob-cmd(pat, s, handle: none) = {
81
+ (
82
+ (kind: "glob-cmd", pattern: pat, alias: s),
83
+ if handle != none {
84
+ (alias: s, handle: handle)
85
+ } else {
86
+ none
87
+ },
88
+ )
89
+ }
90
+
91
+ /// Define a command with a fixed number of arguments, like \hat{x} and \frac{1}{2}
92
+ ///
93
+ /// Arguments:
94
+ /// - num (int): The number of arguments for the command.
95
+ /// - alias (str): Alias command for typst handler.
96
+ /// For example, alias `\frac` to typst's `frac`, as the key in mitex-scope.
97
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
98
+ /// It receives fixed number of arguments, for example `frac(1, 2)` for `\frac{1}{2}`.
99
+ ///
100
+ /// Return: A spec item and a scope item (none for no scope item)
101
+ #let define-cmd(num, alias: none, handle: none) = {
102
+ (
103
+ (
104
+ kind: "cmd",
105
+ args: ("kind": "right", "pattern": (kind: "fixed-len", len: num)),
106
+ alias: alias,
107
+ ),
108
+ if handle != none {
109
+ (alias: alias, handle: handle)
110
+ } else {
111
+ none
112
+ },
113
+ )
114
+ }
115
+
116
+ /// Define an environment with a fixed number of arguments, like \begin{alignedat}{2}
117
+ ///
118
+ /// Arguments:
119
+ /// - num (int): The number of arguments as environment options for the environment.
120
+ /// - alias (str): Alias command for typst handler.
121
+ /// For example, alias `\begin{alignedat}{2}` to typst's `alignedat`,
122
+ /// and alias `\begin{aligned}` to typst's `aligned`, as the key in mitex-scope.
123
+ /// - kind (str): environment kind, it could be "is-math", "is-cases", "is-matrix",
124
+ /// "is-itemize", "is-enumerate"
125
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
126
+ /// It receives fixed number of named arguments as environment options,
127
+ /// for example `alignedat(arg0: ..)` or `alignedat(arg0: .., arg1: ..)`.
128
+ /// And it receives variable length arguments as environment body,
129
+ /// Therefore you need to use `(.. arg) = > {..}` to receive them.
130
+ ///
131
+ /// Return: A spec item and a scope item (none for no scope item)
132
+ #let define-env(num, kind: "none", alias: none, handle: none) = {
133
+ (
134
+ (
135
+ kind: "env",
136
+ args: if num != none {
137
+ (kind: "fixed-len", len: num)
138
+ } else {
139
+ (kind: "none")
140
+ },
141
+ ctx_feature: (kind: kind),
142
+ alias: alias,
143
+ ),
144
+ if handle != none {
145
+ (alias: alias, handle: handle)
146
+ } else {
147
+ none
148
+ },
149
+ )
150
+ }
151
+
152
+ #let define-glob-env(pat, kind: "none", alias: none, handle: none) = {
153
+ (
154
+ (
155
+ kind: "glob-env",
156
+ pattern: pat,
157
+ ctx_feature: (kind: kind),
158
+ alias: alias,
159
+ ),
160
+ if handle != none {
161
+ (alias: alias, handle: handle)
162
+ } else {
163
+ none
164
+ },
165
+ )
166
+ }
167
+
168
+ /// Define a symbol without alias and without handler function, like \alpha => alpha
169
+ ///
170
+ /// Return: A spec item and no scope item (none for no scope item)
171
+ #let sym = ((kind: "sym"), none)
172
+
173
+ /// Define a symbol without alias and with handler function,
174
+ /// like \negthinspace => h(-(3/18) * 1em)
175
+ ///
176
+ /// Arguments:
177
+ /// - handle (function): The handler function, as the value of alias in mitex-scope.
178
+ /// For example, define `negthinspace` to handle `h(-(3/18) * 1em)` in mitex-scope
179
+ ///
180
+ /// Return: A symbol spec and a scope item
181
+ #let of-sym(handle) = ((kind: "sym"), (handle: handle))
182
+
183
+ /// Define a left1-op command without handler, like `\limits` for `\sum\limits`
184
+ ///
185
+ /// Arguments:
186
+ /// - alias (str): Alias command for typst handler.
187
+ /// For example, alias `\limits` to typst's `limits`
188
+ /// and alias `\nolimits` to typst's `scripts`
189
+ ///
190
+ /// Return: A cmd spec and no scope item (none for no scope item)
191
+ #let left1-op(alias) = ((kind: "cmd", args: (kind: "left1"), alias: alias), none)
192
+
193
+ /// Define a cmd1 command like \hat{x} => hat(x)
194
+ ///
195
+ /// Return: A cmd1 spec and a scope item (none for no scope item)
196
+ #let cmd1 = ((kind: "cmd1"), none)
197
+
198
+ /// Define a cmd2 command like \binom{1}{2} => binom(1, 2)
199
+ ///
200
+ /// Return: A cmd2 spec and a scope item (none for no scope item)
201
+ #let cmd2 = ((kind: "cmd2"), none)
202
+
203
+ /// Define a matrix environment without handler
204
+ ///
205
+ /// Return: A matrix-env spec and a scope item (none for no scope item)
206
+ #let matrix-env = ((kind: "matrix-env"), none)
207
+
208
+ /// Receives a list of definitions composed of the above functions, and processes them to return a dictionary containing spec and scope.
209
+ #let process-spec(definitions) = {
210
+ let spec = (:)
211
+ let scope = (:)
212
+ for (key, value) in definitions.pairs() {
213
+ let spec-item = value.at(0)
214
+ let scope-item = value.at(1)
215
+ spec.insert(key, spec-item)
216
+ if scope-item != none {
217
+ if "alias" in scope-item and type(scope-item.alias) == str {
218
+ let key = if scope-item.alias.starts-with("#") {
219
+ scope-item.alias.slice(1)
220
+ } else {
221
+ scope-item.alias
222
+ }
223
+ scope.insert(key, scope-item.handle)
224
+ } else {
225
+ scope.insert(key, scope-item.handle)
226
+ }
227
+ }
228
+ }
229
+ (spec: spec, scope: scope)
230
+ }
@@ -0,0 +1,12 @@
1
+ [package]
2
+ name = "mitex"
3
+ version = "0.2.7"
4
+ entrypoint = "lib.typ"
5
+ authors = ["Myriad-Dreamin", "OrangeX4", "Enter-tainer", "Shichien"]
6
+ license = "Apache-2.0"
7
+ description = "LaTeX support for Typst, powered by Rust and WASM."
8
+
9
+ repository = "https://github.com/mitex-rs/mitex"
10
+ categories = ["utility"]
11
+ keywords = ["wasm", "rust", "LaTeX", "equation"]
12
+ exclude = ["examples"]
mogs/assignment.py ADDED
@@ -0,0 +1,172 @@
1
+ """The summary that `assignment()` shows in the source notebook."""
2
+
3
+ import html
4
+ import shlex
5
+ import sys
6
+ from pathlib import Path
7
+ from typing import TYPE_CHECKING
8
+
9
+ from mogs._env import grading
10
+ from mogs.check import (
11
+ FAIL,
12
+ ITEM_STYLE,
13
+ LIST_STYLE,
14
+ NOTE_STYLE,
15
+ PANEL_STYLE,
16
+ PASS,
17
+ SHADE,
18
+ TITLE_STYLE,
19
+ format_points,
20
+ locate,
21
+ )
22
+ from mogs.config import AssignmentConfig, ConfigError, read_config
23
+ from mogs.source import (
24
+ NO_TEXT,
25
+ Notebook,
26
+ SourceError,
27
+ parse_notebook,
28
+ prompt_previews,
29
+ validate,
30
+ )
31
+
32
+ if TYPE_CHECKING:
33
+ import marimo as mo
34
+
35
+ CELL_STYLE = "padding: 0.1em 1em 0.1em 0; vertical-align: top;"
36
+ UNSAVED = "Save the notebook to see the summary of the assignment."
37
+
38
+
39
+ def assignment(
40
+ *,
41
+ name: str | None = None,
42
+ files: list[str] | tuple[str, ...] = (),
43
+ timeout: float = 60,
44
+ budget: float = 540,
45
+ memory: int = 4096,
46
+ python: str = "3.13",
47
+ requirements: str | None = None,
48
+ ) -> "mo.Html":
49
+ """Declare the assignment's settings, and show a summary of the saved notebook.
50
+
51
+ The build reads the arguments from the source without running it, so they must be
52
+ literals. The build also removes the cell with this call from the student notebook.
53
+
54
+ The summary reads the saved notebook file, not the arguments: its questions, their
55
+ points and tests, and any problem the build would report. It is as fresh as the last run
56
+ of the cell. While grading (`MOGS_GRADING=1`), it returns an empty panel.
57
+
58
+ Args:
59
+ name: Names the student zip and the autograder zip; defaults to the notebook's name.
60
+ files: Data files and folders, relative to the notebook, shipped with it.
61
+ timeout: Seconds per test.
62
+ budget: Seconds for the whole grading run.
63
+ memory: MB per grading process, at least 2048. Linux only.
64
+ python: Python version on Gradescope.
65
+ requirements: A requirements file for Gradescope, relative to the notebook. It is used
66
+ instead of the header's `dependencies`.
67
+
68
+ Returns:
69
+ The summary, as marimo HTML.
70
+ """
71
+
72
+ import marimo as mo
73
+
74
+ del name, files, timeout, budget, memory, python, requirements
75
+
76
+ if grading():
77
+ return mo.Html("")
78
+
79
+ path = locate(sys._getframe(1).f_globals)
80
+
81
+ try:
82
+ if path is None:
83
+ raise FileNotFoundError
84
+ src = path.read_text(encoding="utf-8")
85
+ except (OSError, UnicodeDecodeError):
86
+ return mo.Html(_panel(SHADE, "Assignment", f"<div>{html.escape(UNSAVED)}</div>"))
87
+
88
+ try:
89
+ cfg = read_config(src, path)
90
+ nb = parse_notebook(src)
91
+ except (ConfigError, SourceError) as error:
92
+ return mo.Html(_problems(path.stem, [str(error)], path))
93
+
94
+ problems = validate(nb, cfg) + _data_problems(path, cfg)
95
+ if problems:
96
+ return mo.Html(_problems(cfg.name, problems, path))
97
+
98
+ return mo.Html(summary(cfg, nb, path))
99
+
100
+
101
+ def summary(cfg: AssignmentConfig, nb: Notebook, path: Path) -> str:
102
+ """HTML for a valid assignment: one row per question, then how to build it.
103
+
104
+ A written question's row also has the first line of each of its prompt cells.
105
+ """
106
+
107
+ total = sum(question.points for question in cfg.questions.values())
108
+ count = len(cfg.questions)
109
+ title = f"{cfg.name} · {count} question{'s' if count != 1 else ''} · {format_points(total)}"
110
+ tests = nb.tests_by_question()
111
+ rows = []
112
+
113
+ for qid, question in cfg.questions.items():
114
+ public, hidden = tests.get(qid, ([], []))
115
+
116
+ # The kind cell is HTML; a written question lists its prompts under it.
117
+ if question.manual:
118
+ kind = "written" + "".join(
119
+ f'<div style="{NOTE_STYLE}">{html.escape(line)}</div>'
120
+ for line in prompt_previews(nb, qid) or (NO_TEXT,)
121
+ )
122
+ else:
123
+ kind = html.escape(f"auto-graded: {len(public)} public, {len(hidden)} hidden")
124
+
125
+ texts = (qid, question.title, format_points(question.points))
126
+ cells = [*(html.escape(text) for text in texts), kind]
127
+ rows.append(
128
+ "<tr>" + "".join(f'<td style="{CELL_STYLE}">{cell}</td>' for cell in cells) + "</tr>"
129
+ )
130
+
131
+ body = f'<table style="border-collapse: collapse;">{"".join(rows)}</table>'
132
+
133
+ return _panel(PASS, title, body + _footer(path))
134
+
135
+
136
+ def _problems(name: str, problems: list[str], path: Path) -> str:
137
+ """HTML listing what the build would refuse."""
138
+
139
+ count = len(problems)
140
+ title = f"{name} · {count} problem{'s' if count != 1 else ''}"
141
+ items = "".join(f'<li style="{ITEM_STYLE}">{html.escape(problem)}</li>' for problem in problems)
142
+ body = f'<ul style="{LIST_STYLE}">{items}</ul>'
143
+
144
+ return _panel(FAIL, title, body + _footer(path))
145
+
146
+
147
+ def _data_problems(path: Path, cfg: AssignmentConfig) -> list[str]:
148
+ """The build's complaint about the data files, if any."""
149
+
150
+ from mogs.build import data_files
151
+
152
+ try:
153
+ data_files(path.parent, cfg)
154
+ except ConfigError as error:
155
+ return [str(error)]
156
+
157
+ return []
158
+
159
+
160
+ def _footer(path: Path) -> str:
161
+ note = (
162
+ f"Read from {path.name} when this cell ran; run it again after you save changes. "
163
+ f"Build with: mogs build {shlex.quote(str(path.parent))}"
164
+ )
165
+ return f'<div style="{NOTE_STYLE}">{html.escape(note)}</div>'
166
+
167
+
168
+ def _panel(accent: str, title: str, body: str) -> str:
169
+ return (
170
+ f'<div style="{PANEL_STYLE.format(accent=accent, shade=SHADE)}">'
171
+ f'<div style="{TITLE_STYLE}">{html.escape(title)}</div>{body}</div>'
172
+ )