|
2 | 2 | import json
|
3 | 3 | import logging
|
4 | 4 | import pathlib
|
| 5 | +import re |
5 | 6 | import time
|
6 |
| - |
7 | 7 | from typing import Dict, List, Tuple
|
8 | 8 |
|
9 | 9 | import requests
|
@@ -238,8 +238,64 @@ def assignment_code(student_files:Tuple[pathlib.Path]) -> str:
|
238 | 238 |
|
239 | 239 |
|
240 | 240 | @functools.lru_cache
|
241 |
| -def assignment_instruction(readme_file:pathlib.Path) -> str: |
242 |
| - return readme_file.read_text() |
| 241 | +def assignment_instruction( |
| 242 | + readme_file: pathlib.Path, |
| 243 | + common_content_start_marker: str = r"``From here is common to all assignments\.``", |
| 244 | + common_content_end_marker: str = r"``Until here is common to all assignments\.``", |
| 245 | +) -> str: |
| 246 | + """Extracts assignment-specific instructions from a README.md file. |
| 247 | +
|
| 248 | + This function reads a README.md file and removes content marked as common |
| 249 | + to all assignments, returning only the assignment-specific instructions. |
| 250 | +
|
| 251 | + Args: |
| 252 | + readme_file: Path to the README.md file. |
| 253 | + common_content_start_marker: The marker indicating the start of common content. |
| 254 | + common_content_end_marker: The marker indicating the end of common content. |
| 255 | +
|
| 256 | + Returns: |
| 257 | + A string containing the assignment-specific instructions. |
| 258 | + """ |
| 259 | + |
| 260 | + return exclude_common_contents( |
| 261 | + readme_file.read_text(), |
| 262 | + common_content_start_marker, |
| 263 | + common_content_end_marker, |
| 264 | + ) # Single exit point |
| 265 | + |
| 266 | + |
| 267 | +def exclude_common_contents( |
| 268 | + readme_content:str, |
| 269 | + common_content_start_marker: str = r"``From here is common to all assignments.``", |
| 270 | + common_content_end_marker: str = r"``Until here is common to all assignments.``", |
| 271 | +) -> str: |
| 272 | + """Removes common content from a string. |
| 273 | +
|
| 274 | + This function takes a string and removes the content between the specified |
| 275 | + start and end markers. |
| 276 | +
|
| 277 | + Args: |
| 278 | + readme_content: The input string containing the README content. |
| 279 | + common_content_start_marker: The marker indicating the start of common content. |
| 280 | + common_content_end_marker: The marker indicating the end of common content. |
| 281 | +
|
| 282 | + Returns: |
| 283 | + A string with the common content removed. |
| 284 | + """ |
| 285 | + # Include the markers in the pattern itself |
| 286 | + pattern = rf"({common_content_start_marker}\s*.*?\s*{common_content_end_marker})" |
| 287 | + found_list = re.findall(pattern, readme_content, re.DOTALL | re.IGNORECASE) |
| 288 | + |
| 289 | + instruction = str(readme_content) |
| 290 | + |
| 291 | + if not found_list: |
| 292 | + logging.warning(f"Common content markers '{common_content_start_marker}' and '{common_content_end_marker}' not found in README.md. Returning entire file.") |
| 293 | + else: |
| 294 | + for found in found_list: |
| 295 | + # Remove the common content |
| 296 | + instruction = instruction.replace(found, "") |
| 297 | + |
| 298 | + return instruction |
243 | 299 |
|
244 | 300 |
|
245 | 301 | @functools.lru_cache(maxsize=None)
|
|
0 commit comments