2
2
# Copyright (c) 2021 RACOM s.r.o.
3
3
# SPDX-License-Identifier: MIT
4
4
5
+ import json
5
6
import logging
6
7
from typing import IO , Any , Dict , Iterator , Optional , Tuple , Union
7
8
8
9
from _libyang import ffi , lib
9
10
from .keyed_list import KeyedList
10
11
from .schema import (
11
12
Module ,
13
+ SAnydata ,
12
14
SContainer ,
13
15
SLeaf ,
14
16
SLeafList ,
@@ -104,6 +106,21 @@ def newval_flags(
104
106
return flags
105
107
106
108
109
+ # -------------------------------------------------------------------------------------
110
+ def anydata_format (fmt_string : str ) -> int :
111
+ if fmt_string == "datatree" :
112
+ return lib .LYD_ANYDATA_DATATREE
113
+ if fmt_string == "string" :
114
+ return lib .LYD_ANYDATA_STRING
115
+ if fmt_string == "xml" :
116
+ return lib .LYD_ANYDATA_XML
117
+ if fmt_string == "json" :
118
+ return lib .LYD_ANYDATA_JSON
119
+ if fmt_string == "lyb" :
120
+ return lib .LYD_ANYDATA_LYB
121
+ raise ValueError ("unknown anydata format: %r" % fmt_string )
122
+
123
+
107
124
# -------------------------------------------------------------------------------------
108
125
def parser_flags (
109
126
lyb_mod_update : bool = False ,
@@ -1147,6 +1164,8 @@ def dict_to_dnode(
1147
1164
rpc : bool = False ,
1148
1165
rpcreply : bool = False ,
1149
1166
notification : bool = False ,
1167
+ types : Optional [Tuple [int , ...]] = None ,
1168
+ anydata_fmt : str = "json" ,
1150
1169
) -> Optional [DNode ]:
1151
1170
"""
1152
1171
Convert a python dictionary to a DNode object given a YANG module object. The return
@@ -1254,6 +1273,34 @@ def _create_list(_parent, module, name, key_values, in_rpc_output=False):
1254
1273
created .append (n [0 ])
1255
1274
return n [0 ]
1256
1275
1276
+ def _create_anydata (_parent , module , name , value , in_rpc_output = False ):
1277
+ if value is not None :
1278
+ if isinstance (value , dict ) and anydata_fmt == "json" :
1279
+ value = json .dumps (value )
1280
+ elif not isinstance (value , str ):
1281
+ value = str (value )
1282
+
1283
+ n = ffi .new ("struct lyd_node **" )
1284
+ flags = newval_flags (rpc_output = in_rpc_output , store_only = store_only )
1285
+ ret = lib .lyd_new_any (
1286
+ _parent ,
1287
+ module .cdata ,
1288
+ str2c (name ),
1289
+ str2c (value ),
1290
+ anydata_format (anydata_fmt ),
1291
+ flags ,
1292
+ n ,
1293
+ )
1294
+ if ret != lib .LY_SUCCESS :
1295
+ if _parent :
1296
+ parent_path = repr (DNode .new (module .context , _parent ).path ())
1297
+ else :
1298
+ parent_path = "module %r" % module .name ()
1299
+ raise module .context .error (
1300
+ "failed to create leaf %r as a child of %s" , name , parent_path
1301
+ )
1302
+ created .append (n [0 ])
1303
+
1257
1304
schema_cache = {}
1258
1305
1259
1306
def _find_schema (schema_parent , name , prefix ):
@@ -1270,7 +1317,7 @@ def _find_schema(schema_parent, name, prefix):
1270
1317
if schema_parent is None :
1271
1318
# there may not be any input or any output node in the rpc
1272
1319
return None , None
1273
- for s in schema_parent :
1320
+ for s in schema_parent . children ( types = types ) :
1274
1321
if s .name () != name :
1275
1322
continue
1276
1323
mod = s .module ()
@@ -1370,6 +1417,9 @@ def _to_dnode(_dic, _schema, _parent=ffi.NULL, in_rpc_output=False):
1370
1417
n = _create_container (_parent , module , name , in_rpc_output )
1371
1418
_to_dnode (value , s , n , in_rpc_output )
1372
1419
1420
+ elif isinstance (s , SAnydata ):
1421
+ _create_anydata (_parent , module , name , value , in_rpc_output )
1422
+
1373
1423
result = None
1374
1424
1375
1425
try :
0 commit comments