예제 #1
0
 def print_mem(
     self,
     fmt: str,
     with_siblings: bool = False,
     pretty: bool = False,
     include_implicit_defaults: bool = False,
     trim_default_values: bool = False,
     keep_empty_containers: bool = False,
 ) -> Union[str, bytes]:
     flags = printer_flags(
         with_siblings=with_siblings,
         pretty=pretty,
         include_implicit_defaults=include_implicit_defaults,
         trim_default_values=trim_default_values,
         keep_empty_containers=keep_empty_containers,
     )
     buf = ffi.new("char **")
     fmt = data_format(fmt)
     ret = lib.lyd_print_mem(buf, self.cdata, fmt, flags)
     if ret != 0:
         raise self.context.error("cannot print node")
     try:
         if fmt == lib.LYD_LYB:
             # binary format, do not convert to unicode
             return c2str(buf[0], decode=False)
         return c2str(buf[0], decode=True)
     finally:
         lib.free(buf[0])
예제 #2
0
 def validate(
     self,
     data: bool = False,
     config: bool = False,
     get: bool = False,
     getconfig: bool = False,
     edit: bool = False,
     rpc: bool = False,
     rpcreply: bool = False,
     no_yanglib: bool = False,
 ) -> None:
     if self.cdata.parent:
         raise self.context.error(
             "validation is only supported on top-level nodes")
     flags = parser_flags(
         data=data,
         config=config,
         get=get,
         getconfig=getconfig,
         edit=edit,
         rpc=rpc,
         rpcreply=rpcreply,
         no_yanglib=no_yanglib,
     )
     node_p = ffi.new("struct lyd_node **")
     node_p[0] = self.cdata
     ret = lib.lyd_validate(node_p, flags, ffi.NULL)
     if ret != 0:
         raise self.context.error("validation failed")
예제 #3
0
 def __iter__(self):
     """
     Return an iterator that yields all implemented modules from the context
     """
     idx = ffi.new('uint32_t *')
     mod = lib.ly_ctx_get_module_iter(self._ctx, idx)
     while mod:
         yield Module(self, mod)
         mod = lib.ly_ctx_get_module_iter(self._ctx, idx)
예제 #4
0
 def print_mem(self, fmt='tree', path=None):
     fmt = schema_out_format(fmt)
     buf = ffi.new('char **')
     ret = lib.lys_print_mem(buf, self._module, fmt, str2c(path), 0, 0)
     if ret != 0:
         raise self.context.error('cannot print module')
     try:
         return c2str(buf[0])
     finally:
         lib.free(buf[0])
예제 #5
0
파일: data.py 프로젝트: rjarry/libyang-cffi
 def validate(self, data=False, config=False, strict=False, trusted=False,
              no_yanglib=False):
     flags = parser_flags(
         data=data, config=config, strict=strict, trusted=trusted,
         no_yanglib=no_yanglib)
     node_p = ffi.new('struct lyd_node **')
     node_p[0] = self._node
     ret = lib.lyd_validate(node_p, flags, ffi.NULL)
     if ret != 0:
         self.context.error('validation failed')
예제 #6
0
 def __iter__(self):
     """
     Return an iterator that yields all implemented modules from the context
     """
     if self._ctx is None:
         raise RuntimeError('context already destroyed')
     idx = ffi.new('uint32_t *')
     mod = lib.ly_ctx_get_module_iter(self._ctx, idx)
     while mod:
         yield Module(self, mod)
         mod = lib.ly_ctx_get_module_iter(self._ctx, idx)
예제 #7
0
 def __iter__(self) -> Iterator[Module]:
     """
     Return an iterator that yields all implemented modules from the context
     """
     if self.cdata is None:
         raise RuntimeError("context already destroyed")
     idx = ffi.new("uint32_t *")
     mod = lib.ly_ctx_get_module_iter(self.cdata, idx)
     while mod:
         yield Module(self, mod)
         mod = lib.ly_ctx_get_module_iter(self.cdata, idx)
예제 #8
0
 def print_mem(
     self, fmt: str = "tree", path: Optional[str] = None
 ) -> Union[str, bytes]:
     fmt = schema_out_format(fmt)
     buf = ffi.new("char **")
     ret = lib.lys_print_mem(buf, self.cdata, fmt, str2c(path), 0, 0)
     if ret != 0:
         raise self.context.error("cannot print module")
     try:
         return c2str(buf[0])
     finally:
         lib.free(buf[0])
예제 #9
0
def str2c(s: Optional[str], encode: bool = True):
    if s is None:
        return ffi.NULL
    if hasattr(s, "encode"):
        s = s.encode("utf-8")
    return ffi.new("char []", s)
예제 #10
0
def str2c(s):
    if s is None:
        return ffi.NULL
    if hasattr(s, 'encode'):
        s = s.encode('utf-8')
    return ffi.new('char []', s)
예제 #11
0
 def dump_str(self, fmt=lib.LYS_OUT_TREE, path=None):
     buf = ffi.new('char **')
     ret = lib.lys_print_mem(buf, self._module, fmt, str2c(path), 0, 0)
     if ret != 0:
         raise self.context.error('cannot print module')
     return c2str(buf[0])