File size: 2,162 Bytes
526950e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Apply config/course.yaml classification rules to file paths."""
from __future__ import annotations

import fnmatch
import re
from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True)
class Classification:
    kind: str
    topic: str
    visibility: str
    student_access: bool
    release_status: str
    order: Optional[int] = None
    lecture_date: Optional[str] = None


def classify_path(path: str, rules: dict) -> Optional[Classification]:
    """Classify a repo-relative POSIX path against the loaded course.yaml rules.

    Returns None if no rule matches (caller should record as unmatched diagnostic).
    """
    for rule in rules["classification_rules"]:
        if not fnmatch.fnmatchcase(path, rule["match"]):
            continue

        topic_raw = _extract(rule.get("topic_extract"), path)
        if topic_raw is None:
            topic = "_unknown"
        else:
            topic = rules.get("topic_name_overrides", {}).get(
                topic_raw, _normalize_topic(topic_raw)
            )

        order = _extract_int(rule.get("order_extract"), path)
        lecture_date = _extract(rule.get("lecture_date_extract"), path)

        return Classification(
            kind=rule["kind"],
            topic=topic,
            visibility=rule["visibility"],
            student_access=rule["student_access"],
            release_status=rule["release_status"],
            order=order,
            lecture_date=lecture_date,
        )
    return None


def _extract(pattern: Optional[str], path: str) -> Optional[str]:
    if not pattern:
        return None
    m = re.search(pattern, path)
    return m.group(1) if m else None


def _extract_int(pattern: Optional[str], path: str) -> Optional[int]:
    raw = _extract(pattern, path)
    return int(raw) if raw is not None else None


def _normalize_topic(raw: str) -> str:
    """Single-cap-boundary Camel/Pascal → snake_case lowercase.

    Consecutive capitals (e.g., 'URL', 'XMLParser') are NOT handled —
    use ``topic_name_overrides`` in course.yaml to map such inputs.
    """
    s = re.sub(r"(?<!^)(?=[A-Z])", "_", raw).lower()
    return s