1+ import os
12from copy import deepcopy
2- from typing import Any , TypeVar
3+ from typing import Annotated , Any , Final , TypeVar
34
45from common_library .errors_classes import OsparcErrorMixin
56from models_library .basic_types import ConstrainedStr
6-
7- from pydantic import BaseModel
7+ from pydantic import BaseModel , Discriminator , PositiveInt , Tag
88
99from .utils .string_substitution import OSPARC_IDENTIFIER_PREFIX
1010
1111T = 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
4676class UnresolvedOsparcVariableIdentifierError (OsparcErrorMixin , TypeError ):
4777 msg_template = "Provided argument is unresolved: value={value}"
0 commit comments