22# Copyright (c) 2021 RACOM s.r.o.
33# SPDX-License-Identifier: MIT
44
5+ import json
56import logging
67from typing import IO , Any , Dict , Iterator , Optional , Tuple , Union
78
89from _libyang import ffi , lib
910from .keyed_list import KeyedList
1011from .schema import (
1112 Module ,
13+ SAnydata ,
1214 SContainer ,
1315 SLeaf ,
1416 SLeafList ,
@@ -107,6 +109,21 @@ def newval_flags(
107109 return flags
108110
109111
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+
110127# -------------------------------------------------------------------------------------
111128def parser_flags (
112129 lyb_mod_update : bool = False ,
@@ -1199,6 +1216,8 @@ def dict_to_dnode(
11991216 rpcreply : bool = False ,
12001217 notification : bool = False ,
12011218 store_only : bool = False ,
1219+ types : Optional [Tuple [int , ...]] = None ,
1220+ anydata_fmt : str = "json" ,
12021221) -> Optional [DNode ]:
12031222 """
12041223 Convert a python dictionary to a DNode object given a YANG module object. The return
@@ -1308,6 +1327,34 @@ def _create_list(_parent, module, name, key_values, in_rpc_output=False):
13081327 created .append (n [0 ])
13091328 return n [0 ]
13101329
1330+ def _create_anydata (_parent , module , name , value , in_rpc_output = False ):
1331+ if value is not None :
1332+ if isinstance (value , dict ) and anydata_fmt == "json" :
1333+ value = json .dumps (value )
1334+ elif not isinstance (value , str ):
1335+ value = str (value )
1336+
1337+ n = ffi .new ("struct lyd_node **" )
1338+ flags = newval_flags (rpc_output = in_rpc_output , store_only = store_only )
1339+ ret = lib .lyd_new_any (
1340+ _parent ,
1341+ module .cdata ,
1342+ str2c (name ),
1343+ str2c (value ),
1344+ anydata_format (anydata_fmt ),
1345+ flags ,
1346+ n ,
1347+ )
1348+ if ret != lib .LY_SUCCESS :
1349+ if _parent :
1350+ parent_path = repr (DNode .new (module .context , _parent ).path ())
1351+ else :
1352+ parent_path = "module %r" % module .name ()
1353+ raise module .context .error (
1354+ "failed to create leaf %r as a child of %s" , name , parent_path
1355+ )
1356+ created .append (n [0 ])
1357+
13111358 schema_cache = {}
13121359
13131360 def _find_schema (schema_parent , name , prefix ):
@@ -1324,7 +1371,7 @@ def _find_schema(schema_parent, name, prefix):
13241371 if schema_parent is None :
13251372 # there may not be any input or any output node in the rpc
13261373 return None , None
1327- for s in schema_parent :
1374+ for s in schema_parent . children ( types = types ) :
13281375 if s .name () != name :
13291376 continue
13301377 mod = s .module ()
@@ -1424,6 +1471,9 @@ def _to_dnode(_dic, _schema, _parent=ffi.NULL, in_rpc_output=False):
14241471 n = _create_container (_parent , module , name , in_rpc_output )
14251472 _to_dnode (value , s , n , in_rpc_output )
14261473
1474+ elif isinstance (s , SAnydata ):
1475+ _create_anydata (_parent , module , name , value , in_rpc_output )
1476+
14271477 result = None
14281478
14291479 try :
0 commit comments