File size: 1,370 Bytes
5fe9036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Task auto-discovery and registry.

Any .py file in this directory that defines a module-level SCENARIO variable
(an IncidentScenario instance) will be automatically loaded and registered.

To add a new task:
  1. Create a new .py file in this directory (e.g., tasks/my_new_task.py)
  2. Define SCENARIO = IncidentScenario(task_id="my_new_task", ...)
  3. That's it — the task will be available via the API automatically.
"""

import importlib
import pkgutil
from pathlib import Path
from typing import Dict

from env.scenario import IncidentScenario


def _discover_scenarios() -> Dict[str, IncidentScenario]:
    """Scan all .py files in tasks/ and collect SCENARIO instances."""
    scenarios: Dict[str, IncidentScenario] = {}
    package_dir = Path(__file__).parent

    for finder, module_name, is_pkg in pkgutil.iter_modules([str(package_dir)]):
        if module_name.startswith("_"):
            continue
        try:
            module = importlib.import_module(f"tasks.{module_name}")
            scenario = getattr(module, "SCENARIO", None)
            if isinstance(scenario, IncidentScenario):
                scenarios[scenario.task_id] = scenario
        except Exception as e:
            print(f"Warning: failed to load task module 'tasks.{module_name}': {e}")

    return scenarios


SCENARIOS: Dict[str, IncidentScenario] = _discover_scenarios()