-
Notifications
You must be signed in to change notification settings - Fork 3
init()
-Entrypoint und Environment
#63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# This file is part of the QuestionPy SDK. (https://questionpy.org) | ||
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from .question_type import ExampleQuestionType | ||
|
||
|
||
def init() -> ExampleQuestionType: | ||
return ExampleQuestionType() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
# This file is part of the QuestionPy SDK. (https://questionpy.org) | ||
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from importlib.resources import files | ||
|
||
from questionpy_common.models import QuestionModel, ScoringMethod, AttemptModel, AttemptUi | ||
|
||
from questionpy import QuestionType, Attempt, Question, BaseQuestionState, BaseAttemptState | ||
from questionpy.form import * | ||
from questionpy.form import FormModel, text_input, OptionEnum, option, select, group, static_text, is_checked,\ | ||
checkbox, radio_group, hidden, repeat, is_not_checked | ||
|
||
|
||
class NameGroup(FormModel): | ||
|
@@ -39,19 +31,3 @@ class MyModel(FormModel): | |
|
||
has_name = checkbox(None, "I have a name.") | ||
name_group = group("Name", NameGroup, disable_if=[is_not_checked("has_name")]) | ||
|
||
|
||
class ExampleAttempt(Attempt["ExampleQuestion", BaseAttemptState]): | ||
def export(self) -> AttemptModel: | ||
return AttemptModel(variant=1, ui=AttemptUi( | ||
content=(files(__package__) / "multiple-choice.xhtml").read_text() | ||
)) | ||
|
||
|
||
class ExampleQuestion(Question[BaseQuestionState[MyModel], ExampleAttempt]): | ||
def export(self) -> QuestionModel: | ||
return QuestionModel(scoring_method=ScoringMethod.AUTOMATICALLY_SCORABLE) | ||
|
||
|
||
class ExampleQuestionType(QuestionType[MyModel, ExampleQuestion]): | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from importlib.resources import files | ||
|
||
from questionpy_common.models import AttemptModel, AttemptUi, QuestionModel, ScoringMethod | ||
|
||
from questionpy import Attempt, BaseAttemptState, Question, BaseQuestionState, QuestionType | ||
from .form import MyModel | ||
|
||
|
||
class ExampleAttempt(Attempt["ExampleQuestion", BaseAttemptState]): | ||
def export(self) -> AttemptModel: | ||
return AttemptModel(variant=1, ui=AttemptUi( | ||
content=(files(__package__) / "multiple-choice.xhtml").read_text() | ||
)) | ||
|
||
|
||
class ExampleQuestion(Question[BaseQuestionState[MyModel], ExampleAttempt]): | ||
def export(self) -> QuestionModel: | ||
return QuestionModel(scoring_method=ScoringMethod.AUTOMATICALLY_SCORABLE) | ||
|
||
|
||
class ExampleQuestionType(QuestionType[MyModel, ExampleQuestion]): | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,4 @@ author: Maximilian Haye <[email protected]> | |
name: | ||
de: Beispiel | ||
en: Example | ||
entrypoint: main | ||
languages: [de, en] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from questionpy_common.manifest import Manifest, PackageType # noqa | ||
from questionpy_common.environment import RequestUser, WorkerResourceLimits, Package, OnRequestCallback, Environment, \ | ||
PackageInitFunction, get_qpy_environment, NoEnvironmentError # noqa | ||
from questionpy._qtype import QuestionType, Question, Attempt, BaseQuestionState, BaseAttemptState # noqa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,13 @@ | |
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from abc import ABC | ||
from typing import Optional, Type, Generic, TypeVar, ClassVar, get_args, get_origin, Literal, Union, cast | ||
from typing import Optional, Type, Generic, TypeVar, get_args, get_origin, Literal, Union, cast | ||
|
||
from pydantic import BaseModel, ValidationError | ||
from questionpy_common.qtype import OptionsFormDefinition, BaseQuestionType, BaseQuestion, OptionsFormValidationError, \ | ||
BaseAttempt | ||
|
||
from questionpy import get_qpy_environment | ||
from questionpy.form import FormModel | ||
|
||
_T = TypeVar("_T") | ||
|
@@ -139,9 +140,6 @@ class QuestionType(BaseQuestionType, Generic[_F, _Q]): | |
options_class: Type[FormModel] = FormModel | ||
question_class: Type["Question"] | ||
|
||
# TODO: questionpy-server#67: Replace with global init() function. | ||
implementation: ClassVar[Optional[Type["QuestionType"]]] = None | ||
|
||
def __init__(self, options_class: Optional[Type[_F]] = None, question_class: Optional[Type[_Q]] = None) -> None: | ||
"""Initializes a new question. | ||
|
||
|
@@ -163,8 +161,6 @@ def __init_subclass__(cls, **kwargs: object) -> None: | |
raise TypeError( | ||
f"{cls.__name__} must have the same FormModel as {cls.question_class.state_class.__name__}.") | ||
|
||
QuestionType.implementation = cls | ||
|
||
def get_options_form(self, question_state: Optional[str]) -> tuple[OptionsFormDefinition, dict[str, object]]: | ||
if question_state: | ||
question = self.create_question_from_state(question_state) | ||
|
@@ -186,10 +182,10 @@ def create_question_from_options(self, old_state: Optional[str], form_data: dict | |
# TBD: Should we also update package_name and package_version here? Or check that they match? | ||
state.options = parsed_form_data | ||
else: | ||
env = get_qpy_environment() | ||
state = self.question_class.state_class( | ||
# TODO: Replace these placeholders with values from the environment. | ||
package_name="TODO", | ||
package_version="1.2.3", | ||
package_name=f"{env.main_package.manifest.namespace}.{env.main_package.manifest.short_name}", | ||
package_version=env.main_package.manifest.version, | ||
options=parsed_form_data, | ||
) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,40 @@ | |
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
import json | ||
from typing import Optional | ||
from collections.abc import Generator | ||
from types import SimpleNamespace | ||
from typing import Optional, cast | ||
|
||
import pytest | ||
from questionpy_common.environment import set_qpy_environment | ||
from questionpy_common.models import QuestionModel, ScoringMethod, AttemptModel, AttemptUi | ||
from questionpy_server.worker.runtime.manager import EnvironmentImpl | ||
from questionpy_server.worker.runtime.package import QPyMainPackage | ||
|
||
from questionpy import QuestionType, Question, BaseQuestionState, Attempt, BaseAttemptState | ||
from questionpy import QuestionType, Question, BaseQuestionState, Attempt, BaseAttemptState, Environment, RequestUser | ||
from questionpy.form import FormModel, text_input | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def environment() -> Generator[Environment, None, None]: | ||
env = EnvironmentImpl( | ||
type="test", | ||
limits=None, | ||
request_user=RequestUser(["en"]), | ||
main_package=cast(QPyMainPackage, SimpleNamespace(manifest=SimpleNamespace( | ||
namespace="test_ns", short_name="test_package", | ||
version="1.2.3" | ||
))), | ||
packages={}, | ||
_on_request_callbacks=[] | ||
) | ||
set_qpy_environment(env) | ||
try: | ||
yield env | ||
finally: | ||
set_qpy_environment(None) | ||
|
||
|
||
class SomeModel(FormModel): | ||
input: Optional[str] = text_input("Some Label") | ||
|
||
|
@@ -112,7 +137,7 @@ def export(self) -> QuestionModel: | |
|
||
|
||
QUESTION_STATE_DICT = { | ||
"package_name": "TODO", | ||
"package_name": "test_ns.test_package", | ||
"package_version": "1.2.3", | ||
"options": { | ||
"input": "something" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.