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 ,
@@ -1188,6 +1205,8 @@ def dict_to_dnode(
1188
1205
rpc : bool = False ,
1189
1206
rpcreply : bool = False ,
1190
1207
notification : bool = False ,
1208
+ types : Optional [Tuple [int , ...]] = None ,
1209
+ anydata_fmt : str = "json" ,
1191
1210
) -> Optional [DNode ]:
1192
1211
"""
1193
1212
Convert a python dictionary to a DNode object given a YANG module object. The return
@@ -1295,6 +1314,34 @@ def _create_list(_parent, module, name, key_values, in_rpc_output=False):
1295
1314
created .append (n [0 ])
1296
1315
return n [0 ]
1297
1316
1317
+ def _create_anydata (_parent , module , name , value , in_rpc_output = False ):
1318
+ if value is not None :
1319
+ if isinstance (value , dict ) and anydata_fmt == "json" :
1320
+ value = json .dumps (value )
1321
+ elif not isinstance (value , str ):
1322
+ value = str (value )
1323
+
1324
+ n = ffi .new ("struct lyd_node **" )
1325
+ flags = newval_flags (rpc_output = in_rpc_output , store_only = store_only )
1326
+ ret = lib .lyd_new_any (
1327
+ _parent ,
1328
+ module .cdata ,
1329
+ str2c (name ),
1330
+ str2c (value ),
1331
+ anydata_format (anydata_fmt ),
1332
+ flags ,
1333
+ n ,
1334
+ )
1335
+ if ret != lib .LY_SUCCESS :
1336
+ if _parent :
1337
+ parent_path = repr (DNode .new (module .context , _parent ).path ())
1338
+ else :
1339
+ parent_path = "module %r" % module .name ()
1340
+ raise module .context .error (
1341
+ "failed to create leaf %r as a child of %s" , name , parent_path
1342
+ )
1343
+ created .append (n [0 ])
1344
+
1298
1345
schema_cache = {}
1299
1346
1300
1347
def _find_schema (schema_parent , name , prefix ):
@@ -1311,7 +1358,7 @@ def _find_schema(schema_parent, name, prefix):
1311
1358
if schema_parent is None :
1312
1359
# there may not be any input or any output node in the rpc
1313
1360
return None , None
1314
- for s in schema_parent :
1361
+ for s in schema_parent . children ( types = types ) :
1315
1362
if s .name () != name :
1316
1363
continue
1317
1364
mod = s .module ()
@@ -1411,6 +1458,9 @@ def _to_dnode(_dic, _schema, _parent=ffi.NULL, in_rpc_output=False):
1411
1458
n = _create_container (_parent , module , name , in_rpc_output )
1412
1459
_to_dnode (value , s , n , in_rpc_output )
1413
1460
1461
+ elif isinstance (s , SAnydata ):
1462
+ _create_anydata (_parent , module , name , value , in_rpc_output )
1463
+
1414
1464
result = None
1415
1465
1416
1466
try :
0 commit comments