Skip to content

Commit

Permalink
rename from_yaml and remove expected_completion_time
Browse files Browse the repository at this point in the history
  • Loading branch information
jamagalhaes committed Jan 30, 2025
1 parent 3fbf097 commit 0c8cf00
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions canvas_sdk/questionnaires/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .utils import from_yaml
from .utils import from_yaml as questionnaire_from_yaml

__all__ = ("from_yaml",)
__all__ = ("questionnaire_from_yaml",)
4 changes: 2 additions & 2 deletions canvas_sdk/questionnaires/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from canvas_sdk.effects import Effect
from canvas_sdk.events import Event, EventRequest, EventType
from canvas_sdk.questionnaires import from_yaml
from canvas_sdk.questionnaires import questionnaire_from_yaml
from plugin_runner.plugin_runner import LOADED_PLUGINS
from settings import PLUGIN_DIRECTORY

Expand Down Expand Up @@ -56,4 +56,4 @@ def test_from_yaml_forbidden_questionnaire(

def test_from_yaml_non_plugin_caller() -> None:
"""Test that the from_yaml function returns None when called outside a plugin."""
assert from_yaml("questionnaires/example_questionnaire.yml") is None
assert questionnaire_from_yaml("questionnaires/example_questionnaire.yml") is None
16 changes: 15 additions & 1 deletion canvas_sdk/questionnaires/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import functools
import json
from pathlib import Path
from typing import Any, TypedDict

import yaml
from jsonschema import validate

from canvas_sdk.utils.plugins import plugin_only

Expand Down Expand Up @@ -43,7 +46,6 @@ class QuestionnaireConfig(TypedDict):
content: str
prologue: str
use_in_shx: bool
expected_completion_time: float
questions: list[Question]


Expand All @@ -63,6 +65,7 @@ def from_yaml(questionnaire_name: str, **kwargs: Any) -> QuestionnaireConfig | N
FileNotFoundError: If the questionnaire file does not exist within the plugin's directory
or if the resolved path is invalid.
PermissionError: If the resolved path is outside the plugin's directory.
ValidationError: If the questionnaire file does not conform to the JSON schema.
"""
plugin_dir = kwargs["plugin_dir"]
questionnaire_config_path = Path(plugin_dir / questionnaire_name.lstrip("/")).resolve()
Expand All @@ -73,5 +76,16 @@ def from_yaml(questionnaire_name: str, **kwargs: Any) -> QuestionnaireConfig | N
raise FileNotFoundError(f"Questionnaire {questionnaire_name} not found.")

questionnaire_config = yaml.load(questionnaire_config_path.read_text(), Loader=yaml.SafeLoader)
validate(questionnaire_config, json_schema())

return questionnaire_config


@functools.cache
def json_schema() -> dict[str, Any]:
"""Reads the JSON schema for a Questionnaire Config."""
schema = json.loads(
(Path(__file__).resolve().parent.parent.parent / "schemas/questionnaire.json").read_text()
)

return schema
3 changes: 1 addition & 2 deletions schemas/questionnaire.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"content": { "type": "string" },
"prologue": { "type": "string" },
"use_in_shx": { "type": "boolean" },
"expected_completion_time": { "type": "number" },
"questions": {
"type": "array",
"minItems": 1,
Expand Down Expand Up @@ -68,5 +67,5 @@
}
}
},
"required": ["name", "use_case_in_charting", "code_system", "code", "questions", "expected_completion_time"]
"required": ["name", "use_case_in_charting", "code_system", "code", "questions"]
}

0 comments on commit 0c8cf00

Please sign in to comment.