pydantify-common 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,3 @@
1
+ from .model import PydantifyModel
2
+
3
+ __all__ = ["PydantifyModel"]
@@ -0,0 +1,18 @@
1
+ from lxml import etree
2
+ from .model import XMLPydantifyModel
3
+
4
+
5
+ def model_dump_xml_string(
6
+ model: XMLPydantifyModel, *, pretty_print: bool = False, data_root: bool = False
7
+ ) -> str:
8
+ data = model.model_dump_xml()
9
+ if data_root:
10
+ # Add `<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">` root element
11
+ root = etree.Element(
12
+ "data",
13
+ nsmap={None: "urn:ietf:params:xml:ns:netconf:base:1.0"}, # type: ignore
14
+ )
15
+ root.append(data)
16
+ data = root
17
+
18
+ return etree.tostring(data, encoding=str, pretty_print=pretty_print)
@@ -0,0 +1,57 @@
1
+ from typing import ClassVar, TYPE_CHECKING
2
+
3
+ from pydantic import BaseModel
4
+ from lxml import etree
5
+
6
+ if TYPE_CHECKING:
7
+ from lxml.etree import _Element
8
+
9
+
10
+ class PydantifyModel(BaseModel):
11
+ pass
12
+
13
+
14
+ class XMLPydantifyModel(PydantifyModel):
15
+ namespace: ClassVar[str]
16
+ prefix: ClassVar[str]
17
+
18
+ def model_dump_xml(self) -> "_Element":
19
+ data = self.fields_to_elements()
20
+ return list(data)[0]
21
+
22
+ def fields_to_elements(
23
+ self,
24
+ container_name: str | None = None,
25
+ ) -> "_Element":
26
+ if container_name:
27
+ root = etree.Element(
28
+ container_name,
29
+ nsmap={None: self.namespace}, # type: ignore
30
+ )
31
+ else:
32
+ root = etree.Element("root")
33
+ for field in self.model_dump(exclude_defaults=True, exclude_none=True).keys():
34
+ value = getattr(self, field)
35
+ match value:
36
+ case XMLPydantifyModel():
37
+ root.append(
38
+ value.fields_to_elements(
39
+ container_name=field,
40
+ )
41
+ )
42
+ case list():
43
+ for item in value:
44
+ if isinstance(item, XMLPydantifyModel):
45
+ root.append(
46
+ item.fields_to_elements(
47
+ container_name=field,
48
+ )
49
+ )
50
+ else:
51
+ raise NotImplementedError(
52
+ "List serialization not implemented yet."
53
+ )
54
+ case _:
55
+ elem = etree.SubElement(root, field)
56
+ elem.text = str(value)
57
+ return root
File without changes
@@ -0,0 +1,151 @@
1
+ Metadata-Version: 2.4
2
+ Name: pydantify-common
3
+ Version: 0.1.0
4
+ Summary: Pydantify common classes and functions
5
+ Author-email: ubaumann <github@m.ubaumann.ch>
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: lxml>=6.0.2
8
+ Requires-Dist: pydantic>=2
9
+ Description-Content-Type: text/markdown
10
+
11
+ # pydantify-common
12
+
13
+ A Python library providing common base classes and utilities for building Pydantic-based models with XML serialization support. This package serves as the foundation for the pydantify project, enabling seamless conversion between Pydantic models and XML representations.
14
+
15
+ ## Features
16
+
17
+ - **Base Model Classes**: `PydantifyModel` and `XMLPydantifyModel` for creating structured data models
18
+ - **XML Serialization**: Convert Pydantic models to XML with namespace support
19
+ - **NETCONF Compatibility**: Built-in support for NETCONF data root elements
20
+ - **Type Safety**: Full type hints and Pydantic v2 support
21
+ - **Flexible Output**: Pretty-print and custom root element options
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install pydantify-common
27
+ ```
28
+
29
+ ## Requirements
30
+
31
+ - Python >= 3.10
32
+ - pydantic >= 2
33
+ - lxml >= 6.0.2
34
+
35
+ ## Quick Start
36
+
37
+ ### Basic Model Definition
38
+
39
+ Make sure the pydantic model inherits from `XMLPydantifyModel` and defines `namespace` and `prefix` as class variables.
40
+
41
+ ```python
42
+ from pydantify_common.model import XMLPydantifyModel
43
+
44
+ class MyModel(XMLPydantifyModel):
45
+ namespace = "http://example.com/schema"
46
+ prefix = "ex"
47
+
48
+ name: str
49
+ value: int
50
+ ```
51
+
52
+ ### XML Serialization
53
+
54
+ ```python
55
+ from pydantify_common.helper import model_dump_xml_string
56
+
57
+ model = MyModel(name="test", value=42)
58
+ xml_str = model_dump_xml_string(model, pretty_print=True)
59
+ print(xml_str)
60
+ ```
61
+
62
+ ### NETCONF Data Root
63
+
64
+ ```python
65
+ # Add NETCONF-compatible data root element
66
+ xml_str = model_dump_xml_string(model, pretty_print=True, data_root=True)
67
+ ```
68
+
69
+ ## API Reference
70
+
71
+ ### PydantifyModel
72
+
73
+ Base class for all pydantify models. Inherits from Pydantic's `BaseModel`.
74
+
75
+ ```python
76
+ class PydantifyModel(BaseModel):
77
+ pass
78
+ ```
79
+
80
+ ### XMLPydantifyModel
81
+
82
+ Extended base class with XML serialization capabilities.
83
+
84
+ **Class Variables:**
85
+ - `namespace` (str): XML namespace URI
86
+ - `prefix` (str): XML namespace prefix
87
+
88
+ **Methods:**
89
+ - `model_dump_xml() -> Element`: Returns an lxml Element representation
90
+ - `fields_to_elements(container_name: str | None = None) -> Element`: Converts model fields to XML elements
91
+
92
+ ### model_dump_xml_string()
93
+
94
+ Helper function to serialize XMLPydantifyModel instances to XML strings.
95
+
96
+ ```python
97
+ def model_dump_xml_string(
98
+ model: XMLPydantifyModel,
99
+ *,
100
+ pretty_print: bool = False,
101
+ data_root: bool = False
102
+ ) -> str
103
+ ```
104
+
105
+ **Parameters:**
106
+ - `model`: The XMLPydantifyModel instance to serialize
107
+ - `pretty_print`: Enable formatted XML output (default: False)
108
+ - `data_root`: Add NETCONF-compatible `<data>` root element (default: False)
109
+
110
+ **Returns:** XML string representation of the model
111
+
112
+ ## Examples
113
+
114
+ The project includes comprehensive examples in the `tests/examples/` directory:
115
+
116
+ - **with_augment**: Demonstrates augmented YANG models with namespace support
117
+ - **with_import_uses**: Shows handling of YANG imports and type reuse
118
+
119
+ Run tests to see examples in action:
120
+
121
+ ```bash
122
+ uv run pytest tests/test_examples.py -vvv
123
+ ```
124
+
125
+ ## Development
126
+
127
+ ### Setup Development Environment
128
+
129
+ ```bash
130
+ uv sync
131
+ ```
132
+
133
+ ### Running Tests
134
+
135
+ ```bash
136
+ make tests
137
+ ```
138
+
139
+ ### Code Quality
140
+
141
+ ```bash
142
+ # Type checking
143
+ make mypy
144
+
145
+ # Linting and formatting
146
+ make ruff
147
+ ```
148
+
149
+ ## Contributing
150
+
151
+ Contributions are welcome! Please ensure all tests pass and code follows the project's style guidelines before submitting a pull request.
@@ -0,0 +1,7 @@
1
+ pydantify_common/__init__.py,sha256=I9eMB4OaGmO9_4JFH3MIn_AabuHePlDygTTqgaB0etY,64
2
+ pydantify_common/helper.py,sha256=M-lTgZ4gYbmbyDj5hc_W1TLnCIJLFr1iOjHeeDL4nEw,585
3
+ pydantify_common/model.py,sha256=yVcGycZYZ10lxcXZkhXJ1i70GdkgOfCdOnUzYvpTZQM,1802
4
+ pydantify_common/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ pydantify_common-0.1.0.dist-info/METADATA,sha256=vWyc-M6nRNCPp07Q6lw9_U9zG-tENjhpx14xBlqd6E0,3580
6
+ pydantify_common-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
7
+ pydantify_common-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any