|
| 1 | +from re import Pattern |
| 2 | +from typing import Annotated, Final, TypeAlias |
| 3 | + |
| 4 | +from pydantic import Field |
| 5 | +from pydantic_core import core_schema |
| 6 | + |
| 7 | +# https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Registered_ports |
| 8 | +RegisteredPortInt: TypeAlias = Annotated[int, Field(gt=1024, lt=65535)] |
| 9 | + |
| 10 | +# non-empty bounded string used as identifier |
| 11 | +# e.g. "123" or "name_123" or "fa327c73-52d8-462a-9267-84eeaf0f90e3" but NOT "" |
| 12 | +_ELLIPSIS_CHAR: Final[str] = "..." |
| 13 | + |
| 14 | + |
| 15 | +class ConstrainedStr(str): # noqa: SLOT000 |
| 16 | + pattern: str | Pattern[str] | None = None |
| 17 | + min_length: int | None = None |
| 18 | + max_length: int | None = None |
| 19 | + strip_whitespace: bool = False |
| 20 | + curtail_length: int | None = None |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def _validate(cls, __input_value: str) -> str: |
| 24 | + if cls.curtail_length and len(__input_value) > cls.curtail_length: |
| 25 | + __input_value = __input_value[: cls.curtail_length] |
| 26 | + return cls(__input_value) |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def __get_pydantic_core_schema__(cls, _source_type, _handler): |
| 30 | + return core_schema.no_info_after_validator_function( |
| 31 | + cls._validate, |
| 32 | + core_schema.str_schema( |
| 33 | + pattern=cls.pattern, |
| 34 | + min_length=cls.min_length, |
| 35 | + max_length=cls.max_length, |
| 36 | + strip_whitespace=cls.strip_whitespace, |
| 37 | + ), |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +class IDStr(ConstrainedStr): |
| 42 | + strip_whitespace = True |
| 43 | + min_length = 1 |
| 44 | + max_length = 100 |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def concatenate(*args: "IDStr", link_char: str = " ") -> "IDStr": |
| 48 | + result = link_char.join(args).strip() |
| 49 | + assert IDStr.min_length # nosec |
| 50 | + assert IDStr.max_length # nosec |
| 51 | + if len(result) > IDStr.max_length: |
| 52 | + if IDStr.max_length > len(_ELLIPSIS_CHAR): |
| 53 | + result = ( |
| 54 | + result[: IDStr.max_length - len(_ELLIPSIS_CHAR)].rstrip() |
| 55 | + + _ELLIPSIS_CHAR |
| 56 | + ) |
| 57 | + else: |
| 58 | + result = _ELLIPSIS_CHAR[0] * IDStr.max_length |
| 59 | + if len(result) < IDStr.min_length: |
| 60 | + msg = f"IDStr.concatenate: result is too short: {result}" |
| 61 | + raise ValueError(msg) |
| 62 | + return IDStr(result) |
| 63 | + |
| 64 | + |
| 65 | +class ShortTruncatedStr(ConstrainedStr): |
| 66 | + # NOTE: Use to input e.g. titles or display names |
| 67 | + # A truncated string: |
| 68 | + # - Strips whitespaces and truncate strings that exceed the specified characters limit (curtail_length). |
| 69 | + # - Ensures that the **input** data length to the API is controlled and prevents exceeding large inputs silently, i.e. without raising errors. |
| 70 | + # SEE https://github.com/ITISFoundation/osparc-simcore/pull/5989#discussion_r1650506583 |
| 71 | + strip_whitespace = True |
| 72 | + curtail_length = 600 |
| 73 | + |
| 74 | + |
| 75 | +class LongTruncatedStr(ConstrainedStr): |
| 76 | + # NOTE: Use to input e.g. descriptions or summaries |
| 77 | + # Analogous to ShortTruncatedStr |
| 78 | + strip_whitespace = True |
| 79 | + curtail_length = 65536 # same as github descripton |
0 commit comments