Skip to content

Commit 2dc3eb9

Browse files
author
Andrei Neagu
committed
using env vars in discriminators
1 parent 844b0f4 commit 2dc3eb9

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

packages/models-library/src/models_library/osparc_variable_identifier.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1+
import os
12
from copy import deepcopy
2-
from typing import Any, TypeVar
3+
from typing import Annotated, Any, Final, TypeVar
34

45
from common_library.errors_classes import OsparcErrorMixin
56
from models_library.basic_types import ConstrainedStr
6-
7-
from pydantic import BaseModel
7+
from pydantic import BaseModel, Discriminator, PositiveInt, Tag
88

99
from .utils.string_substitution import OSPARC_IDENTIFIER_PREFIX
1010

1111
T = TypeVar("T")
1212

1313

14-
class OsparcVariableIdentifier(ConstrainedStr):
14+
class _BaseOsparcVariableIdentifier(ConstrainedStr):
1515
# NOTE: To allow parametrized value, set the type to Union[OsparcVariableIdentifier, ...]
1616
# NOTE: When dealing with str types, to avoid unexpected behavior, the following
1717
# order is suggested `OsparcVariableIdentifier | str`
18-
# NOTE: in below regex `{`` and `}` are respectively escaped with `{{` and `}}`
19-
pattern = (
20-
rf"^\${{1,2}}(?:\{{)?{OSPARC_IDENTIFIER_PREFIX}[A-Za-z0-9_]+(?:\}})?(:-.+)?$"
21-
)
2218

2319
def _get_without_template_markers(self) -> str:
2420
# $VAR
@@ -42,6 +38,40 @@ def default_value(self) -> str | None:
4238
parts = self._get_without_template_markers().split(":-")
4339
return parts[1] if len(parts) > 1 else None
4440

41+
@staticmethod
42+
def get_pattern(max_dollars: PositiveInt) -> str:
43+
# NOTE: in below regex `{`` and `}` are respectively escaped with `{{` and `}}`
44+
return rf"^\${{1,{max_dollars}}}(?:\{{)?{OSPARC_IDENTIFIER_PREFIX}[A-Za-z0-9_]+(?:\}})?(:-.+)?$"
45+
46+
47+
class PlatformOsparcVariableIdentifier(_BaseOsparcVariableIdentifier):
48+
pattern = _BaseOsparcVariableIdentifier.get_pattern(max_dollars=2)
49+
50+
51+
class OoilOsparcVariableIdentifier(_BaseOsparcVariableIdentifier):
52+
pattern = _BaseOsparcVariableIdentifier.get_pattern(max_dollars=4)
53+
54+
55+
_PLATFORM: Final[str] = "platform"
56+
_OOIL_VERSION: Final[str] = "ooil-version"
57+
58+
59+
def _get_discriminator_value(v: Any) -> str:
60+
_ = v
61+
if os.environ.get("ENABLE_OOIL_OSPARC_VARIABLE_IDENTIFIER", None):
62+
return _OOIL_VERSION
63+
64+
return _PLATFORM
65+
66+
67+
OsparcVariableIdentifier = Annotated[
68+
(
69+
Annotated[PlatformOsparcVariableIdentifier, Tag(_PLATFORM)]
70+
| Annotated[OoilOsparcVariableIdentifier, Tag(_OOIL_VERSION)]
71+
),
72+
Discriminator(_get_discriminator_value),
73+
]
74+
4575

4676
class UnresolvedOsparcVariableIdentifierError(OsparcErrorMixin, TypeError):
4777
msg_template = "Provided argument is unresolved: value={value}"

packages/service-integration/tests/test_osparc_image_specs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import yaml
1010
from pydantic import BaseModel
11+
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1112
from service_integration.compose_spec_model import BuildItem, Service
1213
from service_integration.osparc_config import (
1314
DockerComposeOverwriteConfig,
@@ -19,7 +20,13 @@
1920

2021

2122
@pytest.fixture
22-
def settings() -> AppSettings:
23+
def settings(monkeypatch: pytest.MonkeyPatch) -> AppSettings:
24+
setenvs_from_dict(
25+
monkeypatch,
26+
{
27+
"ENABLE_OOIL_OSPARC_VARIABLE_IDENTIFIER": "true",
28+
},
29+
)
2330
return AppSettings()
2431

2532

0 commit comments

Comments
 (0)