|
| 1 | +""" |
| 2 | +Helper functions for common check patterns. |
| 3 | +
|
| 4 | +Provides reusable utilities to reduce boilerplate in check definitions. |
| 5 | +
|
| 6 | +Based on check50 by CS50 (https://github.com/cs50/check50) |
| 7 | +Licensed under GPL-3.0 |
| 8 | +""" |
| 9 | + |
| 10 | +from functools import wraps |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | +from . import internal |
| 14 | +from .adapters import create_adapter |
| 15 | + |
| 16 | + |
| 17 | +def standard_compile_check(problem: Optional[str] = None): |
| 18 | + """ |
| 19 | + Create a standard compilation check function. |
| 20 | + |
| 21 | + This helper automatically handles language-specific compilation: |
| 22 | + - C: Compiles with lcs50=True (links cs50 library) |
| 23 | + - Java: Standard compilation |
| 24 | + - Python: Syntax validation (no-op currently) |
| 25 | + |
| 26 | + Args: |
| 27 | + problem: Problem name (e.g., "caesar", "hello") |
| 28 | + If None, will be inferred from check context |
| 29 | + |
| 30 | + Returns: |
| 31 | + A check function that performs compilation |
| 32 | + |
| 33 | + Usage: |
| 34 | + >>> from bootcs.check import check |
| 35 | + >>> from bootcs.check.helpers import standard_compile_check |
| 36 | + >>> |
| 37 | + >>> @check() |
| 38 | + >>> def exists(): |
| 39 | + >>> create_adapter("caesar").require_exists() |
| 40 | + >>> |
| 41 | + >>> # Option 1: Inline assignment |
| 42 | + >>> compiles = standard_compile_check("caesar") |
| 43 | + >>> |
| 44 | + >>> # Option 2: As decorator (more explicit) |
| 45 | + >>> @check(exists) |
| 46 | + >>> def compiles(): |
| 47 | + >>> return standard_compile_check("caesar")() |
| 48 | + |
| 49 | + Note: |
| 50 | + This is a factory function that returns the actual check function. |
| 51 | + It's designed to reduce the repetitive compilation boilerplate across checks. |
| 52 | + """ |
| 53 | + def compile_func(): |
| 54 | + """compiles (or validates syntax)""" |
| 55 | + prob = problem or internal.get_problem_name() |
| 56 | + lang = internal.get_current_language() |
| 57 | + adapter = create_adapter(problem=prob) |
| 58 | + |
| 59 | + if lang == 'c': |
| 60 | + adapter.compile(lcs50=True) |
| 61 | + else: |
| 62 | + adapter.compile() |
| 63 | + |
| 64 | + return compile_func |
| 65 | + |
| 66 | + |
| 67 | +def with_adapter(problem: str): |
| 68 | + """ |
| 69 | + Decorator that provides a pre-created adapter to check functions. |
| 70 | + |
| 71 | + Reduces boilerplate by creating the adapter once and passing it |
| 72 | + to the decorated function. |
| 73 | + |
| 74 | + Args: |
| 75 | + problem: Problem name for adapter creation |
| 76 | + |
| 77 | + Usage: |
| 78 | + >>> @check(compiles) |
| 79 | + >>> @with_adapter("caesar") |
| 80 | + >>> def encrypts_a_as_b(adapter): |
| 81 | + >>> adapter.run("1").stdin("a").stdout("b").exit(0) |
| 82 | + """ |
| 83 | + def decorator(func): |
| 84 | + @wraps(func) |
| 85 | + def wrapper(*args, **kwargs): |
| 86 | + adapter = create_adapter(problem=problem) |
| 87 | + return func(adapter, *args, **kwargs) |
| 88 | + return wrapper |
| 89 | + return decorator |
0 commit comments