Opinionated Python bindings for the libyang library
Project description
Opinionated Python bindings for the libyang
library
Python bindings and packaging of libyang
.
We're focusing on parsing, validating and accessing YANG-modeled JSON data trees.
Essentially, just enough to get gnpy
going.
Want more?
Patches welcome.
Compared to the CFFI libyang bindings, this wrapper takes care of low-level memory management.
This means no more node.free()
and ctx.destroy()
.
We also produce prebuilt binary wheels to make installation very simple.
Usage
Loading YANG data
import oopt_gnpy_libyang as ly
c = ly.Context('tests/yang', ly.ContextOptions.AllImplemented | ly.ContextOptions.NoYangLibrary)
for m in ('iana-if-type', 'ietf-interfaces', 'ietf-ip'):
c.load_module(m)
blob = '''{
"ietf-interfaces:interfaces": {
"interface": [
{
"name": "lo",
"type": "iana-if-type:softwareLoopback",
"ietf-ip:ipv4": {
"address": [
{
"ip": "127.0.0.1",
"prefix-length": 8
}
]
},
"ietf-ip:ipv6": {
"address": [
{
"ip": "::1",
"prefix-length": 128
}
]
}
},
{
"name": "eth0",
"type": "iana-if-type:ethernetCsmacd"
}
]
}
}'''
data = c.parse_data(blob,
ly.DataFormat.JSON, ly.ParseOptions.Strict | ly.ParseOptions.Ordered,
ly.ValidationOptions.Present | ly.ValidationOptions.NoState)
Working with data
Libyang works with forests (sets of trees), this is how to process all the data:
for x in data.siblings():
print(f'a sibling: {x.path}')
for xx in x.childrenDfs():
print(f' {"term " if xx.is_term else "child"}: {xx.path}')
if xx.is_term:
print(f' {xx.as_term()} {" (default)" if xx.as_term().is_default_value else ""}')
Data can be accessed via their known paths, of course. Either as a full, multi-level XPath:
data["interface[name='lo']/ietf-ip:ipv6/address[ip='::1']/prefix-length"].as_term().value == 128
Or individually, one item per index:
data["interface[name='lo']"]["ietf-ip:ipv6"]["address[ip='::1']"]["prefix-length"].as_term().value
Everything is an XPath, so it's possible to take a shortcut and skip specifying keys for single-element lists:
data["interface[name='lo']"]["ietf-ip:ipv6"]["address"]["prefix-length"].as_term().value == 128
The data are provided as native Python types:
type(data["interface[name='lo']"]["ietf-ip:ipv6"]["address"]["prefix-length"]
.as_term().value) == int
Validation errors
In libyang, if an operation fails, error details are available via context.errors()
:
import json
wrong = json.loads(blob)
wrong["ietf-interfaces:interfaces"]["interface"][0]\
["ietf-ip:ipv6"]["address"][0]["prefix-length"] = 666
try:
data = c.parse_data(json.dumps(wrong),
ly.DataFormat.JSON, ly.ParseOptions.Strict | ly.ParseOptions.Ordered,
ly.ValidationOptions.Present | ly.ValidationOptions.NoState)
assert False
except ly.Error:
for error in c.errors():
assert error.path == "Schema location \"/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address/prefix-length\", data location \"/ietf-ip:address[ip='::1']\", line number 1."
assert error.message == 'Value "666" is out of type uint8 min/max bounds.'
Installing
We're producing wheels for many popular platforms. The installation is as simple as:
$ pip install oopt-gnpy-libyang
Building from source
Since this library is a Python wrapper around a C++ wrapper around a C library, source-based builds are more complex. They require:
- a C++20 compiler (e.g., GCC 10+, clang 10+, MSVC 17.2+)
libyang
and its dependencieslibyang-cpp
and its dependencies- CMake 3.21+
Unlike the wheels already bundle all the required libraries, when building from source, libyang
, libyang-cpp
and all their dependencies will have to be installed first.
Also, in a from-source build these won't be bundled into the resulting package.
For an inspiration, consult our GitHub packaging recipes.
License
Copyright © 2021-2023 Telecom Infra Project and GNPy contributors. Licensed under the 3-clause BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for oopt_gnpy_libyang-0.0.10-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53ec3bed38c8b753ac262fa393fbd3f02b842dd5bc1b2179b0640a6be5b17ebc |
|
MD5 | 14745f7445bbeec6e7da8a5f832a6504 |
|
BLAKE2b-256 | f690ac09585e3026f123a2a9ea36a8bca271b447c8150e62c8b372fa96f51f61 |
Hashes for oopt_gnpy_libyang-0.0.10-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76e880601f2f020e831c9974ee13590046559c8162a2f50e71c8e150125f84f8 |
|
MD5 | e2f4183518a4123fa80ffde62c100c35 |
|
BLAKE2b-256 | f772c4fad46f3f5a74b449c8ac560936285c94bd2bc4dc22008cbef04a13325c |
Hashes for oopt_gnpy_libyang-0.0.10-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 742bd440172f995e900a714cbd646015b0c49c1f4959b035bc740d4fb6b796e3 |
|
MD5 | 2959aacbdc17a0e9ab7fe3cb113a20aa |
|
BLAKE2b-256 | 49f22023dde436ed2f20c0bfe8e730931e6e9ff84b3172cc6afab5fa88082dc0 |
Hashes for oopt_gnpy_libyang-0.0.10-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9578088226041b0a6c4de6806694c9d78126eb7d7547f7edf61bca5cb579623 |
|
MD5 | 47f1edf00c9aeaa1bf10446a21c5dc89 |
|
BLAKE2b-256 | a884e9533d63b54386f46947c86821c3c84bd6a1e0968fe3abaa66618b60208f |
Hashes for oopt_gnpy_libyang-0.0.10-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652fa443ad2f3ef490989446b2a35379ca2d2e7a02f96cb239909544cef4f034 |
|
MD5 | f92673aa409c809228897c02e4aefa8e |
|
BLAKE2b-256 | 87809f30b2fe4a1cc42e0b134b8303d360642b92c183ef0558488eb50502e9e1 |
Hashes for oopt_gnpy_libyang-0.0.10-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68effd465c7cc889f18b4489116557fa8e7d8bd9afd5ac3de9713488c660e55f |
|
MD5 | 4ee088248f7ac489c0fed152413215c9 |
|
BLAKE2b-256 | 968407342ca9c2c39d896f88903adab9aa751b9b9365d74398b4c7ac6a744b30 |
Hashes for oopt_gnpy_libyang-0.0.10-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1c7f7979b672039d79e17136d17af6b819dd9899be06ca3cf4e7d1eb91437d7 |
|
MD5 | c1e7f84f57ab785ccb1574f5ba4db66a |
|
BLAKE2b-256 | 7d6cd37408c87d78e3e5533edef633f76c57bceb4d4a8c2bdec15eb7bec52cf1 |
Hashes for oopt_gnpy_libyang-0.0.10-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7346f92cc8d537c33c347f441687eace5d39a74575eb4aa975dbaa6f9c4fc7b2 |
|
MD5 | 6c70c1776e78102f1da1ba13598878c0 |
|
BLAKE2b-256 | faaab0da9e31ed3821fa3f8cb562e366754ac994ea72905eb1eb3a959ae5aa2a |