xyang 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.
- xyang-0.1.0/LICENSE +21 -0
- xyang-0.1.0/PKG-INFO +274 -0
- xyang-0.1.0/README.md +242 -0
- xyang-0.1.0/pyproject.toml +65 -0
- xyang-0.1.0/setup.cfg +4 -0
- xyang-0.1.0/setup.py +35 -0
- xyang-0.1.0/src/xyang/__init__.py +30 -0
- xyang-0.1.0/src/xyang/__main__.py +144 -0
- xyang-0.1.0/src/xyang/ast.py +291 -0
- xyang-0.1.0/src/xyang/augment_expand.py +131 -0
- xyang-0.1.0/src/xyang/errors.py +170 -0
- xyang-0.1.0/src/xyang/identity_graph.py +188 -0
- xyang-0.1.0/src/xyang/json/__init__.py +6 -0
- xyang-0.1.0/src/xyang/json/generator.py +1023 -0
- xyang-0.1.0/src/xyang/json/parser.py +890 -0
- xyang-0.1.0/src/xyang/json/schema_keys.py +109 -0
- xyang-0.1.0/src/xyang/module.py +59 -0
- xyang-0.1.0/src/xyang/parser/__init__.py +11 -0
- xyang-0.1.0/src/xyang/parser/parser_context.py +326 -0
- xyang-0.1.0/src/xyang/parser/statement_parsers.py +1145 -0
- xyang-0.1.0/src/xyang/parser/statement_registry.py +29 -0
- xyang-0.1.0/src/xyang/parser/tokenizer.py +198 -0
- xyang-0.1.0/src/xyang/parser/unsupported_skip.py +89 -0
- xyang-0.1.0/src/xyang/parser/yang_parser.py +508 -0
- xyang-0.1.0/src/xyang/refine_expand.py +217 -0
- xyang-0.1.0/src/xyang/types.py +272 -0
- xyang-0.1.0/src/xyang/uses_expand.py +207 -0
- xyang-0.1.0/src/xyang/validator/__init__.py +15 -0
- xyang-0.1.0/src/xyang/validator/document_validator.py +832 -0
- xyang-0.1.0/src/xyang/validator/if_feature_eval.py +220 -0
- xyang-0.1.0/src/xyang/validator/path_builder.py +28 -0
- xyang-0.1.0/src/xyang/validator/type_checker.py +414 -0
- xyang-0.1.0/src/xyang/validator/validation_error.py +41 -0
- xyang-0.1.0/src/xyang/validator/yang_validator.py +55 -0
- xyang-0.1.0/src/xyang/xpath/__init__.py +59 -0
- xyang-0.1.0/src/xyang/xpath/ast.py +91 -0
- xyang-0.1.0/src/xyang/xpath/evaluator.py +324 -0
- xyang-0.1.0/src/xyang/xpath/functions.py +213 -0
- xyang-0.1.0/src/xyang/xpath/node.py +57 -0
- xyang-0.1.0/src/xyang/xpath/parser.py +328 -0
- xyang-0.1.0/src/xyang/xpath/schema_nav.py +102 -0
- xyang-0.1.0/src/xyang/xpath/tokenizer.py +177 -0
- xyang-0.1.0/src/xyang/xpath/tokens.py +34 -0
- xyang-0.1.0/src/xyang/xpath/utils.py +158 -0
- xyang-0.1.0/src/xyang.egg-info/PKG-INFO +274 -0
- xyang-0.1.0/src/xyang.egg-info/SOURCES.txt +89 -0
- xyang-0.1.0/src/xyang.egg-info/dependency_links.txt +1 -0
- xyang-0.1.0/src/xyang.egg-info/entry_points.txt +2 -0
- xyang-0.1.0/src/xyang.egg-info/requires.txt +5 -0
- xyang-0.1.0/src/xyang.egg-info/top_level.txt +1 -0
- xyang-0.1.0/tests/test_absolute_paths.py +215 -0
- xyang-0.1.0/tests/test_anydata_anyxml.py +79 -0
- xyang-0.1.0/tests/test_augment.py +148 -0
- xyang-0.1.0/tests/test_basic.py +211 -0
- xyang-0.1.0/tests/test_binary_type.py +50 -0
- xyang-0.1.0/tests/test_bits.py +166 -0
- xyang-0.1.0/tests/test_choice_case.py +662 -0
- xyang-0.1.0/tests/test_choice_case_when.py +71 -0
- xyang-0.1.0/tests/test_circular_uses.py +63 -0
- xyang-0.1.0/tests/test_coercion.py +348 -0
- xyang-0.1.0/tests/test_computed_fields.py +342 -0
- xyang-0.1.0/tests/test_current_context_in_predicate.py +213 -0
- xyang-0.1.0/tests/test_deref_standalone.py +238 -0
- xyang-0.1.0/tests/test_duplicate_must_bug.py +86 -0
- xyang-0.1.0/tests/test_enum_validation.py +142 -0
- xyang-0.1.0/tests/test_generic_field_example.py +25 -0
- xyang-0.1.0/tests/test_grouping_uses.py +563 -0
- xyang-0.1.0/tests/test_identity.py +204 -0
- xyang-0.1.0/tests/test_if_feature.py +653 -0
- xyang-0.1.0/tests/test_instance_identifier.py +113 -0
- xyang-0.1.0/tests/test_leaf_list_current.py +270 -0
- xyang-0.1.0/tests/test_leaf_must_relative_path.py +62 -0
- xyang-0.1.0/tests/test_leafref_relative_path.py +234 -0
- xyang-0.1.0/tests/test_list_key_uniqueness.py +77 -0
- xyang-0.1.0/tests/test_must_on_leafref_list.py +613 -0
- xyang-0.1.0/tests/test_must_resolver_minimal.py +184 -0
- xyang-0.1.0/tests/test_nested_choice_under_case.py +161 -0
- xyang-0.1.0/tests/test_nested_choice_with_refine.py +114 -0
- xyang-0.1.0/tests/test_nested_uses.py +165 -0
- xyang-0.1.0/tests/test_refine_list.py +260 -0
- xyang-0.1.0/tests/test_refine_mandatory.py +32 -0
- xyang-0.1.0/tests/test_refine_target_not_found.py +53 -0
- xyang-0.1.0/tests/test_single_parse.py +247 -0
- xyang-0.1.0/tests/test_typedef_union.py +371 -0
- xyang-0.1.0/tests/test_unsupported_skip.py +70 -0
- xyang-0.1.0/tests/test_when.py +288 -0
- xyang-0.1.0/tests/test_xpath_caching.py +235 -0
- xyang-0.1.0/tests/test_xpath_parser.py +181 -0
- xyang-0.1.0/tests/test_xpath_value_list.py +45 -0
- xyang-0.1.0/tests/test_yang_tokenizer_line_comments.py +56 -0
- xyang-0.1.0/tests/test_yangson_ex3_import.py +132 -0
xyang-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 exergy-connect
|
|
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.
|
xyang-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xyang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library implementing a subset of YANG features focused on constraint validation
|
|
5
|
+
Home-page: https://github.com/exergy-connect/xYang
|
|
6
|
+
Author: Exergy LLC
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/exergy-connect/xYang
|
|
9
|
+
Project-URL: Repository, https://github.com/exergy-connect/xYang.git
|
|
10
|
+
Project-URL: Issues, https://github.com/exergy-connect/xYang/issues
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: System :: Networking
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=9.0.3; extra == "dev"
|
|
27
|
+
Requires-Dist: black>=26.3.1; extra == "dev"
|
|
28
|
+
Requires-Dist: PyYAML>=6.0.3; extra == "dev"
|
|
29
|
+
Dynamic: home-page
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
Dynamic: requires-python
|
|
32
|
+
|
|
33
|
+
# xYang
|
|
34
|
+
|
|
35
|
+
**YANG models, validated JSON, and JSON Schema—without dragging in a full management stack.**
|
|
36
|
+
|
|
37
|
+
xYang is a pure-Python library and CLI for **parsing YANG 1.1-shaped modules**, **validating instance data** against the full set of **RFC 7950 built-in types** (each checked with the right rules—ranges, `length`/`pattern`, base64 **`binary`**, **`bits`**, **`union`** disambiguation, **`leafref`** / **`identityref`** / **`instance-identifier`** resolution, and the rest), plus **`must`**, **`when`**, **`if-feature`**, and structure. It **exports** a standards-friendly **JSON Schema (2020-12)** layer augmented with **`x-yang`** metadata for round-trip where supported. It is built for real modules (reference design: [`examples/meta-model.yang`](examples/meta-model.yang)), including **deep** XPath: **`deref()`** tied to schema paths, **`union`** typing, and **`current()`** in list and leaf-list contexts.
|
|
38
|
+
|
|
39
|
+
- **Zero required runtime dependencies** — drop into apps, agents, and pipelines with minimal footprint.
|
|
40
|
+
- **Honest scope** — not every RFC 7950 statement is modeled; what *is* implemented is described precisely in [**FEATURES.md**](FEATURES.md) (including `import` / `include`, `if-feature`, `anydata` / `anyxml`, and JSON `if-features` round-trip). Constructs such as `rpc`, `notification`, and `deviation` are **recognized and skipped** with a log warning so mixed modules still parse.
|
|
41
|
+
- **MIT licensed** — use it in products and internal tools alike.
|
|
42
|
+
|
|
43
|
+
**Repository:** [github.com/exergy-connect/xYang](https://github.com/exergy-connect/xYang) · **Issues:** [github.com/exergy-connect/xYang/issues](https://github.com/exergy-connect/xYang/issues)
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Features (overview)
|
|
48
|
+
|
|
49
|
+
The list below is the short version; [**FEATURES.md**](FEATURES.md) is the authoritative, line-by-line feature matrix and documents the **YANG.json** hybrid format.
|
|
50
|
+
|
|
51
|
+
- **Module structure**: `module` / `submodule`, `yang-version`, `namespace`, `prefix`, metadata, `revision`, `import`, `include`, `feature`
|
|
52
|
+
- **Types**: **All RFC 7950 built-in types** (Section 4.2.4) are **supported and validated** on instance data—`string`, `binary`, numeric types, `decimal64`, `boolean`, `empty`, `enumeration`, `bits`, `union`, `leafref`, `identityref`, and `instance-identifier`—with applicable substatements (`length`, `range`, `pattern`, `fraction-digits`, `require-instance`, etc.). **`typedef`** and imports compose these. Statement-level scope (e.g. skipped `rpc` / `notification`) and JSON Schema details: **FEATURES.md**.
|
|
53
|
+
- **Data nodes**: `container`, `list` + `key`, `leaf`, `leaf-list`, `choice` / `case`, `anydata` / `anyxml`, `grouping` / `uses` / `refine`, `augment` (merge when uses expansion is enabled)
|
|
54
|
+
- **Constraints**: `must`, `when`, `if-feature`, `mandatory`, `default`, `min-elements` / `max-elements`, `pattern`, `length`, `range`, `fraction-digits`
|
|
55
|
+
- **References**: `leafref` (+ `require-instance`), `instance-identifier`, `identityref`, `identity` / `base`, XPath `derived-from()` / `derived-from-or-self()`
|
|
56
|
+
- **Interop**: **`xyang`** CLI (`parse`, `validate`, `convert`) and **JSON Schema** export with **`x-yang`** annotations for generator/parser round-trip where supported
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
**From PyPI** (when published):
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install xyang
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**From a checkout** (editable, for development):
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install -e .
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
There are **no required runtime dependencies**. For **`xyang validate`** with **`.yaml` / `.yml`** instance files, install **PyYAML** (`pip install PyYAML` or `pip install -e ".[dev]"`).
|
|
75
|
+
|
|
76
|
+
**Requirements:** Python **≥ 3.9** (see `pyproject.toml`).
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
### Command-line (`xyang`)
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
xyang -h # help
|
|
86
|
+
xyang parse <file.yang> # print module info
|
|
87
|
+
xyang validate <file.yang> [data.json] # or .yaml/.yml (needs PyYAML); omit file → JSON from stdin
|
|
88
|
+
xyang convert <file.yang> [-o path] # YANG → .yang.json (output path ends with .yang.json)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Without installing the package, from the repo root: `PYTHONPATH=src python3 -m xyang -h`
|
|
92
|
+
|
|
93
|
+
### Parsing a YANG module
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from xyang import parse_yang_file, parse_yang_string
|
|
97
|
+
|
|
98
|
+
# Parse from file
|
|
99
|
+
module = parse_yang_file("examples/meta-model.yang")
|
|
100
|
+
|
|
101
|
+
# Parse from string
|
|
102
|
+
yang_content = """
|
|
103
|
+
module example {
|
|
104
|
+
yang-version 1.1;
|
|
105
|
+
namespace "urn:example";
|
|
106
|
+
prefix "ex";
|
|
107
|
+
|
|
108
|
+
container data {
|
|
109
|
+
leaf name {
|
|
110
|
+
type string;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
"""
|
|
115
|
+
module = parse_yang_string(yang_content)
|
|
116
|
+
|
|
117
|
+
print(f"Module: {module.name}")
|
|
118
|
+
print(f"Namespace: {module.namespace}")
|
|
119
|
+
print(f"Prefix: {module.prefix}")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Validating data
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from xyang import parse_yang_file, YangValidator
|
|
126
|
+
|
|
127
|
+
module = parse_yang_file("examples/meta-model.yang")
|
|
128
|
+
validator = YangValidator(module)
|
|
129
|
+
|
|
130
|
+
# Consolidated JSON document: one tree matching your module’s data layout.
|
|
131
|
+
# XPath comparisons use schema-aware coercion (e.g. string "true" vs boolean leaves).
|
|
132
|
+
data = {
|
|
133
|
+
"data-model": {
|
|
134
|
+
"name": "example",
|
|
135
|
+
"entities": [
|
|
136
|
+
{
|
|
137
|
+
"name": "server",
|
|
138
|
+
"fields": [
|
|
139
|
+
{"name": "id", "type": "string"}
|
|
140
|
+
]
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
is_valid, errors, warnings = validator.validate(data)
|
|
147
|
+
if not is_valid:
|
|
148
|
+
for error in errors:
|
|
149
|
+
print(f"Error: {error}")
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Working with types
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from xyang import TypeConstraint, TypeSystem
|
|
156
|
+
|
|
157
|
+
type_system = TypeSystem()
|
|
158
|
+
constraint = TypeConstraint(
|
|
159
|
+
pattern=r'[a-z_][a-z0-9_]*',
|
|
160
|
+
length="1..64"
|
|
161
|
+
)
|
|
162
|
+
type_system.register_typedef("entity-name", "string", constraint)
|
|
163
|
+
|
|
164
|
+
is_valid, error = type_system.validate("server_name", "entity-name")
|
|
165
|
+
print(f"Valid: {is_valid}")
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Converting YANG → JSON Schema (`.yang.json`)
|
|
169
|
+
|
|
170
|
+
Valid **JSON Schema** for structure and types; YANG-only rules (`must`, `when`, leafref paths, `if-features`, …) ride in **`x-yang`**. Details: [FEATURES.md — YANG.json hybrid format](FEATURES.md#yangjson-hybrid-format).
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from xyang.parser import YangParser
|
|
174
|
+
from xyang.json import schema_to_yang_json
|
|
175
|
+
|
|
176
|
+
parser = YangParser(expand_uses=False)
|
|
177
|
+
module = parser.parse_file("examples/meta-model.yang")
|
|
178
|
+
schema_to_yang_json(module, output_path="meta-model.yang.json")
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
CLI: `xyang convert examples/meta-model.yang -o meta-model.yang.json`
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Project layout
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
xYang/
|
|
189
|
+
├── src/xyang/
|
|
190
|
+
│ ├── __init__.py # Package exports
|
|
191
|
+
│ ├── __main__.py # CLI (parse, validate, convert)
|
|
192
|
+
│ ├── parser/ # YANG parser (incl. unsupported-statement skip)
|
|
193
|
+
│ ├── json/ # JSON Schema generator + parser
|
|
194
|
+
│ ├── validator/ # Document validation
|
|
195
|
+
│ ├── xpath/ # XPath for must/when
|
|
196
|
+
│ ├── ast.py # AST nodes
|
|
197
|
+
│ ├── types.py # Type system
|
|
198
|
+
│ ├── module.py # Module model
|
|
199
|
+
│ └── errors.py
|
|
200
|
+
├── examples/ # meta-model.yang, samples, generated .yang.json
|
|
201
|
+
├── tests/
|
|
202
|
+
├── benchmarks/
|
|
203
|
+
├── FEATURES.md # Full feature list & format spec
|
|
204
|
+
├── pyproject.toml
|
|
205
|
+
└── README.md
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## XPath (schema-aware)
|
|
211
|
+
|
|
212
|
+
Coverage matches what **meta-model.yang** needs, evaluated with **schema context** (not a generic XPath 1.0 engine):
|
|
213
|
+
|
|
214
|
+
- **Paths**: `../field`, `../../field`, absolute paths such as `/data-model/entities`
|
|
215
|
+
- **Functions**: `string()`, `number()`, `concat()`, `string-length()`, `translate()`, `count()`, **`deref()`**, **`current()`**, `not()`, `true()`, `false()`, `boolean()`, `derived-from()`, `derived-from-or-self()`, …
|
|
216
|
+
- **Comparisons & logic**: `=`, `!=`, `<=`, `>=`, `<`, `>`, `and`, `or`
|
|
217
|
+
- **Literal sequences** (xYang extension): RHS `('a', 'b')` for membership-style equality
|
|
218
|
+
- **Predicates & indexing**: e.g. `[name = current()]`, `[1]`
|
|
219
|
+
- **String concat**: `+` between strings in expressions
|
|
220
|
+
|
|
221
|
+
**`deref()`** on leafref values follows the **leafref’s schema path** to resolve the target node; it supports nesting, caching, and cycle detection for the patterns used in production modules here.
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## When & must (examples)
|
|
226
|
+
|
|
227
|
+
**When** — if the condition is false, the node is out of the effective schema; data there is reported as invalid:
|
|
228
|
+
|
|
229
|
+
```yang
|
|
230
|
+
container item_type {
|
|
231
|
+
when "../type = 'array'";
|
|
232
|
+
leaf primitive { type string; }
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Must** — XPath must evaluate true or validation fails (with `error-message` when provided):
|
|
237
|
+
|
|
238
|
+
```yang
|
|
239
|
+
leaf minDate {
|
|
240
|
+
type date;
|
|
241
|
+
must "not(../maxDate) or . <= ../maxDate" {
|
|
242
|
+
error-message "minDate must be less than or equal to maxDate";
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Scope & limitations
|
|
250
|
+
|
|
251
|
+
- **Single JSON instance** — validation is against one consolidated document, not NETCONF/XML fragments or incremental edits.
|
|
252
|
+
- **XPath subset** — unsupported expressions fail at XPath parse time (`UnsupportedXPathError`). Extend the evaluator to add features.
|
|
253
|
+
- **`deref()`** — fully handled for meta-model-style patterns; it remains **schema-coupled** by design, not a standalone generic resolver.
|
|
254
|
+
- **RFC surface** — see [**FEATURES.md**](FEATURES.md) for what is partial, skipped, or out of scope; the parser warns when it skips unsupported top-level-like statements.
|
|
255
|
+
|
|
256
|
+
## Design choices
|
|
257
|
+
|
|
258
|
+
**No mandatory third-party stack** — core package dependencies are empty in `pyproject.toml`; optional **PyYAML** only for YAML instances on the CLI. That keeps xYang easy to embed and audit.
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Development
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
pip install -e ".[dev]"
|
|
266
|
+
pytest
|
|
267
|
+
black src/xyang/
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT License
|
xyang-0.1.0/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# xYang
|
|
2
|
+
|
|
3
|
+
**YANG models, validated JSON, and JSON Schema—without dragging in a full management stack.**
|
|
4
|
+
|
|
5
|
+
xYang is a pure-Python library and CLI for **parsing YANG 1.1-shaped modules**, **validating instance data** against the full set of **RFC 7950 built-in types** (each checked with the right rules—ranges, `length`/`pattern`, base64 **`binary`**, **`bits`**, **`union`** disambiguation, **`leafref`** / **`identityref`** / **`instance-identifier`** resolution, and the rest), plus **`must`**, **`when`**, **`if-feature`**, and structure. It **exports** a standards-friendly **JSON Schema (2020-12)** layer augmented with **`x-yang`** metadata for round-trip where supported. It is built for real modules (reference design: [`examples/meta-model.yang`](examples/meta-model.yang)), including **deep** XPath: **`deref()`** tied to schema paths, **`union`** typing, and **`current()`** in list and leaf-list contexts.
|
|
6
|
+
|
|
7
|
+
- **Zero required runtime dependencies** — drop into apps, agents, and pipelines with minimal footprint.
|
|
8
|
+
- **Honest scope** — not every RFC 7950 statement is modeled; what *is* implemented is described precisely in [**FEATURES.md**](FEATURES.md) (including `import` / `include`, `if-feature`, `anydata` / `anyxml`, and JSON `if-features` round-trip). Constructs such as `rpc`, `notification`, and `deviation` are **recognized and skipped** with a log warning so mixed modules still parse.
|
|
9
|
+
- **MIT licensed** — use it in products and internal tools alike.
|
|
10
|
+
|
|
11
|
+
**Repository:** [github.com/exergy-connect/xYang](https://github.com/exergy-connect/xYang) · **Issues:** [github.com/exergy-connect/xYang/issues](https://github.com/exergy-connect/xYang/issues)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Features (overview)
|
|
16
|
+
|
|
17
|
+
The list below is the short version; [**FEATURES.md**](FEATURES.md) is the authoritative, line-by-line feature matrix and documents the **YANG.json** hybrid format.
|
|
18
|
+
|
|
19
|
+
- **Module structure**: `module` / `submodule`, `yang-version`, `namespace`, `prefix`, metadata, `revision`, `import`, `include`, `feature`
|
|
20
|
+
- **Types**: **All RFC 7950 built-in types** (Section 4.2.4) are **supported and validated** on instance data—`string`, `binary`, numeric types, `decimal64`, `boolean`, `empty`, `enumeration`, `bits`, `union`, `leafref`, `identityref`, and `instance-identifier`—with applicable substatements (`length`, `range`, `pattern`, `fraction-digits`, `require-instance`, etc.). **`typedef`** and imports compose these. Statement-level scope (e.g. skipped `rpc` / `notification`) and JSON Schema details: **FEATURES.md**.
|
|
21
|
+
- **Data nodes**: `container`, `list` + `key`, `leaf`, `leaf-list`, `choice` / `case`, `anydata` / `anyxml`, `grouping` / `uses` / `refine`, `augment` (merge when uses expansion is enabled)
|
|
22
|
+
- **Constraints**: `must`, `when`, `if-feature`, `mandatory`, `default`, `min-elements` / `max-elements`, `pattern`, `length`, `range`, `fraction-digits`
|
|
23
|
+
- **References**: `leafref` (+ `require-instance`), `instance-identifier`, `identityref`, `identity` / `base`, XPath `derived-from()` / `derived-from-or-self()`
|
|
24
|
+
- **Interop**: **`xyang`** CLI (`parse`, `validate`, `convert`) and **JSON Schema** export with **`x-yang`** annotations for generator/parser round-trip where supported
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
**From PyPI** (when published):
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install xyang
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**From a checkout** (editable, for development):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install -e .
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
There are **no required runtime dependencies**. For **`xyang validate`** with **`.yaml` / `.yml`** instance files, install **PyYAML** (`pip install PyYAML` or `pip install -e ".[dev]"`).
|
|
43
|
+
|
|
44
|
+
**Requirements:** Python **≥ 3.9** (see `pyproject.toml`).
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Command-line (`xyang`)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
xyang -h # help
|
|
54
|
+
xyang parse <file.yang> # print module info
|
|
55
|
+
xyang validate <file.yang> [data.json] # or .yaml/.yml (needs PyYAML); omit file → JSON from stdin
|
|
56
|
+
xyang convert <file.yang> [-o path] # YANG → .yang.json (output path ends with .yang.json)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Without installing the package, from the repo root: `PYTHONPATH=src python3 -m xyang -h`
|
|
60
|
+
|
|
61
|
+
### Parsing a YANG module
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from xyang import parse_yang_file, parse_yang_string
|
|
65
|
+
|
|
66
|
+
# Parse from file
|
|
67
|
+
module = parse_yang_file("examples/meta-model.yang")
|
|
68
|
+
|
|
69
|
+
# Parse from string
|
|
70
|
+
yang_content = """
|
|
71
|
+
module example {
|
|
72
|
+
yang-version 1.1;
|
|
73
|
+
namespace "urn:example";
|
|
74
|
+
prefix "ex";
|
|
75
|
+
|
|
76
|
+
container data {
|
|
77
|
+
leaf name {
|
|
78
|
+
type string;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
"""
|
|
83
|
+
module = parse_yang_string(yang_content)
|
|
84
|
+
|
|
85
|
+
print(f"Module: {module.name}")
|
|
86
|
+
print(f"Namespace: {module.namespace}")
|
|
87
|
+
print(f"Prefix: {module.prefix}")
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Validating data
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from xyang import parse_yang_file, YangValidator
|
|
94
|
+
|
|
95
|
+
module = parse_yang_file("examples/meta-model.yang")
|
|
96
|
+
validator = YangValidator(module)
|
|
97
|
+
|
|
98
|
+
# Consolidated JSON document: one tree matching your module’s data layout.
|
|
99
|
+
# XPath comparisons use schema-aware coercion (e.g. string "true" vs boolean leaves).
|
|
100
|
+
data = {
|
|
101
|
+
"data-model": {
|
|
102
|
+
"name": "example",
|
|
103
|
+
"entities": [
|
|
104
|
+
{
|
|
105
|
+
"name": "server",
|
|
106
|
+
"fields": [
|
|
107
|
+
{"name": "id", "type": "string"}
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
is_valid, errors, warnings = validator.validate(data)
|
|
115
|
+
if not is_valid:
|
|
116
|
+
for error in errors:
|
|
117
|
+
print(f"Error: {error}")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Working with types
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from xyang import TypeConstraint, TypeSystem
|
|
124
|
+
|
|
125
|
+
type_system = TypeSystem()
|
|
126
|
+
constraint = TypeConstraint(
|
|
127
|
+
pattern=r'[a-z_][a-z0-9_]*',
|
|
128
|
+
length="1..64"
|
|
129
|
+
)
|
|
130
|
+
type_system.register_typedef("entity-name", "string", constraint)
|
|
131
|
+
|
|
132
|
+
is_valid, error = type_system.validate("server_name", "entity-name")
|
|
133
|
+
print(f"Valid: {is_valid}")
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Converting YANG → JSON Schema (`.yang.json`)
|
|
137
|
+
|
|
138
|
+
Valid **JSON Schema** for structure and types; YANG-only rules (`must`, `when`, leafref paths, `if-features`, …) ride in **`x-yang`**. Details: [FEATURES.md — YANG.json hybrid format](FEATURES.md#yangjson-hybrid-format).
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from xyang.parser import YangParser
|
|
142
|
+
from xyang.json import schema_to_yang_json
|
|
143
|
+
|
|
144
|
+
parser = YangParser(expand_uses=False)
|
|
145
|
+
module = parser.parse_file("examples/meta-model.yang")
|
|
146
|
+
schema_to_yang_json(module, output_path="meta-model.yang.json")
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
CLI: `xyang convert examples/meta-model.yang -o meta-model.yang.json`
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Project layout
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
xYang/
|
|
157
|
+
├── src/xyang/
|
|
158
|
+
│ ├── __init__.py # Package exports
|
|
159
|
+
│ ├── __main__.py # CLI (parse, validate, convert)
|
|
160
|
+
│ ├── parser/ # YANG parser (incl. unsupported-statement skip)
|
|
161
|
+
│ ├── json/ # JSON Schema generator + parser
|
|
162
|
+
│ ├── validator/ # Document validation
|
|
163
|
+
│ ├── xpath/ # XPath for must/when
|
|
164
|
+
│ ├── ast.py # AST nodes
|
|
165
|
+
│ ├── types.py # Type system
|
|
166
|
+
│ ├── module.py # Module model
|
|
167
|
+
│ └── errors.py
|
|
168
|
+
├── examples/ # meta-model.yang, samples, generated .yang.json
|
|
169
|
+
├── tests/
|
|
170
|
+
├── benchmarks/
|
|
171
|
+
├── FEATURES.md # Full feature list & format spec
|
|
172
|
+
├── pyproject.toml
|
|
173
|
+
└── README.md
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## XPath (schema-aware)
|
|
179
|
+
|
|
180
|
+
Coverage matches what **meta-model.yang** needs, evaluated with **schema context** (not a generic XPath 1.0 engine):
|
|
181
|
+
|
|
182
|
+
- **Paths**: `../field`, `../../field`, absolute paths such as `/data-model/entities`
|
|
183
|
+
- **Functions**: `string()`, `number()`, `concat()`, `string-length()`, `translate()`, `count()`, **`deref()`**, **`current()`**, `not()`, `true()`, `false()`, `boolean()`, `derived-from()`, `derived-from-or-self()`, …
|
|
184
|
+
- **Comparisons & logic**: `=`, `!=`, `<=`, `>=`, `<`, `>`, `and`, `or`
|
|
185
|
+
- **Literal sequences** (xYang extension): RHS `('a', 'b')` for membership-style equality
|
|
186
|
+
- **Predicates & indexing**: e.g. `[name = current()]`, `[1]`
|
|
187
|
+
- **String concat**: `+` between strings in expressions
|
|
188
|
+
|
|
189
|
+
**`deref()`** on leafref values follows the **leafref’s schema path** to resolve the target node; it supports nesting, caching, and cycle detection for the patterns used in production modules here.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## When & must (examples)
|
|
194
|
+
|
|
195
|
+
**When** — if the condition is false, the node is out of the effective schema; data there is reported as invalid:
|
|
196
|
+
|
|
197
|
+
```yang
|
|
198
|
+
container item_type {
|
|
199
|
+
when "../type = 'array'";
|
|
200
|
+
leaf primitive { type string; }
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**Must** — XPath must evaluate true or validation fails (with `error-message` when provided):
|
|
205
|
+
|
|
206
|
+
```yang
|
|
207
|
+
leaf minDate {
|
|
208
|
+
type date;
|
|
209
|
+
must "not(../maxDate) or . <= ../maxDate" {
|
|
210
|
+
error-message "minDate must be less than or equal to maxDate";
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## Scope & limitations
|
|
218
|
+
|
|
219
|
+
- **Single JSON instance** — validation is against one consolidated document, not NETCONF/XML fragments or incremental edits.
|
|
220
|
+
- **XPath subset** — unsupported expressions fail at XPath parse time (`UnsupportedXPathError`). Extend the evaluator to add features.
|
|
221
|
+
- **`deref()`** — fully handled for meta-model-style patterns; it remains **schema-coupled** by design, not a standalone generic resolver.
|
|
222
|
+
- **RFC surface** — see [**FEATURES.md**](FEATURES.md) for what is partial, skipped, or out of scope; the parser warns when it skips unsupported top-level-like statements.
|
|
223
|
+
|
|
224
|
+
## Design choices
|
|
225
|
+
|
|
226
|
+
**No mandatory third-party stack** — core package dependencies are empty in `pyproject.toml`; optional **PyYAML** only for YAML instances on the CLI. That keeps xYang easy to embed and audit.
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## Development
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install -e ".[dev]"
|
|
234
|
+
pytest
|
|
235
|
+
black src/xyang/
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT License
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "xyang"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A Python library implementing a subset of YANG features focused on constraint validation"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{name = "Exergy LLC"}
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Topic :: System :: Networking",
|
|
28
|
+
]
|
|
29
|
+
dependencies = []
|
|
30
|
+
# No runtime dependencies - pure Python implementation
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
# pytest 9 / black 26 need Python >= 3.10 to run; use pytest 8.x on 3.9 (see CI).
|
|
34
|
+
dev = [
|
|
35
|
+
"pytest>=9.0.3",
|
|
36
|
+
"black>=26.3.1",
|
|
37
|
+
"PyYAML>=6.0.3",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/exergy-connect/xYang"
|
|
42
|
+
Repository = "https://github.com/exergy-connect/xYang.git"
|
|
43
|
+
Issues = "https://github.com/exergy-connect/xYang/issues"
|
|
44
|
+
|
|
45
|
+
[project.scripts]
|
|
46
|
+
xyang = "xyang.__main__:main"
|
|
47
|
+
|
|
48
|
+
[tool.setuptools.package-dir]
|
|
49
|
+
"" = "src"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.packages.find]
|
|
52
|
+
where = ["src"]
|
|
53
|
+
|
|
54
|
+
[tool.pyright]
|
|
55
|
+
extraPaths = ["src"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
python_files = ["test_*.py"]
|
|
60
|
+
python_classes = ["Test*"]
|
|
61
|
+
python_functions = ["test_*"]
|
|
62
|
+
|
|
63
|
+
[tool.black]
|
|
64
|
+
line-length = 120
|
|
65
|
+
target-version = ["py39", "py310", "py311", "py312", "py313"]
|
xyang-0.1.0/setup.cfg
ADDED
xyang-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Setup script for xYang library.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from setuptools import setup, find_packages
|
|
6
|
+
|
|
7
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
|
8
|
+
long_description = fh.read()
|
|
9
|
+
|
|
10
|
+
setup(
|
|
11
|
+
name="xyang",
|
|
12
|
+
version="0.1.0",
|
|
13
|
+
author="Exergy LLC",
|
|
14
|
+
description="A Python library implementing a subset of YANG features focused on constraint validation",
|
|
15
|
+
long_description=long_description,
|
|
16
|
+
long_description_content_type="text/markdown",
|
|
17
|
+
url="https://github.com/exergy-connect/xYang",
|
|
18
|
+
packages=find_packages(where="src"),
|
|
19
|
+
package_dir={"": "src"},
|
|
20
|
+
classifiers=[
|
|
21
|
+
"Development Status :: 3 - Alpha",
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.9",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Programming Language :: Python :: 3.13",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
"Topic :: System :: Networking",
|
|
32
|
+
],
|
|
33
|
+
python_requires=">=3.9",
|
|
34
|
+
install_requires=[], # No dependencies - pure Python; dev deps live in pyproject.toml
|
|
35
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""
|
|
2
|
+
xYang2 - YANG parsing and validation using xpath.
|
|
3
|
+
|
|
4
|
+
Based on xYang; uses xpath for parsing must/when expressions and for
|
|
5
|
+
document validation (when, structure, must, type checks).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .errors import (
|
|
9
|
+
YangCircularUsesError,
|
|
10
|
+
YangRefineTargetNotFoundError,
|
|
11
|
+
YangSemanticError,
|
|
12
|
+
)
|
|
13
|
+
from .parser import parse_yang_file, parse_yang_string
|
|
14
|
+
from .validator.yang_validator import YangValidator
|
|
15
|
+
|
|
16
|
+
# Re-export for compatibility
|
|
17
|
+
from .module import YangModule
|
|
18
|
+
from .types import TypeConstraint, TypeSystem
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"parse_yang_file",
|
|
22
|
+
"parse_yang_string",
|
|
23
|
+
"YangModule",
|
|
24
|
+
"YangValidator",
|
|
25
|
+
"TypeConstraint",
|
|
26
|
+
"TypeSystem",
|
|
27
|
+
"YangSemanticError",
|
|
28
|
+
"YangCircularUsesError",
|
|
29
|
+
"YangRefineTargetNotFoundError",
|
|
30
|
+
]
|