codehood-cli 0.5.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.
- codehood/__init__.py +0 -0
- codehood/__main__.py +4 -0
- codehood/actions/__init__.py +0 -0
- codehood/actions/base.py +119 -0
- codehood/actions/resources.py +30 -0
- codehood/api/__init__.py +0 -0
- codehood/api/base.py +256 -0
- codehood/api/generate.py +1370 -0
- codehood/api/generated.py +3818 -0
- codehood/api/openapi.py +134 -0
- codehood/api/pages.py +53 -0
- codehood/cli/__init__.py +64 -0
- codehood/cli/api.py +422 -0
- codehood/cli/attendance.py +136 -0
- codehood/cli/auth.py +106 -0
- codehood/cli/base.py +42 -0
- codehood/cli/chill.py +52 -0
- codehood/cli/exam.py +107 -0
- codehood/cli/grades.py +185 -0
- codehood/cli/init.py +161 -0
- codehood/cli/push.py +609 -0
- codehood/cli/show.py +77 -0
- codehood/cli/uitils.py +26 -0
- codehood/functional.py +40 -0
- codehood/globs.py +71 -0
- codehood/grades.py +74 -0
- codehood/hypothesis.py +274 -0
- codehood/models/__init__.py +0 -0
- codehood/models/base.py +15 -0
- codehood/models/repo.py +187 -0
- codehood/models/validators.py +177 -0
- codehood/push/__init__.py +225 -0
- codehood/push/base.py +216 -0
- codehood/push/calendar.py +664 -0
- codehood/push/errors.py +136 -0
- codehood/push/exam.py +636 -0
- codehood/push/exclude.py +60 -0
- codehood/push/grades.py +455 -0
- codehood/push/hook.py +48 -0
- codehood/push/links.py +614 -0
- codehood/push/ours.py +78 -0
- codehood/push/plan.py +1981 -0
- codehood/push/project.py +272 -0
- codehood/push/question.py +338 -0
- codehood/push/report.py +244 -0
- codehood/push/repository.py +326 -0
- codehood/push/resource.py +576 -0
- codehood/push/run.py +1275 -0
- codehood/push/unconsumed.py +208 -0
- codehood/push/wire.py +222 -0
- codehood/py.typed +0 -0
- codehood/repo/__init__.py +62 -0
- codehood/repo/_config.py +28 -0
- codehood/repo/_layout.py +132 -0
- codehood/repo/_scaffold.py +244 -0
- codehood/repo/diff.py +71 -0
- codehood/repo/file_tracker.py +124 -0
- codehood/theme.py +101 -0
- codehood/widgets/__init__.py +0 -0
- codehood/widgets/mascot.py +76 -0
- codehood/widgets/questions/__init__.py +65 -0
- codehood/widgets/questions/base.py +119 -0
- codehood/widgets/questions/controls.py +194 -0
- codehood/widgets/questions/fill_in.py +81 -0
- codehood/widgets/questions/responses.py +137 -0
- codehood/widgets/questions/simple.py +117 -0
- codehood_cli-0.5.0.dist-info/METADATA +294 -0
- codehood_cli-0.5.0.dist-info/RECORD +71 -0
- codehood_cli-0.5.0.dist-info/WHEEL +4 -0
- codehood_cli-0.5.0.dist-info/entry_points.txt +4 -0
- codehood_cli-0.5.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
import tomllib
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
import tomlkit
|
|
10
|
+
|
|
11
|
+
from ..models.repo import CourseIdentity, RepoConfig, ServerConfig
|
|
12
|
+
from . import _layout
|
|
13
|
+
from ._config import config_path
|
|
14
|
+
|
|
15
|
+
PathStatus = Literal["created", "exists"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class PathReport:
|
|
20
|
+
"""
|
|
21
|
+
One line of `init`'s output: a path, and whether it was just created.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
path: Path
|
|
25
|
+
status: PathStatus
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class RepoError(Exception):
|
|
29
|
+
"""
|
|
30
|
+
Base class for errors `write_repo` raises. `cli/init.py` catches this
|
|
31
|
+
and reports it as a clean CLI error rather than a traceback.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class IdentityConflictError(RepoError):
|
|
36
|
+
"""
|
|
37
|
+
Raised when an existing `codehood.toml` names a different course than
|
|
38
|
+
the one `init` was asked to write. `init` never changes an identity
|
|
39
|
+
field once committed -- see the spec's "Nothing is ever overwritten".
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def write_repo(
|
|
44
|
+
path: Path,
|
|
45
|
+
identity: CourseIdentity,
|
|
46
|
+
server: ServerConfig,
|
|
47
|
+
*,
|
|
48
|
+
name: str | None = None,
|
|
49
|
+
git: bool = True,
|
|
50
|
+
examples: bool = True,
|
|
51
|
+
) -> list[PathReport]:
|
|
52
|
+
"""
|
|
53
|
+
Write (or repair) a course repository at `path`.
|
|
54
|
+
|
|
55
|
+
Creates whatever is missing from the layout in
|
|
56
|
+
`dev/specs/to-do/init.md`'s "The layout" and leaves whatever already
|
|
57
|
+
exists untouched, so running this twice with the same arguments is a
|
|
58
|
+
no-op. Never overwrites a file.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
path: The repository root. Created if missing.
|
|
62
|
+
identity: The course's `(discipline, instructor, edition)`.
|
|
63
|
+
server: The server this repository syncs with.
|
|
64
|
+
git: Run `git init` (skipped if `path` is already inside a work
|
|
65
|
+
tree).
|
|
66
|
+
examples: Write the example question and exam.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
One `PathReport` per path considered, in write order.
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
IdentityConflictError: An existing `codehood.toml` names a
|
|
73
|
+
different course.
|
|
74
|
+
"""
|
|
75
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
76
|
+
config_path(path).parent.mkdir(parents=True, exist_ok=True)
|
|
77
|
+
reports = [
|
|
78
|
+
ensure_config(config_path(path), identity, server),
|
|
79
|
+
ensure_file(
|
|
80
|
+
path / _layout.README_FILE,
|
|
81
|
+
_layout.render_readme(name or identity.discipline),
|
|
82
|
+
),
|
|
83
|
+
ensure_file(path / _layout.CALENDAR_FILE, _layout.render_calendar()),
|
|
84
|
+
ensure_file(path / _layout.GITIGNORE_FILE, _layout.render_gitignore()),
|
|
85
|
+
ensure_dir(path / _layout.QUESTIONS_DIR),
|
|
86
|
+
ensure_dir(path / _layout.EXAMS_DIR),
|
|
87
|
+
ensure_dir(path / _layout.RESOURCES_DIR),
|
|
88
|
+
ensure_dir(path / _layout.REPOSITORIES_DIR),
|
|
89
|
+
*ensure_machine_dir(path),
|
|
90
|
+
]
|
|
91
|
+
if examples:
|
|
92
|
+
reports.append(
|
|
93
|
+
ensure_file(
|
|
94
|
+
path / _layout.QUESTIONS_DIR / _layout.EXAMPLE_QUESTION_FILE,
|
|
95
|
+
_layout.render_example_question(),
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
reports.append(
|
|
99
|
+
ensure_file(
|
|
100
|
+
path / _layout.EXAMS_DIR / _layout.EXAMPLE_EXAM_FILE,
|
|
101
|
+
_layout.render_example_exam(identity.discipline),
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
git_report = ensure_git(path, enabled=git)
|
|
105
|
+
if git_report is not None:
|
|
106
|
+
reports.append(git_report)
|
|
107
|
+
return [
|
|
108
|
+
PathReport(_relative_to(report.path, path), report.status) for report in reports
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _relative_to(path: Path, root: Path) -> Path:
|
|
113
|
+
"""
|
|
114
|
+
`path` relative to `root`, or `path` itself when it lies elsewhere (a config override).
|
|
115
|
+
"""
|
|
116
|
+
try:
|
|
117
|
+
return path.relative_to(root)
|
|
118
|
+
except ValueError:
|
|
119
|
+
return path
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def ensure_machine_dir(root: Path) -> list[PathReport]:
|
|
123
|
+
"""
|
|
124
|
+
Create `.local/codehood/` under `root` with the `.gitignore` that keeps it out of git.
|
|
125
|
+
"""
|
|
126
|
+
directory = root / _layout.MACHINE_DIR
|
|
127
|
+
return [
|
|
128
|
+
ensure_dir(directory),
|
|
129
|
+
ensure_file(directory / _layout.GITIGNORE_FILE, _layout.MACHINE_GITIGNORE),
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def ensure_file(path: Path, content: str) -> PathReport:
|
|
134
|
+
"""
|
|
135
|
+
Write `content` to `path` if it does not already exist.
|
|
136
|
+
"""
|
|
137
|
+
if path.exists():
|
|
138
|
+
return PathReport(path, "exists")
|
|
139
|
+
path.write_text(content, encoding="utf-8")
|
|
140
|
+
return PathReport(path, "created")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def ensure_dir(path: Path) -> PathReport:
|
|
144
|
+
"""
|
|
145
|
+
Create `path` as a directory if it does not already exist.
|
|
146
|
+
"""
|
|
147
|
+
existed = path.exists()
|
|
148
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
149
|
+
return PathReport(path, "exists" if existed else "created")
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def read_config(path: Path) -> RepoConfig | None:
|
|
153
|
+
"""
|
|
154
|
+
Read `codehood.toml` at `path`, or `None` if it does not exist.
|
|
155
|
+
|
|
156
|
+
Used by anything that wants the current repository's server URL as a
|
|
157
|
+
default (e.g. `api.base.get_client`) without duplicating `_ensure_config`'s
|
|
158
|
+
parsing.
|
|
159
|
+
"""
|
|
160
|
+
if not path.exists():
|
|
161
|
+
return None
|
|
162
|
+
return RepoConfig.model_validate(tomllib.loads(path.read_text(encoding="utf-8")))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def ensure_config(
|
|
166
|
+
path: Path, identity: CourseIdentity, server: ServerConfig
|
|
167
|
+
) -> PathReport:
|
|
168
|
+
"""
|
|
169
|
+
Write `codehood.toml`, or validate an existing one.
|
|
170
|
+
|
|
171
|
+
An existing file is read but never rewritten, so a second `init` with the
|
|
172
|
+
same arguments changes no bytes.
|
|
173
|
+
"""
|
|
174
|
+
if path.exists():
|
|
175
|
+
existing = RepoConfig.model_validate(
|
|
176
|
+
tomllib.loads(path.read_text(encoding="utf-8"))
|
|
177
|
+
)
|
|
178
|
+
if existing.course != identity:
|
|
179
|
+
raise IdentityConflictError(
|
|
180
|
+
f"{path} already names a course "
|
|
181
|
+
f"(discipline={existing.course.discipline!r}, "
|
|
182
|
+
f"instructor={existing.course.instructor!r}, "
|
|
183
|
+
f"edition={existing.course.edition!r}); "
|
|
184
|
+
"init never changes an identity field"
|
|
185
|
+
)
|
|
186
|
+
return PathReport(path, "exists")
|
|
187
|
+
path.write_text(render_config(identity, server), encoding="utf-8")
|
|
188
|
+
return PathReport(path, "created")
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def render_config(identity: CourseIdentity, server: ServerConfig) -> str:
|
|
192
|
+
"""
|
|
193
|
+
Render `codehood.toml`'s contents as TOML text.
|
|
194
|
+
"""
|
|
195
|
+
doc = tomlkit.document()
|
|
196
|
+
course = tomlkit.table()
|
|
197
|
+
course["discipline"] = identity.discipline
|
|
198
|
+
course["instructor"] = identity.instructor
|
|
199
|
+
course["edition"] = identity.edition
|
|
200
|
+
doc["course"] = course
|
|
201
|
+
server_table = tomlkit.table()
|
|
202
|
+
server_table["url"] = server.url
|
|
203
|
+
doc["server"] = server_table
|
|
204
|
+
return tomlkit.dumps(doc)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def ensure_git(path: Path, *, enabled: bool) -> PathReport | None:
|
|
208
|
+
"""
|
|
209
|
+
Run `git init` in `path`, unless disabled or already inside a work
|
|
210
|
+
tree.
|
|
211
|
+
|
|
212
|
+
Returns `None` when there is nothing to report: `git` is disabled, or
|
|
213
|
+
`path` is already inside a work tree it does not own (so creating a
|
|
214
|
+
nested repository would be wrong -- see the spec's git test).
|
|
215
|
+
"""
|
|
216
|
+
if not enabled:
|
|
217
|
+
return None
|
|
218
|
+
git_dir = path / _layout.GIT_DIR
|
|
219
|
+
if git_dir.exists():
|
|
220
|
+
return PathReport(git_dir, "exists")
|
|
221
|
+
if is_inside_work_tree(path):
|
|
222
|
+
return None
|
|
223
|
+
subprocess.run(
|
|
224
|
+
["git", "init"],
|
|
225
|
+
cwd=path,
|
|
226
|
+
check=True,
|
|
227
|
+
capture_output=True,
|
|
228
|
+
text=True,
|
|
229
|
+
)
|
|
230
|
+
return PathReport(git_dir, "created")
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def is_inside_work_tree(path: Path) -> bool:
|
|
234
|
+
"""
|
|
235
|
+
Return whether `path` is already inside a git work tree.
|
|
236
|
+
"""
|
|
237
|
+
result = subprocess.run(
|
|
238
|
+
["git", "rev-parse", "--is-inside-work-tree"],
|
|
239
|
+
cwd=path,
|
|
240
|
+
capture_output=True,
|
|
241
|
+
check=False,
|
|
242
|
+
text=True,
|
|
243
|
+
)
|
|
244
|
+
return result.returncode == 0 and result.stdout.strip() == "true"
|
codehood/repo/diff.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class FileType(enum.IntEnum):
|
|
7
|
+
"""
|
|
8
|
+
File by type.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
WARNING = -1
|
|
12
|
+
UNTRACKED = 0
|
|
13
|
+
CONFIG = 1
|
|
14
|
+
README = 2
|
|
15
|
+
ROSTER = 3
|
|
16
|
+
CALENDAR = 4
|
|
17
|
+
QUESTION = 8
|
|
18
|
+
RESOURCE = 16
|
|
19
|
+
EXAM = 32
|
|
20
|
+
|
|
21
|
+
def path(self, rel_path: Path | str, /) -> Path:
|
|
22
|
+
"""
|
|
23
|
+
Return the relative path to the repo root for a path of the given type.
|
|
24
|
+
"""
|
|
25
|
+
return self.base() / rel_path
|
|
26
|
+
|
|
27
|
+
def base(self) -> Path:
|
|
28
|
+
"""
|
|
29
|
+
The base path (root directory) for each file type in the repo.
|
|
30
|
+
"""
|
|
31
|
+
return BASE_PATHS.get(self, Path(""))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
BASE_PATHS = {
|
|
35
|
+
FileType.QUESTION: Path("questions"),
|
|
36
|
+
FileType.RESOURCE: Path("resources"),
|
|
37
|
+
FileType.EXAM: Path("exams"),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class AddFile:
|
|
43
|
+
name: str
|
|
44
|
+
type: FileType
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def path(self):
|
|
48
|
+
return self.type.path(self.name)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class DeleteFile:
|
|
53
|
+
name: str
|
|
54
|
+
type: FileType
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def path(self):
|
|
58
|
+
return self.type.path(self.name)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass(frozen=True)
|
|
62
|
+
class UpdateFile:
|
|
63
|
+
name: str
|
|
64
|
+
type: FileType
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def path(self):
|
|
68
|
+
return self.type.path(self.name)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Diff = AddFile | DeleteFile | UpdateFile
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable, Sequence
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Literal, NamedTuple, Protocol
|
|
6
|
+
|
|
7
|
+
from ..functional import curry
|
|
8
|
+
from ..globs import is_excluded
|
|
9
|
+
from .diff import AddFile, Diff, FileType
|
|
10
|
+
|
|
11
|
+
MDQ_SUFFIXES = (".md", ".mdq")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# File scanners
|
|
16
|
+
#
|
|
17
|
+
class ScanFiles(Protocol):
|
|
18
|
+
def __call__(self) -> Iterable[File]: ...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@curry(1)
|
|
22
|
+
def scan_files(root: Path, exclude: Sequence[str] = ()) -> Iterable[File]:
|
|
23
|
+
"""
|
|
24
|
+
Yield all files codehood tracks in the repository.
|
|
25
|
+
|
|
26
|
+
Files outside a Codehood tree are ignored. Files under `resources/` and
|
|
27
|
+
`questions/` that match an `exclude` pattern (see `codehood.globs`) are
|
|
28
|
+
still yielded, flagged `excluded`, so a caller can report them and
|
|
29
|
+
protect what the server holds for them.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def skipped(path: Path) -> bool:
|
|
33
|
+
return is_excluded(path.relative_to(root).as_posix(), exclude)
|
|
34
|
+
|
|
35
|
+
def if_exists(path: str | Path, typ: FileType):
|
|
36
|
+
if (path := root / path).exists():
|
|
37
|
+
yield File(str(path.relative_to(root)), typ)
|
|
38
|
+
|
|
39
|
+
# Basic common files
|
|
40
|
+
yield from if_exists("codehood.toml", FileType.CONFIG)
|
|
41
|
+
yield from if_exists("README.md", FileType.README)
|
|
42
|
+
yield from if_exists("calendar.md", FileType.CALENDAR)
|
|
43
|
+
yield from if_exists("roster.csv", FileType.ROSTER)
|
|
44
|
+
|
|
45
|
+
# Resources -- recurses, and only files become resources (see
|
|
46
|
+
# `dev/specs/to-do/push.md`, "A resource's slug is its flattened path
|
|
47
|
+
# under `resources/`": a subdirectory is not itself a resource).
|
|
48
|
+
base = root / "resources"
|
|
49
|
+
for resource in base.rglob("*"):
|
|
50
|
+
if resource.is_dir():
|
|
51
|
+
continue
|
|
52
|
+
name = str(resource.relative_to(base))
|
|
53
|
+
yield File(name, FileType.RESOURCE, excluded=skipped(resource))
|
|
54
|
+
|
|
55
|
+
# Questions -- recurses, and only files become questions (same fix as
|
|
56
|
+
# `resources/`, see `dev/specs/to-do/push-questions.md`).
|
|
57
|
+
base = root / "questions"
|
|
58
|
+
for question in base.rglob("*"):
|
|
59
|
+
if question.is_dir():
|
|
60
|
+
continue
|
|
61
|
+
name = str(question.relative_to(base))
|
|
62
|
+
excluded = skipped(question)
|
|
63
|
+
if question.suffix in MDQ_SUFFIXES:
|
|
64
|
+
yield File(name, FileType.QUESTION, excluded=excluded)
|
|
65
|
+
else:
|
|
66
|
+
yield File(
|
|
67
|
+
name,
|
|
68
|
+
FileType.WARNING,
|
|
69
|
+
f"not a question file: {question.name}",
|
|
70
|
+
excluded=excluded,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Exams -- recurses, and only files become exams. The directories are
|
|
74
|
+
# the author's: type and status come from the path (see `push.exam`).
|
|
75
|
+
base = root / "exams"
|
|
76
|
+
for exam in sorted(base.rglob("*")):
|
|
77
|
+
if exam.is_dir():
|
|
78
|
+
continue
|
|
79
|
+
name = str(exam.relative_to(base))
|
|
80
|
+
excluded = skipped(exam)
|
|
81
|
+
if exam.suffix in MDQ_SUFFIXES:
|
|
82
|
+
yield File(name, FileType.EXAM, excluded=excluded)
|
|
83
|
+
else:
|
|
84
|
+
yield File(
|
|
85
|
+
name, FileType.WARNING, f"not an exam file: {name}", excluded=excluded
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
#
|
|
90
|
+
# Differs
|
|
91
|
+
#
|
|
92
|
+
class ScanDiffs(Protocol):
|
|
93
|
+
def __call__(self, files: Iterable[File]) -> Iterable[Diff]: ...
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@curry(1)
|
|
97
|
+
def find_diffs(
|
|
98
|
+
root: Path,
|
|
99
|
+
files: Iterable[File],
|
|
100
|
+
mode: Literal["quick", "strict"] = "quick",
|
|
101
|
+
) -> Iterable[Diff]:
|
|
102
|
+
"""
|
|
103
|
+
Track files in the repo and flag which ones were changed since the last commit.
|
|
104
|
+
|
|
105
|
+
There is no local state to diff against -- see `dev/specs/to-do/
|
|
106
|
+
push.md`, "There is no local state" -- so every file currently reads
|
|
107
|
+
as added. A caller wanting an actual diff compares against the
|
|
108
|
+
server's state instead, the way `codehood.push.plan.plan_sync` does
|
|
109
|
+
for resources.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
for file in files:
|
|
113
|
+
yield AddFile(file.name, file.type)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class File(NamedTuple):
|
|
117
|
+
name: str
|
|
118
|
+
type: FileType
|
|
119
|
+
message: str | None = None
|
|
120
|
+
excluded: bool = False
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def path(self) -> Path:
|
|
124
|
+
return self.type.path(self.name)
|
codehood/theme.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Codehood's visual identity for the terminal (see `docs/ui/identity.md`).
|
|
3
|
+
|
|
4
|
+
Textual apps use the hex-based themes below. Plain Rich output uses
|
|
5
|
+
`RICH_THEME`, which maps semantic roles onto the 16 ANSI colors so the user's
|
|
6
|
+
terminal palette decides the exact hues.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import ClassVar
|
|
12
|
+
|
|
13
|
+
from rich.style import Style
|
|
14
|
+
from rich.theme import Theme as RichTheme
|
|
15
|
+
from textual.app import App
|
|
16
|
+
from textual.theme import Theme
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"CODEHOOD_DARK",
|
|
20
|
+
"CODEHOOD_LIGHT",
|
|
21
|
+
"RICH_THEME",
|
|
22
|
+
"CodehoodApp",
|
|
23
|
+
"role_style",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
#: Forest: the default theme. Token names follow identity.md.
|
|
27
|
+
CODEHOOD_DARK = Theme(
|
|
28
|
+
name="codehood-dark",
|
|
29
|
+
dark=True,
|
|
30
|
+
background="#0d211a", # base-200
|
|
31
|
+
surface="#15302a", # base-100
|
|
32
|
+
panel="#1f4034", # base-300
|
|
33
|
+
foreground="#e6f0ea", # content
|
|
34
|
+
primary="#4aa0ff",
|
|
35
|
+
secondary="#ffc400",
|
|
36
|
+
accent="#ff4d97",
|
|
37
|
+
warning="#ff6f1a",
|
|
38
|
+
error="#ff5c5c",
|
|
39
|
+
success="#4cd08a",
|
|
40
|
+
variables={
|
|
41
|
+
"info": "#00d2e6",
|
|
42
|
+
"text-muted": "#8fb3a4",
|
|
43
|
+
"foreground-muted": "#8fb3a4",
|
|
44
|
+
},
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
#: Cerrado: the light theme.
|
|
48
|
+
CODEHOOD_LIGHT = Theme(
|
|
49
|
+
name="codehood",
|
|
50
|
+
dark=False,
|
|
51
|
+
background="#f7f3ea", # base-200
|
|
52
|
+
surface="#fffdf8", # base-100
|
|
53
|
+
panel="#e6dfd0", # base-300
|
|
54
|
+
foreground="#1f2a24", # content
|
|
55
|
+
primary="#1b4b9b",
|
|
56
|
+
secondary="#f2b800",
|
|
57
|
+
accent="#a8309a",
|
|
58
|
+
warning="#e0731f",
|
|
59
|
+
error="#b5452a",
|
|
60
|
+
success="#337a27",
|
|
61
|
+
variables={
|
|
62
|
+
"info": "#2670c0",
|
|
63
|
+
"text-muted": "#5f6862",
|
|
64
|
+
"foreground-muted": "#5f6862",
|
|
65
|
+
},
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
#: Semantic style names for Rich output, mapped to ANSI color names.
|
|
69
|
+
RICH_THEME = RichTheme(
|
|
70
|
+
{
|
|
71
|
+
"error": "red",
|
|
72
|
+
"success": "green",
|
|
73
|
+
"warning": "yellow",
|
|
74
|
+
"secondary": "yellow",
|
|
75
|
+
"primary": "blue",
|
|
76
|
+
"accent": "magenta",
|
|
77
|
+
"info": "cyan",
|
|
78
|
+
"muted": "bright_black",
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def role_style(name: str) -> Style:
|
|
84
|
+
"""
|
|
85
|
+
Return the ANSI-based Rich style for a semantic role such as `error`.
|
|
86
|
+
|
|
87
|
+
Useful inside Textual, where `Text` styles are not looked up in a Rich theme.
|
|
88
|
+
"""
|
|
89
|
+
return RICH_THEME.styles[name]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class CodehoodApp[ReturnType](App[ReturnType]):
|
|
93
|
+
"""Base app that registers the Codehood themes and starts on Forest."""
|
|
94
|
+
|
|
95
|
+
THEMES: ClassVar[tuple[Theme, ...]] = (CODEHOOD_DARK, CODEHOOD_LIGHT)
|
|
96
|
+
|
|
97
|
+
def __init__(self) -> None:
|
|
98
|
+
super().__init__()
|
|
99
|
+
for theme in self.THEMES:
|
|
100
|
+
self.register_theme(theme)
|
|
101
|
+
self.theme = CODEHOOD_DARK.name
|
|
File without changes
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""
|
|
2
|
+
`Mascot`: the small blocky codehood mascot, looping through a blink/hop
|
|
3
|
+
animation. Reusable anywhere in the TUI -- `codehood chill`
|
|
4
|
+
(`src/codehood_cli/cli/chill.py`) just mounts one full-screen with a caption
|
|
5
|
+
underneath.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from textual.widgets import Static
|
|
11
|
+
|
|
12
|
+
__all__ = ["Mascot"]
|
|
13
|
+
|
|
14
|
+
_IDLE = """\
|
|
15
|
+
████████
|
|
16
|
+
██████████
|
|
17
|
+
██ ██ ██
|
|
18
|
+
████████ \
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
_BLINK = """\
|
|
22
|
+
████████
|
|
23
|
+
██████████
|
|
24
|
+
██████████
|
|
25
|
+
████████ \
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
_SQUAT = """\
|
|
29
|
+
██████████
|
|
30
|
+
██ ██ ██
|
|
31
|
+
████████ \
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
_JUMP = """\
|
|
35
|
+
████████
|
|
36
|
+
██████████
|
|
37
|
+
██ ██ ██
|
|
38
|
+
██████████
|
|
39
|
+
████████ \
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
#: Mostly idle, with an occasional blink or hop thrown in.
|
|
43
|
+
_SEQUENCE = (_IDLE, _IDLE, _BLINK, _IDLE, _SQUAT, _JUMP, _SQUAT, _IDLE)
|
|
44
|
+
|
|
45
|
+
#: Seconds each frame stays on screen.
|
|
46
|
+
_FRAME_SECONDS = 0.4
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Mascot(Static):
|
|
50
|
+
"""The codehood mascot. Starts animating as soon as it's mounted."""
|
|
51
|
+
|
|
52
|
+
DEFAULT_CSS = """
|
|
53
|
+
Mascot {
|
|
54
|
+
width: auto;
|
|
55
|
+
height: auto;
|
|
56
|
+
content-align: center middle;
|
|
57
|
+
text-align: center;
|
|
58
|
+
}
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def __init__(
|
|
62
|
+
self,
|
|
63
|
+
*,
|
|
64
|
+
name: str | None = None,
|
|
65
|
+
id: str | None = None,
|
|
66
|
+
classes: str | None = None,
|
|
67
|
+
) -> None:
|
|
68
|
+
super().__init__(_SEQUENCE[0], name=name, id=id, classes=classes)
|
|
69
|
+
self._frame = 0
|
|
70
|
+
|
|
71
|
+
def on_mount(self) -> None:
|
|
72
|
+
self.set_interval(_FRAME_SECONDS, self._advance)
|
|
73
|
+
|
|
74
|
+
def _advance(self) -> None:
|
|
75
|
+
self._frame = (self._frame + 1) % len(_SEQUENCE)
|
|
76
|
+
self.update(_SEQUENCE[self._frame])
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Interactive Textual widgets for answering one MDQ question at a time.
|
|
3
|
+
|
|
4
|
+
Not wired into any `codehood` command yet -- see
|
|
5
|
+
`dev/specs/to-do/question-widgets.md`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from .base import QuestionDocument, QuestionWidget, question_document
|
|
11
|
+
from .fill_in import FillInQuestion
|
|
12
|
+
from .responses import (
|
|
13
|
+
BlankResponse,
|
|
14
|
+
EssayResponse,
|
|
15
|
+
FillInResponse,
|
|
16
|
+
MultipleChoiceResponse,
|
|
17
|
+
MultipleSelectionResponse,
|
|
18
|
+
NumericResponse,
|
|
19
|
+
Response,
|
|
20
|
+
ShortAnswerResponse,
|
|
21
|
+
TrueFalseResponse,
|
|
22
|
+
)
|
|
23
|
+
from .simple import (
|
|
24
|
+
EssayQuestion,
|
|
25
|
+
MultipleChoiceQuestion,
|
|
26
|
+
MultipleSelectionQuestion,
|
|
27
|
+
NumericQuestion,
|
|
28
|
+
ShortAnswerQuestion,
|
|
29
|
+
TrueFalseQuestion,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"QUESTION_WIDGETS",
|
|
34
|
+
"BlankResponse",
|
|
35
|
+
"EssayQuestion",
|
|
36
|
+
"EssayResponse",
|
|
37
|
+
"FillInQuestion",
|
|
38
|
+
"FillInResponse",
|
|
39
|
+
"MultipleChoiceQuestion",
|
|
40
|
+
"MultipleChoiceResponse",
|
|
41
|
+
"MultipleSelectionQuestion",
|
|
42
|
+
"MultipleSelectionResponse",
|
|
43
|
+
"NumericQuestion",
|
|
44
|
+
"NumericResponse",
|
|
45
|
+
"QuestionDocument",
|
|
46
|
+
"QuestionWidget",
|
|
47
|
+
"Response",
|
|
48
|
+
"ShortAnswerQuestion",
|
|
49
|
+
"ShortAnswerResponse",
|
|
50
|
+
"TrueFalseQuestion",
|
|
51
|
+
"TrueFalseResponse",
|
|
52
|
+
"question_document",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
#: Dispatch a parsed document to its widget class by `document["type"]` --
|
|
56
|
+
#: `QUESTION_WIDGETS[document["type"]](document, on_response=...)`.
|
|
57
|
+
QUESTION_WIDGETS: dict[str, type[QuestionWidget]] = {
|
|
58
|
+
"multiple-choice": MultipleChoiceQuestion,
|
|
59
|
+
"multiple-selection": MultipleSelectionQuestion,
|
|
60
|
+
"true-false": TrueFalseQuestion,
|
|
61
|
+
"numeric": NumericQuestion,
|
|
62
|
+
"short-answer": ShortAnswerQuestion,
|
|
63
|
+
"essay": EssayQuestion,
|
|
64
|
+
"fill-in": FillInQuestion,
|
|
65
|
+
}
|