annotate libervia/backend/models/types.py @ 4309:b56b1eae7994

component email gateway: add multicasting: XEP-0033 multicasting is now supported both for incoming and outgoing messages. XEP-0033 metadata are converted to suitable Email headers and vice versa. Email address and JID are both supported, and delivery is done by the gateway when suitable on incoming messages. rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents ffc43219e0b2
children e1fcf4dd9012
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4296
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia models custom types
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import TYPE_CHECKING, Annotated, Any, TypeAlias, cast
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from pydantic.json_schema import JsonSchemaValue
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from pydantic_core import core_schema
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber.jid import JID
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 class _JIDType:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 """Use Twisted's JID in Python type, and serialize it to str
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 In Python, both JID and str are accepted, str are converted to JID.
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 """
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 @staticmethod
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def validate_jid(value: str) -> JID:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 return JID(value)
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @staticmethod
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def serialize_jid(jid: JID) -> str:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 return str(jid)
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 @classmethod
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def __get_pydantic_core_schema__(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 cls, source_type: Any, handler: GetCoreSchemaHandler
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 ) -> core_schema.CoreSchema:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 return core_schema.json_or_python_schema(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 json_schema=core_schema.no_info_after_validator_function(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 cls.validate_jid, core_schema.str_schema()
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 python_schema=core_schema.union_schema(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 [
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 core_schema.is_instance_schema(JID),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 core_schema.no_info_after_validator_function(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 cls.validate_jid, core_schema.str_schema()
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ]
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 serialization=core_schema.plain_serializer_function_ser_schema(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 cls.serialize_jid, when_used="json"
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 )
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 @classmethod
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def __get_pydantic_json_schema__(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 ) -> JsonSchemaValue:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 json_schema = handler(schema)
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 json_schema.update(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 {
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 "format": "jid",
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 "description": "A valid Jabber ID (JID) in the format "
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 '"user@domain/resource"',
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 }
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 )
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return json_schema
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 class _StrictJIDType(_JIDType):
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Strict version of JIDType which only accept JID in Python."""
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @classmethod
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def __get_pydantic_core_schema__(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 cls, source_type: Any, handler: GetCoreSchemaHandler
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 ) -> core_schema.CoreSchema:
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return core_schema.json_or_python_schema(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 json_schema=core_schema.no_info_after_validator_function(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 cls.validate_jid, core_schema.str_schema()
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 python_schema=core_schema.is_instance_schema(JID),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 serialization=core_schema.plain_serializer_function_ser_schema(
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 cls.serialize_jid, when_used="json"
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 ),
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 )
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 # We annotate the types so static type checker understand them as JID.
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 JIDType = Annotated[JID, _JIDType]
ffc43219e0b2 core (models): add new models for `DiscoIdentity` and `JID`:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 StrictJIDType = Annotated[JID, _StrictJIDType]