def cdata_leaf_value(cdata) -> Any: cdata = ffi.cast("struct lyd_node_leaf_list *", cdata) val_type = cdata.value_type if val_type in Type.STR_TYPES: return c2str(cdata.value_str) if val_type in Type.NUM_TYPES: return int(c2str(cdata.value_str)) if val_type == Type.BOOL: return bool(cdata.value.bln) if val_type == Type.DEC64: return lib.lyd_dec64_to_double(ffi.cast("struct lyd_node *", cdata)) if val_type == Type.LEAFREF: return DLeaf.cdata_leaf_value(cdata.value.leafref) return None
def parse_data_file(self, fileobj, fmt, data=False, config=False, strict=False, trusted=False, no_yanglib=False, rpc=False): if self._ctx is None: raise RuntimeError('context already destroyed') flags = parser_flags(data=data, config=config, strict=strict, trusted=trusted, no_yanglib=no_yanglib, rpc=rpc) fmt = data_format(fmt) args = [] if rpc: args.append(ffi.cast('struct lyd_node *', ffi.NULL)) dnode = lib.lyd_parse_fd(self._ctx, fileobj.fileno(), fmt, flags, *args) if not dnode: raise self.error('failed to parse data tree') return DNode.new(self, dnode)
def __init__(self, search_path=None, disable_searchdir_cwd=True, pointer=None): if pointer is not None: self._ctx = ffi.cast('struct ly_ctx *', pointer) return # already initialized options = 0 if disable_searchdir_cwd: options |= lib.LY_CTX_DISABLE_SEARCHDIR_CWD self._ctx = lib.ly_ctx_new(ffi.NULL, options) if not self._ctx: raise self.error('cannot create context') search_dirs = [] if 'YANGPATH' in os.environ: search_dirs.extend( os.environ['YANGPATH'].strip(': \t\r\n\'"').split(':')) elif 'YANG_MODPATH' in os.environ: search_dirs.extend( os.environ['YANG_MODPATH'].strip(': \t\r\n\'"').split(':')) if search_path: search_dirs.extend(search_path.strip(': \t\r\n\'"').split(':')) for path in search_dirs: if not os.path.isdir(path): continue if lib.ly_ctx_set_searchdir(self._ctx, str2c(path)) != 0: raise self.error('cannot set search dir')
def new(cls, context: "libyang.Context", cdata) -> "DNode": cdata = ffi.cast("struct lyd_node *", cdata) nodecls = cls.NODETYPE_CLASS.get(cdata.schema.nodetype, None) if nodecls is None: raise NotImplementedError("node type %s not implemented" % cdata.schema.nodetype) return nodecls(context, cdata)
def __init__(self, context, node_p): """ :arg Context context: The libyang.Context python object. :arg struct lyd_node * node_p: The pointer to the C structure allocated by libyang.so. """ self.context = context self._node = ffi.cast('struct lyd_node *', node_p)
def _skip(node) -> bool: if node.nodetype not in types: return True if node.nodetype != lib.LYS_LEAF: return False leaf = ffi.cast("struct lys_node_leaf *", node) if lib.lys_is_key(leaf, ffi.NULL): return True return False
def _init_yang_list(snode): if snode.flags & lib.LYS_USERORDERED: return [] # ordered list, return an empty builtin list # unordered lists if snode.nodetype == SNode.LEAFLIST: return KeyedList(key_name=None) if snode not in list_keys_cache: list_snode = ffi.cast("struct lys_node_list *", snode) keys = [] for i in range(list_snode.keys_size): key = ffi.cast("struct lys_node *", list_snode.keys[i]) keys.append(c2str(key.name)) if len(keys) == 1: list_keys_cache[snode] = keys[0] else: list_keys_cache[snode] = tuple(keys) return KeyedList(key_name=list_keys_cache[snode])
def parse_data_mem( self, d: Union[str, bytes], fmt: str, data: bool = False, config: bool = False, get: bool = False, getconfig: bool = False, edit: bool = False, rpc: bool = False, rpcreply: bool = False, strict: bool = False, trusted: bool = False, no_yanglib: bool = False, rpc_request: Optional[DNode] = None, data_tree: Optional[DNode] = None, ) -> DNode: if self.cdata is None: raise RuntimeError("context already destroyed") flags = parser_flags( data=data, config=config, get=get, getconfig=getconfig, edit=edit, rpc=rpc, rpcreply=rpcreply, strict=strict, trusted=trusted, no_yanglib=no_yanglib, ) fmt = data_format(fmt) if fmt == lib.LYD_LYB: d = str2c(d, encode=False) else: d = str2c(d, encode=True) args = [] if rpcreply: if rpc_request is None: raise ValueError( "rpc_request node is required when rpcreply=True") args.append(rpc_request.cdata) if rpc or rpcreply: if data_tree is not None: args.append(data_tree.cdata) else: args.append(ffi.cast("struct lyd_node *", ffi.NULL)) dnode = lib.lyd_parse_mem(self.cdata, d, fmt, flags, *args) if not dnode: raise self.error("failed to parse data tree") return DNode.new(self, dnode)
def __init__( self, search_path: Optional[str] = None, disable_searchdir_cwd: bool = True, pointer=None, # C type: "struct ly_ctx *" cdata=None, # C type: "struct ly_ctx *" ): if pointer is not None: deprecated("pointer=", "cdata=", "2.0.0") cdata = pointer if cdata is not None: self.cdata = ffi.cast("struct ly_ctx *", cdata) return # already initialized options = 0 if disable_searchdir_cwd: options |= lib.LY_CTX_DISABLE_SEARCHDIR_CWD self.cdata = ffi.gc( lib.ly_ctx_new(ffi.NULL, options), lambda ctx: lib.ly_ctx_destroy(ctx, ffi.NULL), ) if not self.cdata: raise self.error("cannot create context") search_dirs = [] if "YANGPATH" in os.environ: search_dirs.extend( os.environ["YANGPATH"].strip(": \t\r\n'\"").split(":")) elif "YANG_MODPATH" in os.environ: search_dirs.extend( os.environ["YANG_MODPATH"].strip(": \t\r\n'\"").split(":")) if search_path: search_dirs.extend(search_path.strip(": \t\r\n'\"").split(":")) for path in search_dirs: if not os.path.isdir(path): continue if lib.ly_ctx_set_searchdir(self.cdata, str2c(path)) != 0: raise self.error("cannot set search dir")
def keys(self) -> Iterator[SNode]: for i in range(self.cdata_list.keys_size): node = ffi.cast("struct lys_node *", self.cdata_list.keys[i]) yield SLeaf(self.context, node)
def _leaf(self): deprecated("_leaf", "cdata_leaf", "2.0.0") return ffi.cast("struct lyd_node_leaf_list *", self.cdata)
def __init__(self, context, node_p): super().__init__(context, node_p) self._leaf = ffi.cast('struct lyd_node_leaf_list *', node_p)
def new(cls, context, node_p): node_p = ffi.cast('struct lyd_node *', node_p) nodecls = cls.NODETYPE_CLASS.get(node_p.schema.nodetype, DNode) return nodecls(context, node_p)
def __init__(self, context: "libyang.Context", cdata): super().__init__(context, cdata) self.cdata_list = ffi.cast("struct lys_node_list *", cdata)
def keys(self): for i in range(self._list.keys_size): node = ffi.cast('struct lys_node *', self._list.keys[i]) yield SLeaf(self.context, node)
def __init__(self, context, node_p): super().__init__(context, node_p) self._container = ffi.cast('struct lys_node_container *', node_p)
def __init__(self, context, node_p): Node.__init__(self, context, node_p) self._list = ffi.cast('struct lys_node_list *', node_p)
def new(context: "libyang.Context", cdata) -> "SNode": cdata = ffi.cast("struct lys_node *", cdata) nodecls = SNode.NODETYPE_CLASS.get(cdata.nodetype, None) if nodecls is None: raise TypeError("node type %s not implemented" % cdata.nodetype) return nodecls(context, cdata)