qt-pydantic 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qt_pydantic-0.1.0/LICENSE +21 -0
- qt_pydantic-0.1.0/PKG-INFO +70 -0
- qt_pydantic-0.1.0/README.md +49 -0
- qt_pydantic-0.1.0/pyproject.toml +41 -0
- qt_pydantic-0.1.0/qt_pydantic/__init__.py +15 -0
- qt_pydantic-0.1.0/qt_pydantic/annotations.py +168 -0
- qt_pydantic-0.1.0/qt_pydantic.egg-info/PKG-INFO +70 -0
- qt_pydantic-0.1.0/qt_pydantic.egg-info/SOURCES.txt +10 -0
- qt_pydantic-0.1.0/qt_pydantic.egg-info/dependency_links.txt +1 -0
- qt_pydantic-0.1.0/qt_pydantic.egg-info/requires.txt +14 -0
- qt_pydantic-0.1.0/qt_pydantic.egg-info/top_level.txt +1 -0
- qt_pydantic-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Beat Reichenbach
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: qt-pydantic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pydantic BaseModel Annotations for Qt
|
|
5
|
+
Author: Beat Reichenbach
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: qtpy<3,>=2.0
|
|
11
|
+
Requires-Dist: pydantic<3,>=2.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: black>=24.0; extra == "dev"
|
|
14
|
+
Requires-Dist: python-semantic-release>=9.0; extra == "dev"
|
|
15
|
+
Requires-Dist: PySide6; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
17
|
+
Provides-Extra: pyside2
|
|
18
|
+
Requires-Dist: PySide2; extra == "pyside2"
|
|
19
|
+
Provides-Extra: pyside6
|
|
20
|
+
Requires-Dist: PySide6; extra == "pyside6"
|
|
21
|
+
|
|
22
|
+
# qt-pydantic
|
|
23
|
+
|
|
24
|
+
The `qt-pydantic` package adds support for Qt types in Pydantic BaseModels.
|
|
25
|
+
Using these annotations allows for easy serialization and deserialization of Qt types.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Install using pip:
|
|
31
|
+
```shell
|
|
32
|
+
pip install qt-pydantic
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from PySide6 import QtCore, QtGui
|
|
39
|
+
from pydantic import BaseModel
|
|
40
|
+
from qt_pydantic import QSize, QColor, QDate
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Define a model with Qt types
|
|
44
|
+
class Settings(BaseModel):
|
|
45
|
+
size: QSize
|
|
46
|
+
date: QDate
|
|
47
|
+
color: QColor
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Parse json string into model
|
|
51
|
+
json_data = '{"size": [720, 480], "date": "2021-01-01", "color": [255, 95, 135]}'
|
|
52
|
+
settings = Settings.model_validate_json(json_data)
|
|
53
|
+
|
|
54
|
+
# Model types are actual Qt types
|
|
55
|
+
assert isinstance(settings.size, QtCore.QSize)
|
|
56
|
+
assert isinstance(settings.date, QtCore.QDate)
|
|
57
|
+
assert isinstance(settings.color, QtGui.QColor)
|
|
58
|
+
|
|
59
|
+
# Turn model into json string
|
|
60
|
+
data = settings.model_dump_json(indent=2)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Contributing
|
|
64
|
+
|
|
65
|
+
To contribute please refer to the [Contributing Guide](CONTRIBUTING.md).
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT License. Copyright 2024 - Beat Reichenbach.
|
|
70
|
+
See the [License file](LICENSE) for details.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# qt-pydantic
|
|
2
|
+
|
|
3
|
+
The `qt-pydantic` package adds support for Qt types in Pydantic BaseModels.
|
|
4
|
+
Using these annotations allows for easy serialization and deserialization of Qt types.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install using pip:
|
|
10
|
+
```shell
|
|
11
|
+
pip install qt-pydantic
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from PySide6 import QtCore, QtGui
|
|
18
|
+
from pydantic import BaseModel
|
|
19
|
+
from qt_pydantic import QSize, QColor, QDate
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Define a model with Qt types
|
|
23
|
+
class Settings(BaseModel):
|
|
24
|
+
size: QSize
|
|
25
|
+
date: QDate
|
|
26
|
+
color: QColor
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Parse json string into model
|
|
30
|
+
json_data = '{"size": [720, 480], "date": "2021-01-01", "color": [255, 95, 135]}'
|
|
31
|
+
settings = Settings.model_validate_json(json_data)
|
|
32
|
+
|
|
33
|
+
# Model types are actual Qt types
|
|
34
|
+
assert isinstance(settings.size, QtCore.QSize)
|
|
35
|
+
assert isinstance(settings.date, QtCore.QDate)
|
|
36
|
+
assert isinstance(settings.color, QtGui.QColor)
|
|
37
|
+
|
|
38
|
+
# Turn model into json string
|
|
39
|
+
data = settings.model_dump_json(indent=2)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Contributing
|
|
43
|
+
|
|
44
|
+
To contribute please refer to the [Contributing Guide](CONTRIBUTING.md).
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT License. Copyright 2024 - Beat Reichenbach.
|
|
49
|
+
See the [License file](LICENSE) for details.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "qt-pydantic"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Pydantic BaseModel Annotations for Qt"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Beat Reichenbach"}
|
|
7
|
+
]
|
|
8
|
+
dependencies = [
|
|
9
|
+
"qtpy>=2.0,<3",
|
|
10
|
+
"pydantic>=2.0,<3",
|
|
11
|
+
]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
license = {text = "MIT"}
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
|
|
16
|
+
[project.optional-dependencies]
|
|
17
|
+
dev = [
|
|
18
|
+
"black>=24.0",
|
|
19
|
+
"python-semantic-release>=9.0",
|
|
20
|
+
"PySide6",
|
|
21
|
+
"pytest>=8.0",
|
|
22
|
+
]
|
|
23
|
+
pyside2 = ["PySide2"]
|
|
24
|
+
pyside6 = ["PySide6"]
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["setuptools"]
|
|
28
|
+
build-backend = "setuptools.build_meta"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["qt_pydantic"]
|
|
32
|
+
|
|
33
|
+
[tool.semantic_release]
|
|
34
|
+
version_variables = ["qt_pydantic/__init__.py:__version__"]
|
|
35
|
+
version_toml = ["pyproject.toml:project.version"]
|
|
36
|
+
|
|
37
|
+
[tool.semantic_release.remote]
|
|
38
|
+
ignore_token_for_push = true
|
|
39
|
+
|
|
40
|
+
[tool.black]
|
|
41
|
+
skip-string-normalization = true
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from qtpy import QtCore, QtGui
|
|
7
|
+
from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
|
|
8
|
+
from pydantic.json_schema import JsonSchemaValue
|
|
9
|
+
from pydantic_core import core_schema
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class QAnnotation:
|
|
13
|
+
qtype: type
|
|
14
|
+
schema: core_schema.CoreSchema
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def __get_pydantic_core_schema__(
|
|
18
|
+
cls,
|
|
19
|
+
_source_type: Any,
|
|
20
|
+
_handler: GetCoreSchemaHandler,
|
|
21
|
+
) -> core_schema.CoreSchema:
|
|
22
|
+
chain_schema = core_schema.chain_schema(
|
|
23
|
+
[cls.schema, core_schema.no_info_plain_validator_function(cls.validate)]
|
|
24
|
+
)
|
|
25
|
+
python_schema = core_schema.union_schema(
|
|
26
|
+
[core_schema.is_instance_schema(cls.qtype), chain_schema]
|
|
27
|
+
)
|
|
28
|
+
serialization = core_schema.plain_serializer_function_ser_schema(cls.serialize)
|
|
29
|
+
return core_schema.json_or_python_schema(
|
|
30
|
+
json_schema=chain_schema,
|
|
31
|
+
python_schema=python_schema,
|
|
32
|
+
serialization=serialization,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def __get_pydantic_json_schema__(
|
|
37
|
+
cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
|
|
38
|
+
) -> JsonSchemaValue:
|
|
39
|
+
return handler(cls.schema)
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def validate(cls, value: Any) -> QAnnotation.qtype:
|
|
43
|
+
if isinstance(value, Sequence) and not isinstance(value, str):
|
|
44
|
+
return cls.qtype(*value)
|
|
45
|
+
return cls.qtype(value)
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def serialize(value: Any) -> Any: ...
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# QtCore
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class QSize(QAnnotation):
|
|
55
|
+
qtype = QtCore.QSize
|
|
56
|
+
schema = core_schema.tuple_schema([core_schema.int_schema()] * 2)
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def serialize(value: Any) -> Any:
|
|
60
|
+
return value.width(), value.height()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class QSizeF(QSize):
|
|
64
|
+
qtype = QtCore.QSizeF
|
|
65
|
+
schema = core_schema.tuple_schema([core_schema.float_schema()] * 2)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class QPoint(QAnnotation):
|
|
69
|
+
qtype = QtCore.QPoint
|
|
70
|
+
schema = core_schema.tuple_schema([core_schema.int_schema()] * 2)
|
|
71
|
+
|
|
72
|
+
@staticmethod
|
|
73
|
+
def serialize(value: Any) -> Any:
|
|
74
|
+
return value.x(), value.y()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class QPointF(QPoint):
|
|
78
|
+
qtype = QtCore.QPointF
|
|
79
|
+
schema = core_schema.tuple_schema([core_schema.float_schema()] * 2)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class QRect(QAnnotation):
|
|
83
|
+
qtype = QtCore.QRect
|
|
84
|
+
schema = core_schema.tuple_schema([core_schema.int_schema()] * 4)
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def serialize(value: Any) -> Any:
|
|
88
|
+
return value.x(), value.y(), value.width(), value.height()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class QRectF(QRect):
|
|
92
|
+
qtype = QtCore.QRectF
|
|
93
|
+
schema = core_schema.tuple_schema([core_schema.float_schema()] * 4)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class QDate(QAnnotation):
|
|
97
|
+
qtype = QtCore.QDate
|
|
98
|
+
schema = core_schema.date_schema()
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def validate(cls, value: Any) -> QAnnotation.qtype:
|
|
102
|
+
return cls.qtype(value.year, value.month, value.day)
|
|
103
|
+
|
|
104
|
+
@staticmethod
|
|
105
|
+
def serialize(value: Any) -> Any:
|
|
106
|
+
return value.toString(QtCore.Qt.DateFormat.ISODate)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class QDateTime(QAnnotation):
|
|
110
|
+
qtype = QtCore.QDateTime
|
|
111
|
+
schema = core_schema.datetime_schema()
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
def validate(cls, value: Any) -> QAnnotation.qtype:
|
|
115
|
+
msecs = int(value.timestamp() * 1000)
|
|
116
|
+
return cls.qtype.fromMSecsSinceEpoch(msecs)
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def serialize(value: Any) -> Any:
|
|
120
|
+
return value.toString(QtCore.Qt.DateFormat.ISODate)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class QTime(QAnnotation):
|
|
124
|
+
qtype = QtCore.QTime
|
|
125
|
+
schema = core_schema.time_schema()
|
|
126
|
+
|
|
127
|
+
@classmethod
|
|
128
|
+
def validate(cls, value: Any) -> QAnnotation.qtype:
|
|
129
|
+
return cls.qtype(
|
|
130
|
+
value.hour, value.minute, value.second, value.microsecond // 1000
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
@staticmethod
|
|
134
|
+
def serialize(value: Any) -> Any:
|
|
135
|
+
return value.toString(QtCore.Qt.DateFormat.ISODate)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class QUuid(QAnnotation):
|
|
139
|
+
qtype = QtCore.QUuid
|
|
140
|
+
schema = core_schema.str_schema()
|
|
141
|
+
|
|
142
|
+
@staticmethod
|
|
143
|
+
def serialize(value: Any) -> Any:
|
|
144
|
+
return value.toString()
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# QColor
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class QColor(QAnnotation):
|
|
151
|
+
qtype = QtGui.QColor
|
|
152
|
+
schema = core_schema.union_schema(
|
|
153
|
+
[
|
|
154
|
+
# name
|
|
155
|
+
core_schema.str_schema(),
|
|
156
|
+
# rgb(a)
|
|
157
|
+
core_schema.tuple_schema(
|
|
158
|
+
[core_schema.int_schema(ge=0, le=255)],
|
|
159
|
+
variadic_item_index=0,
|
|
160
|
+
min_length=3,
|
|
161
|
+
max_length=4,
|
|
162
|
+
),
|
|
163
|
+
]
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def serialize(value: Any) -> Any:
|
|
168
|
+
return value.getRgb()
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: qt-pydantic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pydantic BaseModel Annotations for Qt
|
|
5
|
+
Author: Beat Reichenbach
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: qtpy<3,>=2.0
|
|
11
|
+
Requires-Dist: pydantic<3,>=2.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: black>=24.0; extra == "dev"
|
|
14
|
+
Requires-Dist: python-semantic-release>=9.0; extra == "dev"
|
|
15
|
+
Requires-Dist: PySide6; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
17
|
+
Provides-Extra: pyside2
|
|
18
|
+
Requires-Dist: PySide2; extra == "pyside2"
|
|
19
|
+
Provides-Extra: pyside6
|
|
20
|
+
Requires-Dist: PySide6; extra == "pyside6"
|
|
21
|
+
|
|
22
|
+
# qt-pydantic
|
|
23
|
+
|
|
24
|
+
The `qt-pydantic` package adds support for Qt types in Pydantic BaseModels.
|
|
25
|
+
Using these annotations allows for easy serialization and deserialization of Qt types.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Install using pip:
|
|
31
|
+
```shell
|
|
32
|
+
pip install qt-pydantic
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from PySide6 import QtCore, QtGui
|
|
39
|
+
from pydantic import BaseModel
|
|
40
|
+
from qt_pydantic import QSize, QColor, QDate
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Define a model with Qt types
|
|
44
|
+
class Settings(BaseModel):
|
|
45
|
+
size: QSize
|
|
46
|
+
date: QDate
|
|
47
|
+
color: QColor
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Parse json string into model
|
|
51
|
+
json_data = '{"size": [720, 480], "date": "2021-01-01", "color": [255, 95, 135]}'
|
|
52
|
+
settings = Settings.model_validate_json(json_data)
|
|
53
|
+
|
|
54
|
+
# Model types are actual Qt types
|
|
55
|
+
assert isinstance(settings.size, QtCore.QSize)
|
|
56
|
+
assert isinstance(settings.date, QtCore.QDate)
|
|
57
|
+
assert isinstance(settings.color, QtGui.QColor)
|
|
58
|
+
|
|
59
|
+
# Turn model into json string
|
|
60
|
+
data = settings.model_dump_json(indent=2)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Contributing
|
|
64
|
+
|
|
65
|
+
To contribute please refer to the [Contributing Guide](CONTRIBUTING.md).
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT License. Copyright 2024 - Beat Reichenbach.
|
|
70
|
+
See the [License file](LICENSE) for details.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
qt_pydantic/__init__.py
|
|
5
|
+
qt_pydantic/annotations.py
|
|
6
|
+
qt_pydantic.egg-info/PKG-INFO
|
|
7
|
+
qt_pydantic.egg-info/SOURCES.txt
|
|
8
|
+
qt_pydantic.egg-info/dependency_links.txt
|
|
9
|
+
qt_pydantic.egg-info/requires.txt
|
|
10
|
+
qt_pydantic.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
qt_pydantic
|