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 ,
@@ -107,6 +109,21 @@ def newval_flags(
107
109
return flags
108
110
109
111
112
+ # -------------------------------------------------------------------------------------
113
+ def anydata_format (fmt_string : str ) -> int :
114
+ if fmt_string == "datatree" :
115
+ return lib .LYD_ANYDATA_DATATREE
116
+ if fmt_string == "string" :
117
+ return lib .LYD_ANYDATA_STRING
118
+ if fmt_string == "xml" :
119
+ return lib .LYD_ANYDATA_XML
120
+ if fmt_string == "json" :
121
+ return lib .LYD_ANYDATA_JSON
122
+ if fmt_string == "lyb" :
123
+ return lib .LYD_ANYDATA_LYB
124
+ raise ValueError ("unknown anydata format: %r" % fmt_string )
125
+
126
+
110
127
# -------------------------------------------------------------------------------------
111
128
def parser_flags (
112
129
lyb_mod_update : bool = False ,
@@ -1192,6 +1209,8 @@ def dict_to_dnode(
1192
1209
rpcreply : bool = False ,
1193
1210
notification : bool = False ,
1194
1211
store_only : bool = False ,
1212
+ types : Optional [Tuple [int , ...]] = None ,
1213
+ anydata_fmt : str = "json" ,
1195
1214
) -> Optional [DNode ]:
1196
1215
"""
1197
1216
Convert a python dictionary to a DNode object given a YANG module object. The return
@@ -1301,6 +1320,34 @@ def _create_list(_parent, module, name, key_values, in_rpc_output=False):
1301
1320
created .append (n [0 ])
1302
1321
return n [0 ]
1303
1322
1323
+ def _create_anydata (_parent , module , name , value , in_rpc_output = False ):
1324
+ if value is not None :
1325
+ if isinstance (value , dict ) and anydata_fmt == "json" :
1326
+ value = json .dumps (value )
1327
+ elif not isinstance (value , str ):
1328
+ value = str (value )
1329
+
1330
+ n = ffi .new ("struct lyd_node **" )
1331
+ flags = newval_flags (rpc_output = in_rpc_output , store_only = store_only )
1332
+ ret = lib .lyd_new_any (
1333
+ _parent ,
1334
+ module .cdata ,
1335
+ str2c (name ),
1336
+ str2c (value ),
1337
+ anydata_format (anydata_fmt ),
1338
+ flags ,
1339
+ n ,
1340
+ )
1341
+ if ret != lib .LY_SUCCESS :
1342
+ if _parent :
1343
+ parent_path = repr (DNode .new (module .context , _parent ).path ())
1344
+ else :
1345
+ parent_path = "module %r" % module .name ()
1346
+ raise module .context .error (
1347
+ "failed to create leaf %r as a child of %s" , name , parent_path
1348
+ )
1349
+ created .append (n [0 ])
1350
+
1304
1351
schema_cache = {}
1305
1352
1306
1353
def _find_schema (schema_parent , name , prefix ):
@@ -1317,7 +1364,7 @@ def _find_schema(schema_parent, name, prefix):
1317
1364
if schema_parent is None :
1318
1365
# there may not be any input or any output node in the rpc
1319
1366
return None , None
1320
- for s in schema_parent :
1367
+ for s in schema_parent . children ( types = types ) :
1321
1368
if s .name () != name :
1322
1369
continue
1323
1370
mod = s .module ()
@@ -1417,6 +1464,9 @@ def _to_dnode(_dic, _schema, _parent=ffi.NULL, in_rpc_output=False):
1417
1464
n = _create_container (_parent , module , name , in_rpc_output )
1418
1465
_to_dnode (value , s , n , in_rpc_output )
1419
1466
1467
+ elif isinstance (s , SAnydata ):
1468
+ _create_anydata (_parent , module , name , value , in_rpc_output )
1469
+
1420
1470
result = None
1421
1471
1422
1472
try :
0 commit comments