[docs]defget_project_and_task_names()->tuple[str,str]:"""Retrieves ``project`` and ``task`` from script arguments. Raises: RuntimeError: If ``project`` or ``task`` arguments are missing. Returns: The ``project`` and ``task`` names. """has_project_arg,has_task_arg=False,Falseforarginsys.argv:ifarg.startswith("project="):has_project_arg=Trueproject_name=arg.split("=",maxsplit=1)[-1]ifarg.startswith("task="):has_task_arg=Truetask_name=arg.split("=",maxsplit=1)[-1]ifnothas_project_arg:error_msg=("Invalid script arguments. You must specify the ""``project`` argument in the form ``project=foo``.")raiseRuntimeError(error_msg)ifnothas_task_arg:error_msg=("Invalid script arguments. You must specify the ""``task`` argument in the form ``task=bar``.")raiseRuntimeError(error_msg)returnproject_name,task_name
[docs]defget_absolute_project_path()->str:""". Returns: The absolute path to the ``project`` module. """project_name,_=get_project_and_task_names()returnf"{os.environ['CNEUROMAX_PATH']}/cneuromax/projects/{project_name}/"
[docs]defget_project_module()->ModuleType:"""Retrieves the ``project`` module. Raises: RuntimeError: If the ``project`` argument is invalid or the ``project`` module does not exist. Returns: The ``project`` module. """project_name,_=get_project_and_task_names()try:project_module=import_module(name=f"cneuromax.projects.{project_name}",)exceptModuleNotFoundErroraserror:ifstr(error).startswith("No module named 'cneuromax."):error_msg=("Invalid project name. Make sure that "f"`cneuromax/projects/{project_name}/__init__.py` exists.")raiseRuntimeError(error_msg)fromerrorraisereturnproject_module
[docs]defget_task_runner_class()->Any:# noqa: ANN401""". Raises: RuntimeError: If the ``project`` module does not define a :mod:`~cneuromax.runner.BaseTaskRunner` class. Returns: The :mod:`~cneuromax.runner.BaseTaskRunner` class. """project_module=get_project_module()try:task_runner=project_module.TaskRunnerexceptAttributeErroraserror:error_msg=("Invalid project module. The ``project`` module must ""define a ``TaskRunner`` class.")raiseRuntimeError(error_msg)fromerrorreturntask_runner