|
| 1 | +# pydantify-common |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Base Model Classes**: `PydantifyModel` and `XMLPydantifyModel` for creating structured data models |
| 8 | +- **XML Serialization**: Convert Pydantic models to XML with namespace support |
| 9 | +- **NETCONF Compatibility**: Built-in support for NETCONF data root elements |
| 10 | +- **Type Safety**: Full type hints and Pydantic v2 support |
| 11 | +- **Flexible Output**: Pretty-print and custom root element options |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install pydantify-common |
| 17 | +``` |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- Python >= 3.10 |
| 22 | +- pydantic >= 2 |
| 23 | +- lxml >= 6.0.2 |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +### Basic Model Definition |
| 28 | + |
| 29 | +Make sure the pydantic model inherits from `XMLPydantifyModel` and defines `namespace` and `prefix` as class variables. |
| 30 | + |
| 31 | +```python |
| 32 | +from pydantify_common.model import XMLPydantifyModel |
| 33 | + |
| 34 | +class MyModel(XMLPydantifyModel): |
| 35 | + namespace = "http://example.com/schema" |
| 36 | + prefix = "ex" |
| 37 | + |
| 38 | + name: str |
| 39 | + value: int |
| 40 | +``` |
| 41 | + |
| 42 | +### XML Serialization |
| 43 | + |
| 44 | +```python |
| 45 | +from pydantify_common.helper import model_dump_xml_string |
| 46 | + |
| 47 | +model = MyModel(name="test", value=42) |
| 48 | +xml_str = model_dump_xml_string(model, pretty_print=True) |
| 49 | +print(xml_str) |
| 50 | +``` |
| 51 | + |
| 52 | +### NETCONF Data Root |
| 53 | + |
| 54 | +```python |
| 55 | +# Add NETCONF-compatible data root element |
| 56 | +xml_str = model_dump_xml_string(model, pretty_print=True, data_root=True) |
| 57 | +``` |
| 58 | + |
| 59 | +## API Reference |
| 60 | + |
| 61 | +### PydantifyModel |
| 62 | + |
| 63 | +Base class for all pydantify models. Inherits from Pydantic's `BaseModel`. |
| 64 | + |
| 65 | +```python |
| 66 | +class PydantifyModel(BaseModel): |
| 67 | + pass |
| 68 | +``` |
| 69 | + |
| 70 | +### XMLPydantifyModel |
| 71 | + |
| 72 | +Extended base class with XML serialization capabilities. |
| 73 | + |
| 74 | +**Class Variables:** |
| 75 | +- `namespace` (str): XML namespace URI |
| 76 | +- `prefix` (str): XML namespace prefix |
| 77 | + |
| 78 | +**Methods:** |
| 79 | +- `model_dump_xml() -> Element`: Returns an lxml Element representation |
| 80 | +- `fields_to_elements(container_name: str | None = None) -> Element`: Converts model fields to XML elements |
| 81 | + |
| 82 | +### model_dump_xml_string() |
| 83 | + |
| 84 | +Helper function to serialize XMLPydantifyModel instances to XML strings. |
| 85 | + |
| 86 | +```python |
| 87 | +def model_dump_xml_string( |
| 88 | + model: XMLPydantifyModel, |
| 89 | + *, |
| 90 | + pretty_print: bool = False, |
| 91 | + data_root: bool = False |
| 92 | +) -> str |
| 93 | +``` |
| 94 | + |
| 95 | +**Parameters:** |
| 96 | +- `model`: The XMLPydantifyModel instance to serialize |
| 97 | +- `pretty_print`: Enable formatted XML output (default: False) |
| 98 | +- `data_root`: Add NETCONF-compatible `<data>` root element (default: False) |
| 99 | + |
| 100 | +**Returns:** XML string representation of the model |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +The project includes comprehensive examples in the `tests/examples/` directory: |
| 105 | + |
| 106 | +- **with_augment**: Demonstrates augmented YANG models with namespace support |
| 107 | +- **with_import_uses**: Shows handling of YANG imports and type reuse |
| 108 | + |
| 109 | +Run tests to see examples in action: |
| 110 | + |
| 111 | +```bash |
| 112 | +uv run pytest tests/test_examples.py -vvv |
| 113 | +``` |
| 114 | + |
| 115 | +## Development |
| 116 | + |
| 117 | +### Setup Development Environment |
| 118 | + |
| 119 | +```bash |
| 120 | +uv sync |
| 121 | +``` |
| 122 | + |
| 123 | +### Running Tests |
| 124 | + |
| 125 | +```bash |
| 126 | +make tests |
| 127 | +``` |
| 128 | + |
| 129 | +### Code Quality |
| 130 | + |
| 131 | +```bash |
| 132 | +# Type checking |
| 133 | +make mypy |
| 134 | + |
| 135 | +# Linting and formatting |
| 136 | +make ruff |
| 137 | +``` |
| 138 | + |
| 139 | +## Contributing |
| 140 | + |
| 141 | +Contributions are welcome! Please ensure all tests pass and code follows the project's style guidelines before submitting a pull request. |
0 commit comments