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_str(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_str(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.6-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32395242dc8b5faa90bbfffe952107c8c6f915be1e395716428dd2bb7b5b45ba |
|
MD5 | 563db22fb5afcd7963a3336c8eead170 |
|
BLAKE2b-256 | 586f84fa30593360eac68c36622c9275ea3339d2a539ebba3874dc568b0d8108 |
Hashes for oopt_gnpy_libyang-0.0.6-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5935117a1fc8e48e5adaafc46a990a3c93c461a352e8004dcb3818b7d082b6cb |
|
MD5 | 9551327e1ad6db413f20e09b00f214dd |
|
BLAKE2b-256 | c8aad9b10ce32bb725eeff9c388988b882fc9b0ad6237dae715a3612422f76d8 |
Hashes for oopt_gnpy_libyang-0.0.6-cp311-cp311-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b82c5cb3646ce936a499eabef12efb71a9c30e93eb49895acd5b2dfc6f8aa33c |
|
MD5 | 056d92c651d6cf432031f07603563a07 |
|
BLAKE2b-256 | 39f2fb2886ac1d9d036b6366d788007f13213a9c4696851ddbffd05cd377e626 |
Hashes for oopt_gnpy_libyang-0.0.6-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b30c6b8a1a115a6c26fa31354da64cca774964a3b476a205d62209dfd45dc1b8 |
|
MD5 | f00852c68c5b3cd5703aa0774b52e87c |
|
BLAKE2b-256 | 18f9423a3a169d93fe052c3ac5d3f91541707814b395a3e1a6e4b6f0fcb4772a |
Hashes for oopt_gnpy_libyang-0.0.6-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3fd450243b44f576a64fbb7ddb2b03e8f251d5ab5e859dbbb8394d9d424b9af |
|
MD5 | e468e5bab2025cd22e24e492cf93c4fc |
|
BLAKE2b-256 | 9b551b24da83fef971f720f278b6d3b5feb5c98d537f8904dc41c1dbcd06f3c2 |
Hashes for oopt_gnpy_libyang-0.0.6-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3932241b84976c6db8d617e73391d4e9fa7e0fe32c7896f954e91e555e60cede |
|
MD5 | 6435528e1a0f3b0b6469b962d8372547 |
|
BLAKE2b-256 | 7be101bb7c77df87527d4c676232c465785c7a21e85733d1e5405828a1043d93 |
Hashes for oopt_gnpy_libyang-0.0.6-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16ab83d2ba175aaaa35c7856ca825b8da0c63c8d7e3aa3b5c42165630d7960ed |
|
MD5 | b57dd6cb56fdc84284affc122e0cc7bb |
|
BLAKE2b-256 | bb36a893598d8feaef262b701b229174ca9642d9ffb30e1864310fc6b2123c7b |
Hashes for oopt_gnpy_libyang-0.0.6-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 369213a74ac8886e11a1741222b621758a68bc2a46999e023cd5fd979b6c915f |
|
MD5 | e1b282a510fa924af3a4ee16269a4b09 |
|
BLAKE2b-256 | ce55e5324a802f8f9cc1bc4ecdc0445caec4ee47b5472fbbb2294c3879747953 |