uwquest 0.1.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.
- quest/__init__.py +34 -0
- quest/cli.py +237 -0
- quest/core/__init__.py +2 -0
- quest/core/models.py +113 -0
- quest/core/parsers.py +136 -0
- quest/core/session.py +275 -0
- quest/errors.py +39 -0
- quest/readers/__init__.py +32 -0
- quest/readers/academic_requirements.py +36 -0
- quest/readers/account.py +25 -0
- quest/readers/course_availability.py +160 -0
- quest/readers/dashboard.py +29 -0
- quest/readers/enrollment_dates.py +15 -0
- quest/readers/financial_aid.py +19 -0
- quest/readers/grades.py +43 -0
- quest/readers/holds.py +28 -0
- quest/readers/milestones.py +20 -0
- quest/readers/program.py +19 -0
- quest/readers/schedule.py +79 -0
- quest/readers/transcript.py +43 -0
- quest/tui/__init__.py +7 -0
- quest/tui/app.py +249 -0
- uwquest-0.1.0.dist-info/METADATA +199 -0
- uwquest-0.1.0.dist-info/RECORD +27 -0
- uwquest-0.1.0.dist-info/WHEEL +4 -0
- uwquest-0.1.0.dist-info/entry_points.txt +2 -0
- uwquest-0.1.0.dist-info/licenses/LICENSE +21 -0
quest/__init__.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (C) 2026 Amol Venkataraman
|
|
3
|
+
"""uwquest: a read-first client for UWaterloo Quest (PeopleSoft) self-service.
|
|
4
|
+
|
|
5
|
+
The session layer loads a browser cookie session and drives the PeopleSoft
|
|
6
|
+
component/ICAction state machine; readers turn pages into plain dataclasses.
|
|
7
|
+
Nothing here enrolls, pays, or touches Personal Information.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
from .core.session import (
|
|
13
|
+
BrowserCookieSource,
|
|
14
|
+
FileCookieSource,
|
|
15
|
+
QuestSession,
|
|
16
|
+
)
|
|
17
|
+
from .errors import (
|
|
18
|
+
BlockedAction,
|
|
19
|
+
BlockedComponent,
|
|
20
|
+
NotAuthorized,
|
|
21
|
+
QuestError,
|
|
22
|
+
SessionExpired,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"BlockedAction",
|
|
27
|
+
"BlockedComponent",
|
|
28
|
+
"BrowserCookieSource",
|
|
29
|
+
"FileCookieSource",
|
|
30
|
+
"NotAuthorized",
|
|
31
|
+
"QuestError",
|
|
32
|
+
"QuestSession",
|
|
33
|
+
"SessionExpired",
|
|
34
|
+
]
|
quest/cli.py
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (C) 2026 Amol Venkataraman
|
|
3
|
+
"""Command-line interface. Read-only; prints tables or JSON."""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import dataclasses
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from . import __version__, readers
|
|
13
|
+
from .core.session import BrowserCookieSource, FileCookieSource, QuestSession
|
|
14
|
+
from .errors import QuestError
|
|
15
|
+
|
|
16
|
+
DEFAULT_COOKIES = os.path.expanduser(
|
|
17
|
+
os.environ.get("QUEST_COOKIES", "~/.config/quest/cookies.json")
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _build_session(args) -> QuestSession:
|
|
22
|
+
if args.browser:
|
|
23
|
+
source = BrowserCookieSource(args.browser)
|
|
24
|
+
else:
|
|
25
|
+
path = args.cookies_file or DEFAULT_COOKIES
|
|
26
|
+
if not os.path.exists(path):
|
|
27
|
+
sys.exit(
|
|
28
|
+
f"error: no cookies file at {path}. Export Quest cookies there, pass "
|
|
29
|
+
"--cookies-file PATH, or use --browser brave to read them live."
|
|
30
|
+
)
|
|
31
|
+
source = FileCookieSource(path)
|
|
32
|
+
return QuestSession(source, base=args.base)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _norm(x):
|
|
36
|
+
if dataclasses.is_dataclass(x) and not isinstance(x, type):
|
|
37
|
+
return dataclasses.asdict(x)
|
|
38
|
+
return x
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _cut(text: str, width: int) -> str:
|
|
42
|
+
text = " ".join(str(text).split())
|
|
43
|
+
return text if len(text) <= width else text[: width - 1] + "…"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _table(headers: list[str], rows: list[list], max_widths: list[int] | None = None) -> str:
|
|
47
|
+
"""Plain aligned columns; cells longer than their max width are cut with '…'."""
|
|
48
|
+
max_widths = max_widths or [10_000] * len(headers)
|
|
49
|
+
cells = [[_cut(v, m) for v, m in zip(r, max_widths, strict=True)] for r in rows]
|
|
50
|
+
widths = [max(len(h), *(len(r[i]) for r in cells)) for i, h in enumerate(headers)]
|
|
51
|
+
lines = [" ".join(h.ljust(w) for h, w in zip(headers, widths, strict=True)),
|
|
52
|
+
" ".join("-" * w for w in widths)]
|
|
53
|
+
lines += [" ".join(c.ljust(w) for c, w in zip(r, widths, strict=True)) for r in cells]
|
|
54
|
+
return "\n".join(line.rstrip() for line in lines)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _split_course(course: str) -> tuple[str, str]:
|
|
58
|
+
code, _, title = course.partition(" - ")
|
|
59
|
+
return code.strip(), title.strip()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _format_status(d: dict) -> str:
|
|
63
|
+
p = d.get("program") or {}
|
|
64
|
+
ne = d.get("next_enrollment") or {}
|
|
65
|
+
holds = d.get("holds") or []
|
|
66
|
+
load = ", ".join(x for x in (p.get("academic_load"), p.get("campus")) if x)
|
|
67
|
+
hold_text = "none" if not holds else f"{len(holds)}: " + "; ".join(
|
|
68
|
+
h.get("reason", "") for h in holds
|
|
69
|
+
)
|
|
70
|
+
nxt = ", ".join(x for x in (ne.get("session"), ne.get("begins_on")) if x) or "-"
|
|
71
|
+
lines = [
|
|
72
|
+
f"{'Program':<17}{p.get('plan', '')}",
|
|
73
|
+
f"{'Load':<17}{load}",
|
|
74
|
+
f"{'Holds':<17}{hold_text}",
|
|
75
|
+
f"{'Next enrollment':<17}{nxt}",
|
|
76
|
+
"",
|
|
77
|
+
f"This term: {d.get('classes_this_term', 0)} classes",
|
|
78
|
+
]
|
|
79
|
+
for c in d.get("schedule") or []:
|
|
80
|
+
code, title = _split_course(c.get("course", ""))
|
|
81
|
+
lines.append(f" {code:<10} {_cut(title, 40):<40} {c.get('status', '')}")
|
|
82
|
+
lines += ["", "Account (latest terms)"]
|
|
83
|
+
for a in (d.get("account") or [])[:4]:
|
|
84
|
+
lines.append(f" {a['term']:<14} {a['registered_status']:<18} {a['balance']:>10}")
|
|
85
|
+
return "\n".join(lines)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _format_schedule(classes: list[dict]) -> str:
|
|
89
|
+
rows = []
|
|
90
|
+
for c in classes:
|
|
91
|
+
code, title = _split_course(c.get("course", ""))
|
|
92
|
+
for n, m in enumerate(c.get("meetings") or [{}]):
|
|
93
|
+
first = n == 0
|
|
94
|
+
rows.append([
|
|
95
|
+
code if first else "", title if first else "",
|
|
96
|
+
f"{m.get('component', '')} {m.get('section', '')}".strip(),
|
|
97
|
+
m.get("days_times", ""), m.get("room", ""), m.get("instructor", ""),
|
|
98
|
+
c.get("status", "") if first else "",
|
|
99
|
+
])
|
|
100
|
+
return _table(
|
|
101
|
+
["Course", "Title", "Class", "Days/Times", "Room", "Instructor", "Status"],
|
|
102
|
+
rows, [9, 15, 7, 21, 13, 15, 9],
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _format_classes(sections: list[dict]) -> str:
|
|
107
|
+
seats = any(s.get("enrollment_cap") for s in sections)
|
|
108
|
+
headers = ["Class", "Section", "Status", "Days/Times", "Room", "Instructor"]
|
|
109
|
+
widths = [5, 15, 9, 21, 9, 18]
|
|
110
|
+
if seats:
|
|
111
|
+
headers += ["Enrl", "Wait"]
|
|
112
|
+
widths = [5, 15, 9, 21, 9, 15, 7, 4]
|
|
113
|
+
rows = []
|
|
114
|
+
for s in sections:
|
|
115
|
+
row = [s.get("class_nbr", ""), s.get("section", ""), s.get("status", ""),
|
|
116
|
+
s.get("days_time", ""), s.get("room", ""), s.get("instructor", "")]
|
|
117
|
+
if seats:
|
|
118
|
+
enrl = f"{s.get('enrollment_total', '')}/{s.get('enrollment_cap', '')}"
|
|
119
|
+
row += [enrl if enrl != "/" else "", s.get("waitlist_total", "")]
|
|
120
|
+
rows.append(row)
|
|
121
|
+
return _table(headers, rows, widths)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
_FORMATTERS = {"status": _format_status, "schedule": _format_schedule,
|
|
125
|
+
"classes": _format_classes}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _emit(data, as_json: bool, cmd: str = "") -> None:
|
|
129
|
+
rows = [_norm(x) for x in data] if isinstance(data, list) else _norm(data)
|
|
130
|
+
if as_json:
|
|
131
|
+
print(json.dumps(rows, indent=2))
|
|
132
|
+
return
|
|
133
|
+
if not rows:
|
|
134
|
+
print("(nothing to show)")
|
|
135
|
+
return
|
|
136
|
+
if cmd in _FORMATTERS:
|
|
137
|
+
print(_FORMATTERS[cmd](rows))
|
|
138
|
+
elif isinstance(rows, list) and isinstance(rows[0], dict):
|
|
139
|
+
cols = list(rows[0].keys())
|
|
140
|
+
print(_table(cols, [[r.get(c, "") for c in cols] for r in rows]))
|
|
141
|
+
elif isinstance(rows, list):
|
|
142
|
+
print("\n".join(str(r) for r in rows))
|
|
143
|
+
else:
|
|
144
|
+
print(json.dumps(rows, indent=2))
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
148
|
+
p = argparse.ArgumentParser(prog="quest", description="Read-first client for UWaterloo Quest.")
|
|
149
|
+
p.add_argument("--version", action="version", version=f"quest {__version__}")
|
|
150
|
+
p.add_argument("--cookies-file", help=f"exported cookies JSON (default {DEFAULT_COOKIES})")
|
|
151
|
+
p.add_argument("--browser", help="read the live session from this browser instead (e.g. brave)")
|
|
152
|
+
p.add_argument("--base", default="https://quest.pecs.uwaterloo.ca", help="Quest base URL")
|
|
153
|
+
p.add_argument("--json", action="store_true", help="machine-readable output")
|
|
154
|
+
# --json also works after the subcommand; SUPPRESS keeps it from resetting the global one.
|
|
155
|
+
common = argparse.ArgumentParser(add_help=False)
|
|
156
|
+
common.add_argument("--json", action="store_true", default=argparse.SUPPRESS,
|
|
157
|
+
help="machine-readable output")
|
|
158
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
159
|
+
for name, help_text in [
|
|
160
|
+
("status", "one-screen summary (program, balance, holds, next enrollment)"),
|
|
161
|
+
("holds", "holds / service indicators"),
|
|
162
|
+
("account", "per-term registration status and balance"),
|
|
163
|
+
("schedule", "enrolled classes for the current term"),
|
|
164
|
+
("milestones", "milestones (requirements, scholarships)"),
|
|
165
|
+
("program", "plan, academic load, campus"),
|
|
166
|
+
("enrollment-dates", "when enrollment opens"),
|
|
167
|
+
("financial-aid", "financial aid years on record"),
|
|
168
|
+
("academic-requirements", "advisement report (experimental)"),
|
|
169
|
+
("terms", "term labels available for grades"),
|
|
170
|
+
]:
|
|
171
|
+
sub.add_parser(name, help=help_text, parents=[common])
|
|
172
|
+
g = sub.add_parser(
|
|
173
|
+
"grades", help="grades for a term (defaults to most recent)", parents=[common]
|
|
174
|
+
)
|
|
175
|
+
g.add_argument("--term", help="e.g. 'Winter 2026' (see `quest terms`)")
|
|
176
|
+
cs = sub.add_parser(
|
|
177
|
+
"classes", help="course availability / class search (no enrollment)", parents=[common]
|
|
178
|
+
)
|
|
179
|
+
cs.add_argument("subject", help="subject code, e.g. MATH")
|
|
180
|
+
cs.add_argument("catalog_nbr", nargs="?", help="course number, e.g. 135")
|
|
181
|
+
cs.add_argument("--term", help="e.g. 'Fall 2026' (defaults to the first offered)")
|
|
182
|
+
cs.add_argument("--open-only", action="store_true", help="only sections with seats open")
|
|
183
|
+
cs.add_argument("--seats", action="store_true", help="also fetch seat counts (slower)")
|
|
184
|
+
tr = sub.add_parser(
|
|
185
|
+
"transcript", help="download the unofficial transcript PDF", parents=[common]
|
|
186
|
+
)
|
|
187
|
+
tr.add_argument("-o", "--output", required=True, help="write the PDF to this path")
|
|
188
|
+
tr.add_argument("--type", default="Undergrad Unofficial", help="report type")
|
|
189
|
+
sub.add_parser("tui", help="launch the terminal UI (needs the 'tui' extra)")
|
|
190
|
+
return p
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def main(argv: list[str] | None = None) -> int:
|
|
194
|
+
args = build_parser().parse_args(argv)
|
|
195
|
+
name = args.cmd.replace("-", "_")
|
|
196
|
+
reader_name = {
|
|
197
|
+
"terms": "available_terms",
|
|
198
|
+
"classes": "course_availability",
|
|
199
|
+
"status": "dashboard",
|
|
200
|
+
"transcript": "transcript_pdf",
|
|
201
|
+
}.get(name, name)
|
|
202
|
+
if name == "tui":
|
|
203
|
+
try:
|
|
204
|
+
from .tui import run
|
|
205
|
+
except ImportError:
|
|
206
|
+
sys.exit("error: the TUI needs the 'tui' extra: pip install 'uwquest[tui]'")
|
|
207
|
+
run(_build_session(args))
|
|
208
|
+
return 0
|
|
209
|
+
reader = getattr(readers, reader_name)
|
|
210
|
+
try:
|
|
211
|
+
with _build_session(args) as session:
|
|
212
|
+
if name == "grades":
|
|
213
|
+
data = reader(session, args.term)
|
|
214
|
+
elif name == "classes":
|
|
215
|
+
data = reader(
|
|
216
|
+
session, args.subject, args.catalog_nbr,
|
|
217
|
+
term=args.term, open_only=args.open_only, with_seats=args.seats,
|
|
218
|
+
)
|
|
219
|
+
elif name == "transcript":
|
|
220
|
+
pdf = reader(session, args.type)
|
|
221
|
+
with open(args.output, "wb") as f:
|
|
222
|
+
f.write(pdf)
|
|
223
|
+
if args.json:
|
|
224
|
+
print(json.dumps({"path": args.output, "bytes": len(pdf)}))
|
|
225
|
+
else:
|
|
226
|
+
print(f"wrote {len(pdf)} bytes to {args.output}")
|
|
227
|
+
return 0
|
|
228
|
+
else:
|
|
229
|
+
data = reader(session)
|
|
230
|
+
except QuestError as e:
|
|
231
|
+
sys.exit(f"error: {e}")
|
|
232
|
+
_emit(data, args.json, name)
|
|
233
|
+
return 0
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
if __name__ == "__main__":
|
|
237
|
+
sys.exit(main())
|
quest/core/__init__.py
ADDED
quest/core/models.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (C) 2026 Amol Venkataraman
|
|
3
|
+
"""Plain, JSON-serialisable data the readers return."""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dataclasses import asdict, dataclass, field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class Hold:
|
|
11
|
+
reason: str
|
|
12
|
+
department: str = ""
|
|
13
|
+
detail: str = ""
|
|
14
|
+
|
|
15
|
+
def to_dict(self) -> dict:
|
|
16
|
+
return asdict(self)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class AccountTerm:
|
|
21
|
+
term: str
|
|
22
|
+
registered_status: str = ""
|
|
23
|
+
balance: str = ""
|
|
24
|
+
|
|
25
|
+
def to_dict(self) -> dict:
|
|
26
|
+
return asdict(self)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class Meeting:
|
|
31
|
+
class_nbr: str = ""
|
|
32
|
+
section: str = ""
|
|
33
|
+
component: str = ""
|
|
34
|
+
days_times: str = ""
|
|
35
|
+
room: str = ""
|
|
36
|
+
instructor: str = ""
|
|
37
|
+
start_end: str = ""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class ScheduleClass:
|
|
42
|
+
course: str = ""
|
|
43
|
+
status: str = ""
|
|
44
|
+
units: str = ""
|
|
45
|
+
grading: str = ""
|
|
46
|
+
grade: str = ""
|
|
47
|
+
meetings: list[Meeting] = field(default_factory=list)
|
|
48
|
+
|
|
49
|
+
def to_dict(self) -> dict:
|
|
50
|
+
return asdict(self)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class CourseGrade:
|
|
55
|
+
course: str = ""
|
|
56
|
+
description: str = ""
|
|
57
|
+
units: str = ""
|
|
58
|
+
grading: str = ""
|
|
59
|
+
grade: str = ""
|
|
60
|
+
|
|
61
|
+
def to_dict(self) -> dict:
|
|
62
|
+
return asdict(self)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class Milestone:
|
|
67
|
+
milestone: str = ""
|
|
68
|
+
level: str = ""
|
|
69
|
+
status: str = ""
|
|
70
|
+
program: str = ""
|
|
71
|
+
|
|
72
|
+
def to_dict(self) -> dict:
|
|
73
|
+
return asdict(self)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass
|
|
77
|
+
class Program:
|
|
78
|
+
plan: str = ""
|
|
79
|
+
academic_load: str = ""
|
|
80
|
+
campus: str = ""
|
|
81
|
+
|
|
82
|
+
def to_dict(self) -> dict:
|
|
83
|
+
return asdict(self)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass
|
|
87
|
+
class ClassSection:
|
|
88
|
+
class_nbr: str = ""
|
|
89
|
+
section: str = ""
|
|
90
|
+
status: str = ""
|
|
91
|
+
days_time: str = ""
|
|
92
|
+
room: str = ""
|
|
93
|
+
instructor: str = ""
|
|
94
|
+
dates: str = ""
|
|
95
|
+
campus: str = ""
|
|
96
|
+
# populated only when seat counts are requested (a per-section drill):
|
|
97
|
+
enrollment_cap: str = ""
|
|
98
|
+
enrollment_total: str = ""
|
|
99
|
+
available_seats: str = ""
|
|
100
|
+
waitlist_total: str = ""
|
|
101
|
+
|
|
102
|
+
def to_dict(self) -> dict:
|
|
103
|
+
return asdict(self)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class Row:
|
|
108
|
+
"""A generic parsed grid row, when no typed model applies yet."""
|
|
109
|
+
|
|
110
|
+
cells: dict[str, str] = field(default_factory=dict)
|
|
111
|
+
|
|
112
|
+
def to_dict(self) -> dict:
|
|
113
|
+
return dict(self.cells)
|
quest/core/parsers.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (C) 2026 Amol Venkataraman
|
|
3
|
+
"""HTML parsing helpers for PeopleSoft classic grids and pages.
|
|
4
|
+
|
|
5
|
+
PeopleSoft renders data as <table> grids with header cells carrying a
|
|
6
|
+
`PSLEVEL*GRIDCOLUMNHDR` class and body cells carrying `PSLEVEL*GRID*ROW`. These
|
|
7
|
+
helpers pull clean header/row data out without depending on exact field ids.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
from selectolax.lexbor import LexborHTMLParser as HTMLParser
|
|
14
|
+
|
|
15
|
+
_WS = re.compile(r"\s+")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _clean(text: str) -> str:
|
|
19
|
+
return _WS.sub(" ", text.replace("\xa0", " ")).strip()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def find_message(html: str, patterns: list[str]) -> str | None:
|
|
23
|
+
"""Return the first matching status/empty-state message, if present."""
|
|
24
|
+
for p in patterns:
|
|
25
|
+
m = re.search(p, html, re.I)
|
|
26
|
+
if m:
|
|
27
|
+
return _clean(m.group(0))
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _is_grid(table) -> bool:
|
|
32
|
+
cls = table.attributes.get("class") or ""
|
|
33
|
+
if "GRID" in cls.upper() or "ps_grid" in cls:
|
|
34
|
+
return True
|
|
35
|
+
# a table that contains PeopleSoft grid header cells
|
|
36
|
+
return table.css_first("th.PSLEVEL1GRIDCOLUMNHDR, th[class*=GRIDCOLUMNHDR]") is not None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def grids(html: str) -> list:
|
|
40
|
+
"""All PeopleSoft data-grid tables on the page, in document order."""
|
|
41
|
+
tree = HTMLParser(html)
|
|
42
|
+
return [t for t in tree.css("table") if _is_grid(t)]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def parse_grid(table) -> list[dict]:
|
|
46
|
+
"""Turn one grid <table> node into a list of {header: value} rows."""
|
|
47
|
+
headers: list[str] = []
|
|
48
|
+
for th in table.css("th"):
|
|
49
|
+
headers.append(_clean(th.text()))
|
|
50
|
+
rows: list[dict] = []
|
|
51
|
+
for tr in table.css("tr"):
|
|
52
|
+
cells = tr.css("td")
|
|
53
|
+
if not cells:
|
|
54
|
+
continue
|
|
55
|
+
values = [_clean(td.text()) for td in cells]
|
|
56
|
+
if not any(values):
|
|
57
|
+
continue
|
|
58
|
+
if headers and len(headers) == len(values):
|
|
59
|
+
rows.append(dict(zip(headers, values, strict=False)))
|
|
60
|
+
else:
|
|
61
|
+
rows.append({str(i): v for i, v in enumerate(values)})
|
|
62
|
+
return rows
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def first_grid_rows(html: str, min_cols: int = 2) -> list[dict]:
|
|
66
|
+
"""Rows of the first grid with at least `min_cols` header columns."""
|
|
67
|
+
for table in grids(html):
|
|
68
|
+
rows = parse_grid(table)
|
|
69
|
+
if rows and len(rows[0]) >= min_cols:
|
|
70
|
+
return rows
|
|
71
|
+
return []
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def page_title(html: str) -> str:
|
|
75
|
+
m = re.search(r"<title>(.*?)</title>", html, re.I | re.S)
|
|
76
|
+
return _clean(m.group(1)) if m else ""
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
_TERM = re.compile(r"id='TERM_CAR\$(\d+)'[^>]*>([^<]+)<")
|
|
80
|
+
_GO = re.compile(r"'([A-Z0-9_]*(?:SSR_PB_GO|PB_GO)[A-Z0-9_]*)'")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def parse_terms(html: str) -> list[tuple[int, str]]:
|
|
84
|
+
"""Term picker rows as (row index, label), e.g. (0, 'Fall 2026')."""
|
|
85
|
+
return [(int(i), _clean(label)) for i, label in _TERM.findall(html)]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def find_go_action(html: str) -> str | None:
|
|
89
|
+
"""The 'continue' / GO ICAction id on a term-picker page, if present."""
|
|
90
|
+
m = _GO.search(html)
|
|
91
|
+
return m.group(1) if m else None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _id_text_map(source) -> dict[str, str]:
|
|
95
|
+
tree = source if hasattr(source, "css") else HTMLParser(source)
|
|
96
|
+
out: dict[str, str] = {}
|
|
97
|
+
for el in tree.css("span, a, div"):
|
|
98
|
+
i = el.attributes.get("id", "")
|
|
99
|
+
if "$" in i and i not in out:
|
|
100
|
+
text = _clean(el.text())
|
|
101
|
+
if text:
|
|
102
|
+
out[i] = text
|
|
103
|
+
return out
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def field_value(source, base: str, index: int = 0) -> str:
|
|
107
|
+
"""Text of the value cell whose id is ``base$<index>``."""
|
|
108
|
+
return _id_text_map(source).get(f"{base}${index}", "")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def text_by_id(source, element_id: str) -> str:
|
|
112
|
+
"""Text of the element with this exact id (for non-indexed detail fields)."""
|
|
113
|
+
tree = source if hasattr(source, "css") else HTMLParser(source)
|
|
114
|
+
el = tree.css_first(f"[id='{element_id}']")
|
|
115
|
+
return _clean(el.text()) if el else ""
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def grid_by_fields(source, columns: dict[str, str]) -> list[dict]:
|
|
119
|
+
"""Extract a PeopleSoft nested grid by field id.
|
|
120
|
+
|
|
121
|
+
`columns` maps output names to field-id bases (e.g. {"grade": "CRSE_GRADE_OFF"}).
|
|
122
|
+
Each grid row shares an index suffix (`BASE$<row>`); returns one dict per row.
|
|
123
|
+
"""
|
|
124
|
+
idmap = _id_text_map(source)
|
|
125
|
+
cols: dict[str, dict[int, str]] = {}
|
|
126
|
+
for name, base in columns.items():
|
|
127
|
+
prefix = base + "$"
|
|
128
|
+
d: dict[int, str] = {}
|
|
129
|
+
for key, val in idmap.items():
|
|
130
|
+
if key.startswith(prefix):
|
|
131
|
+
suf = key[len(prefix):]
|
|
132
|
+
if suf.isdigit():
|
|
133
|
+
d[int(suf)] = val
|
|
134
|
+
cols[name] = d
|
|
135
|
+
indices = sorted({i for d in cols.values() for i in d})
|
|
136
|
+
return [{name: cols[name].get(i, "") for name in columns} for i in indices]
|