pydantify-common 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.
- pydantify_common-0.1.0/.github/workflows/main.yaml +83 -0
- pydantify_common-0.1.0/.gitignore +10 -0
- pydantify_common-0.1.0/.python-version +1 -0
- pydantify_common-0.1.0/Makefile +21 -0
- pydantify_common-0.1.0/PKG-INFO +151 -0
- pydantify_common-0.1.0/README.md +141 -0
- pydantify_common-0.1.0/pyproject.toml +26 -0
- pydantify_common-0.1.0/src/pydantify_common/__init__.py +3 -0
- pydantify_common-0.1.0/src/pydantify_common/helper.py +18 -0
- pydantify_common-0.1.0/src/pydantify_common/model.py +57 -0
- pydantify_common-0.1.0/src/pydantify_common/py.typed +0 -0
- pydantify_common-0.1.0/tests/examples/with_augment/README.md +34 -0
- pydantify_common-0.1.0/tests/examples/with_augment/configuration.yang +21 -0
- pydantify_common-0.1.0/tests/examples/with_augment/expected.json +28 -0
- pydantify_common-0.1.0/tests/examples/with_augment/expected.xml +23 -0
- pydantify_common-0.1.0/tests/examples/with_augment/expected_ns.xml +23 -0
- pydantify_common-0.1.0/tests/examples/with_augment/interfaces.yang +41 -0
- pydantify_common-0.1.0/tests/examples/with_augment/model.py +120 -0
- pydantify_common-0.1.0/tests/examples/with_augment/namespaces.yang +33 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/README.md +34 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/configuration.yang +26 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/expected.json +28 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/expected.xml +23 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/expected_ns.xml +23 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/interfaces.yang +31 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/model.py +120 -0
- pydantify_common-0.1.0/tests/examples/with_import_uses/namespaces.yang +31 -0
- pydantify_common-0.1.0/tests/test_examples.py +99 -0
- pydantify_common-0.1.0/uv.lock +653 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: test pydantify_common
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
linters:
|
|
7
|
+
name: linters
|
|
8
|
+
strategy:
|
|
9
|
+
matrix:
|
|
10
|
+
python-version: ["3.13"]
|
|
11
|
+
platform: [ubuntu-latest]
|
|
12
|
+
runs-on: ${{ matrix.platform }}
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Install uv and set the python version
|
|
17
|
+
uses: astral-sh/setup-uv@v6
|
|
18
|
+
with:
|
|
19
|
+
enable-cache: true
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
|
|
22
|
+
- name: Install the project
|
|
23
|
+
run: uv sync --locked --all-extras --dev
|
|
24
|
+
|
|
25
|
+
- name: Run ruff
|
|
26
|
+
run: make ruff
|
|
27
|
+
- name: Run mypy
|
|
28
|
+
run: make mypy
|
|
29
|
+
|
|
30
|
+
pytest:
|
|
31
|
+
name: Testing on Python ${{ matrix.python-version }} (${{ matrix.platform}})
|
|
32
|
+
defaults:
|
|
33
|
+
run:
|
|
34
|
+
shell: bash
|
|
35
|
+
strategy:
|
|
36
|
+
fail-fast: false
|
|
37
|
+
matrix:
|
|
38
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
39
|
+
# platform: [ubuntu-latest, macOS-latest, windows-latest]
|
|
40
|
+
platform: [ubuntu-latest, macOS-latest]
|
|
41
|
+
runs-on: ${{ matrix.platform }}
|
|
42
|
+
steps:
|
|
43
|
+
- uses: actions/checkout@v4
|
|
44
|
+
|
|
45
|
+
- name: Install uv and set the python version
|
|
46
|
+
uses: astral-sh/setup-uv@v6
|
|
47
|
+
with:
|
|
48
|
+
enable-cache: true
|
|
49
|
+
python-version: ${{ matrix.python-version }}
|
|
50
|
+
|
|
51
|
+
- name: Install the project
|
|
52
|
+
run: uv sync --locked --all-extras --dev
|
|
53
|
+
|
|
54
|
+
- name: Run pytest
|
|
55
|
+
run: make pytest
|
|
56
|
+
|
|
57
|
+
release:
|
|
58
|
+
name: Releasing to pypi
|
|
59
|
+
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
|
|
60
|
+
needs: [linters, pytest]
|
|
61
|
+
runs-on: ubuntu-latest
|
|
62
|
+
steps:
|
|
63
|
+
- uses: actions/checkout@v2
|
|
64
|
+
|
|
65
|
+
- name: Install uv and set the python version
|
|
66
|
+
uses: astral-sh/setup-uv@v6
|
|
67
|
+
with:
|
|
68
|
+
enable-cache: true
|
|
69
|
+
python-version: "3.13"
|
|
70
|
+
|
|
71
|
+
- name: build release
|
|
72
|
+
run: uv build
|
|
73
|
+
|
|
74
|
+
- name: Publish package to PyPI
|
|
75
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
76
|
+
with:
|
|
77
|
+
user: __token__
|
|
78
|
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
|
79
|
+
|
|
80
|
+
- uses: actions/upload-artifact@v5
|
|
81
|
+
with:
|
|
82
|
+
name: build
|
|
83
|
+
path: dist/*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.10
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
PROJECT=pydantify_common
|
|
2
|
+
CODE_DIRS=src/${PROJECT} tests
|
|
3
|
+
|
|
4
|
+
# Run pytest
|
|
5
|
+
.PHONY: pytest
|
|
6
|
+
pytest:
|
|
7
|
+
uv run pytest -vs ${ARGS}
|
|
8
|
+
|
|
9
|
+
# Check if the python code needs to be reformatted
|
|
10
|
+
.PHONY: ruff
|
|
11
|
+
black:
|
|
12
|
+
uv run ruff format --check ${CODE_DIRS}
|
|
13
|
+
|
|
14
|
+
# Python type check
|
|
15
|
+
.PHONY: mypy
|
|
16
|
+
mypy:
|
|
17
|
+
uv run mypy src/${PROJECT}
|
|
18
|
+
|
|
19
|
+
# Runn pytest, black and mypy
|
|
20
|
+
.PHONY: tests
|
|
21
|
+
tests: pytest black mypy
|
|
@@ -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,141 @@
|
|
|
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,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantify-common"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Pydantify common classes and functions"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "ubaumann", email = "github@m.ubaumann.ch" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"lxml>=6.0.2",
|
|
12
|
+
"pydantic>=2",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["hatchling"]
|
|
17
|
+
build-backend = "hatchling.build"
|
|
18
|
+
|
|
19
|
+
[dependency-groups]
|
|
20
|
+
dev = [
|
|
21
|
+
"lxml-stubs>=0.5.1",
|
|
22
|
+
"mypy>=1.19.1",
|
|
23
|
+
"pytest>=9.0.2",
|
|
24
|
+
"rich>=14.2.0",
|
|
25
|
+
"ruff>=0.11.12",
|
|
26
|
+
]
|
|
@@ -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,34 @@
|
|
|
1
|
+
# Augment
|
|
2
|
+
|
|
3
|
+
## tree
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
$ pyang -f tree configuration.yang namespaces.yang interfaces.yang
|
|
7
|
+
module: configuration
|
|
8
|
+
+--rw configuration
|
|
9
|
+
+--rw devicename string
|
|
10
|
+
+--rw ns:namespaces* [name]
|
|
11
|
+
+--rw ns:name string
|
|
12
|
+
+--rw if:interfaces* [name]
|
|
13
|
+
+--rw if:name string
|
|
14
|
+
+--rw if:ip? string
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## sample-xml-skeleton
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
$ pyang -f sample-xml-skeleton configuration.yang namespaces.yang interfaces.yang
|
|
21
|
+
<?xml version='1.0' encoding='UTF-8'?>
|
|
22
|
+
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
|
|
23
|
+
<configuration xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-configuration">
|
|
24
|
+
<devicename/>
|
|
25
|
+
<namespaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-namespaces">
|
|
26
|
+
<name/>
|
|
27
|
+
<interfaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
28
|
+
<name/>
|
|
29
|
+
<ip/>
|
|
30
|
+
</interfaces>
|
|
31
|
+
</namespaces>
|
|
32
|
+
</configuration>
|
|
33
|
+
</data>
|
|
34
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module configuration {
|
|
2
|
+
namespace "http://pydantify.github.io/ns/yang/pydantify-multimodel-configuration";
|
|
3
|
+
|
|
4
|
+
prefix configuration;
|
|
5
|
+
|
|
6
|
+
description
|
|
7
|
+
'Example using multimodel imports';
|
|
8
|
+
|
|
9
|
+
container configuration {
|
|
10
|
+
description
|
|
11
|
+
'Just a simple example of a container with uses.';
|
|
12
|
+
|
|
13
|
+
leaf devicename {
|
|
14
|
+
type string;
|
|
15
|
+
mandatory "true";
|
|
16
|
+
description
|
|
17
|
+
"Device name. Example value: sw01";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"configuration": {
|
|
3
|
+
"devicename": "sw01",
|
|
4
|
+
"namespaces": [
|
|
5
|
+
{
|
|
6
|
+
"name": "ns1",
|
|
7
|
+
"interfaces": [
|
|
8
|
+
{
|
|
9
|
+
"name": "GigabitEthernet0/0/0",
|
|
10
|
+
"ip": "::1"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "ns2",
|
|
16
|
+
"interfaces": [
|
|
17
|
+
{
|
|
18
|
+
"name": "GigabitEthernet0/0/1",
|
|
19
|
+
"ip": "::1"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "GigabitEthernet0/0/2"
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8'?>
|
|
2
|
+
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
|
|
3
|
+
<configuration xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-configuration">
|
|
4
|
+
<devicename>sw01</devicename>
|
|
5
|
+
<namespaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-namespaces">
|
|
6
|
+
<name>ns1</name>
|
|
7
|
+
<interfaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
8
|
+
<name>GigabitEthernet0/0/0</name>
|
|
9
|
+
<ip>::1</ip>
|
|
10
|
+
</interfaces>
|
|
11
|
+
</namespaces>
|
|
12
|
+
<namespaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-namespaces">
|
|
13
|
+
<name>ns2</name>
|
|
14
|
+
<interfaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
15
|
+
<name>GigabitEthernet0/0/1</name>
|
|
16
|
+
<ip>::1</ip>
|
|
17
|
+
</interfaces>
|
|
18
|
+
<interfaces xmlns="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
19
|
+
<name>GigabitEthernet0/0/2</name>
|
|
20
|
+
</interfaces>
|
|
21
|
+
</namespaces>
|
|
22
|
+
</configuration>
|
|
23
|
+
</data>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8'?>
|
|
2
|
+
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
|
|
3
|
+
<configuration:configuration xmlns:configuration="http://pydantify.github.io/ns/yang/pydantify-multimodel-configuration">
|
|
4
|
+
<configuration:devicename>sw01</configuration:devicename>
|
|
5
|
+
<ns:namespaces xmlns:ns="http://pydantify.github.io/ns/yang/pydantify-multimodel-namespaces">
|
|
6
|
+
<ns:name>ns1</ns:name>
|
|
7
|
+
<if:interfaces xmlns:if="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
8
|
+
<if:name>GigabitEthernet0/0/0</if:name>
|
|
9
|
+
<if:ip>::1</if:ip>
|
|
10
|
+
</if:interfaces>
|
|
11
|
+
</ns:namespaces>
|
|
12
|
+
<ns:namespaces xmlns:ns="http://pydantify.github.io/ns/yang/pydantify-multimodel-namespaces">
|
|
13
|
+
<ns:name>ns2</ns:name>
|
|
14
|
+
<if:interfaces xmlns:if="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
15
|
+
<if:name>GigabitEthernet0/0/1</if:name>
|
|
16
|
+
<if:ip>::1</if:ip>
|
|
17
|
+
</if:interfaces>
|
|
18
|
+
<if:interfaces xmlns:if="http://pydantify.github.io/ns/yang/pydantify-multimodel-interfaces">
|
|
19
|
+
<if:name>GigabitEthernet0/0/2</if:name>
|
|
20
|
+
</if:interfaces>
|
|
21
|
+
</ns:namespaces>
|
|
22
|
+
</configuration:configuration>
|
|
23
|
+
</data>
|