cass.db¶
SQLite storage: schema and domain models, CRUD, enriched queries, pending-change sync, table catalog metadata.
cass.db ¶
Database layer — schema, CRUD, introspection, and enriched queries.
Import everything from here::
from cass.db import get_db, CanvasStudent, save_canvas_students
from cass import db # also works
TableCapability
dataclass
¶
TableCapability(
editable: bool = False,
pushable: bool = False,
pull_guarded: bool = False,
viewer_rank: int = 999,
editable_columns: frozenset[str] | None = None,
)
Shared table behavior metadata for CLI and viewer workflows.
QueryResult ¶
Thin wrapper around a sqlite3 cursor for Rich table rendering.
CanvasAssignment ¶
Bases: Struct
A Canvas LMS assignment.
from_api
classmethod
¶
Convert a Canvas API assignment response to a domain model.
CanvasGrade ¶
Bases: Struct
A grade ready for Canvas API push.
CanvasStudent ¶
Bases: Struct
A Canvas LMS student record.
from_api
classmethod
¶
Convert a Canvas API student response to a domain model.
CanvasSubmission ¶
Bases: Struct
A Canvas LMS submission record.
get_table_capability ¶
Return shared behavior metadata for a table.
connect_db ¶
Open a lightweight connection to an existing database.
Unlike open_db, this skips schema init and reconciliation — use it
when you know the DB already exists (e.g. in a background thread while
the viewer's main connection is alive).
editable_canvas_tables ¶
Return the current editable Canvas-managed working tables.
get_assignment_groups ¶
Return distinct non-empty assignment groups, sorted alphabetically.
get_db ¶
Return the shared Database, creating it on first call.
get_meta ¶
Retrieve a value from the meta table, or None if not found.
load_canvas_assignment_ids ¶
Return every Canvas assignment id, ascending.
load_canvas_grades ¶
Load Canvas grades.
load_canvas_student_ids ¶
Return every canvas_id in the Canvas roster.
open_db ¶
Open a database for the resolved project root and ensure schema exists.
save_canvas_assignments ¶
Save Canvas assignments from domain models.
save_canvas_students ¶
Upsert Canvas students into the source table.
save_canvas_submissions ¶
Upsert Canvas submissions.
upsert_canvas_grade ¶
upsert_canvas_grade(
sdb: Database,
canvas_user_id: int,
canvas_assignment_id: int,
posted_grade: str,
) -> str
Insert or update a single canvas grade, returning the previous value.
Returns:
| Type | Description |
|---|---|
str
|
Previous posted_grade (empty string if row didn't exist). |
get_column_names ¶
Return column names for a table.
get_primary_keys ¶
Return primary key column names for a table.
get_tables ¶
Return list of non-excluded tables with their type.
is_editable ¶
Check if a table is editable (has PKs, is a real table, not read-only).
revert_changes ¶
revert_changes(
sdb: Database,
pending: dict[
str, dict[str, dict[str, dict[str, object]]]
],
) -> int
Revert all pending changes in the DB by restoring baseline values.
Returns:
| Type | Description |
|---|---|
int
|
Number of field changes reverted. |
update_cell ¶
update_cell(
sdb: Database,
table: str,
pk: dict[str, object],
column: str,
value: object,
) -> dict[str, object]
Update a single cell in a table.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with |
build_submissions_query ¶
Build the shared submissions dataset query.
get_enriched_rows ¶
Return all rows from a table using enriched query if available.
run_query ¶
Execute arbitrary SQL and return results as list of dicts.
sql ¶
Execute SQL and return a QueryResult with .columns and .fetchall().
canvas_apply ¶
Push pending changes to Canvas and clear synced rows on success.
canvas_preview ¶
Compare pending changes against live Canvas state.
get_pending_changes ¶
get_pending_changes(
sdb: Database | None = None,
) -> dict[str, dict[str, dict[str, dict[str, object]]]]
Compute pending changes by diffing main tables against synced shadows.
Returns the same PendingChanges structure used by the viewer:
{table: {pk_key: {column: {"baseline": ..., "current": ...}}}}.
get_pull_blocking_tables ¶
Return Canvas-managed working tables with pending local edits.
mark_synced_assignments ¶
Update synced shadow for specific assignments after push.
mark_synced_grades ¶
Update synced shadow for specific grade rows after push.
preview_assignments ¶
preview_assignments(
conn: Database,
table_changes: _TableChanges,
client: CanvasClient,
) -> list[dict[str, object]]
Preview pending assignment changes against live Canvas state.
preview_grades ¶
preview_grades(
conn: Database,
table_changes: _TableChanges,
client: CanvasClient,
) -> list[dict[str, object]]
Preview pending grade changes against live Canvas submissions.
pull_block_reason ¶
Describe why pull is blocked, or return None when pull is allowed.
snapshot_canvas_synced ¶
Copy current canvas tables into synced shadow tables.
Called after a successful pull to record what Canvas has.
build_canvas_gradebook_matrix ¶
Build a gradebook matrix for CLI/report output.
load_canvas_gradebook_data ¶
Load shared Canvas gradebook data for both CLI and viewer.
catalog ¶
Shared catalog metadata for user-facing cass data workflows.
TableCapability
dataclass
¶
TableCapability(
editable: bool = False,
pushable: bool = False,
pull_guarded: bool = False,
viewer_rank: int = 999,
editable_columns: frozenset[str] | None = None,
)
Shared table behavior metadata for CLI and viewer workflows.
get_table_capability ¶
Return shared behavior metadata for a table.
core ¶
SQLite database for cass — connection, schema, meta, and CRUD.
Schema v16: Canvas source tables (canvas_*) plus Canvas grade tables for manual push workflows. Synced shadow tables (_canvas_assignments_synced, _canvas_grades_synced) provide persistent change tracking between local edits and Canvas state.
connect_db ¶
Open a lightweight connection to an existing database.
Unlike open_db, this skips schema init and reconciliation — use it
when you know the DB already exists (e.g. in a background thread while
the viewer's main connection is alive).
open_db ¶
Open a database for the resolved project root and ensure schema exists.
get_db ¶
Return the shared Database, creating it on first call.
editable_canvas_tables ¶
Return the current editable Canvas-managed working tables.
get_meta ¶
Retrieve a value from the meta table, or None if not found.
save_canvas_students ¶
Upsert Canvas students into the source table.
load_canvas_student_ids ¶
Return every canvas_id in the Canvas roster.
save_canvas_assignments ¶
Save Canvas assignments from domain models.
load_canvas_assignment_ids ¶
Return every Canvas assignment id, ascending.
save_canvas_submissions ¶
Upsert Canvas submissions.
upsert_canvas_grade ¶
upsert_canvas_grade(
sdb: Database,
canvas_user_id: int,
canvas_assignment_id: int,
posted_grade: str,
) -> str
Insert or update a single canvas grade, returning the previous value.
Returns:
| Type | Description |
|---|---|
str
|
Previous posted_grade (empty string if row didn't exist). |
get_assignment_groups ¶
Return distinct non-empty assignment groups, sorted alphabetically.
load_canvas_grades ¶
Load Canvas grades.
ibis_adapter ¶
Generic DB adapter via ibis-framework — DuckDB and SQLite backends.
connect_file ¶
Connect to a DuckDB or SQLite file via ibis.
Raises:
| Type | Description |
|---|---|
SystemExit
|
If the file does not exist or has an unsupported suffix. |
list_tables ¶
List all tables in the connected database.
get_schema ¶
Return column names and type strings for a table.
get_primary_keys ¶
Detect primary key columns via PRAGMA table_info.
Works for both DuckDB (pk flag is bool) and SQLite (pk flag is int > 0). Falls back to empty list if detection fails.
get_rows ¶
Fetch rows from a table as sanitized list of dicts.
get_row_count ¶
Return the number of rows in a table.
update_cell ¶
update_cell(
con: BaseBackend,
table: str,
pk_col: str,
pk_val: Any,
column: str,
value: Any,
) -> dict[str, Any]
Update a single cell value and return status dict.
get_categorical_columns ¶
get_categorical_columns(
con: BaseBackend, table: str, *, max_distinct: int = 10
) -> dict[str, list[str]]
Detect string columns with few distinct values and return their options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
BaseBackend
|
ibis connection. |
required |
table
|
str
|
Table name. |
required |
max_distinct
|
int
|
Maximum distinct values to qualify as categorical. |
10
|
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Mapping of column name to sorted list of distinct values. |
run_sql ¶
Execute raw SQL and return (column_names, list_of_row_dicts).
introspection ¶
Viewer DB introspection — table metadata, cell editing, and revert.
Functions used by the viewer (and tests) to inspect table structure, update individual cells, and revert pending changes.
get_tables ¶
Return list of non-excluded tables with their type.
get_primary_keys ¶
Return primary key column names for a table.
get_column_names ¶
Return column names for a table.
is_editable ¶
Check if a table is editable (has PKs, is a real table, not read-only).
update_cell ¶
update_cell(
sdb: Database,
table: str,
pk: dict[str, object],
column: str,
value: object,
) -> dict[str, object]
Update a single cell in a table.
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with |
revert_changes ¶
revert_changes(
sdb: Database,
pending: dict[
str, dict[str, dict[str, dict[str, object]]]
],
) -> int
Revert all pending changes in the DB by restoring baseline values.
Returns:
| Type | Description |
|---|---|
int
|
Number of field changes reverted. |
queries ¶
Enriched queries and shared query dataset helpers.
QueryResult ¶
Thin wrapper around a sqlite3 cursor for Rich table rendering.
build_submissions_query ¶
Build the shared submissions dataset query.
get_enriched_rows ¶
Return all rows from a table using enriched query if available.
sql ¶
Execute SQL and return a QueryResult with .columns and .fetchall().
run_query ¶
Execute arbitrary SQL and return results as list of dicts.
schema ¶
Domain models for cass — Canvas students, assignments, submissions, grades.
CanvasStudent ¶
Bases: Struct
A Canvas LMS student record.
from_api
classmethod
¶
Convert a Canvas API student response to a domain model.
CanvasAssignment ¶
Bases: Struct
A Canvas LMS assignment.
from_api
classmethod
¶
Convert a Canvas API assignment response to a domain model.
CanvasSubmission ¶
Bases: Struct
A Canvas LMS submission record.
CanvasGrade ¶
Bases: Struct
A grade ready for Canvas API push.
sync ¶
Canvas sync state and shared preview/apply workflows.
snapshot_canvas_synced ¶
Copy current canvas tables into synced shadow tables.
Called after a successful pull to record what Canvas has.
mark_synced_assignments ¶
Update synced shadow for specific assignments after push.
mark_synced_grades ¶
Update synced shadow for specific grade rows after push.
get_pending_changes ¶
get_pending_changes(
sdb: Database | None = None,
) -> dict[str, dict[str, dict[str, dict[str, object]]]]
Compute pending changes by diffing main tables against synced shadows.
Returns the same PendingChanges structure used by the viewer:
{table: {pk_key: {column: {"baseline": ..., "current": ...}}}}.
get_pull_blocking_tables ¶
Return Canvas-managed working tables with pending local edits.
pull_block_reason ¶
Describe why pull is blocked, or return None when pull is allowed.
preview_assignments ¶
preview_assignments(
conn: Database,
table_changes: _TableChanges,
client: CanvasClient,
) -> list[dict[str, object]]
Preview pending assignment changes against live Canvas state.
preview_grades ¶
preview_grades(
conn: Database,
table_changes: _TableChanges,
client: CanvasClient,
) -> list[dict[str, object]]
Preview pending grade changes against live Canvas submissions.
canvas_preview ¶
Compare pending changes against live Canvas state.
canvas_apply ¶
Push pending changes to Canvas and clear synced rows on success.