示例#1
0
    def create_data_path(
        self,
        path: str,
        parent: Optional[DNode] = None,
        value: Any = None,
        update: bool = True,
        no_parent_ret: bool = True,
        rpc_output: bool = False,
        force_return_value: bool = True,
    ) -> Optional[DNode]:
        if self.cdata is None:
            raise RuntimeError("context already destroyed")
        lib.lypy_set_errno(0)
        if value is not None:
            if isinstance(value, bool):
                value = str(value).lower()
            elif not isinstance(value, str):
                value = str(value)
        flags = path_flags(update=update,
                           no_parent_ret=no_parent_ret,
                           rpc_output=rpc_output)
        dnode = lib.lyd_new_path(
            parent.cdata if parent else ffi.NULL,
            self.cdata,
            str2c(path),
            str2c(value),
            0,
            flags,
        )
        if lib.lypy_get_errno() != lib.LY_SUCCESS:
            if lib.ly_vecode(self.cdata) != lib.LYVE_PATH_EXISTS:
                raise self.error("cannot create data path: %s", path)
            lib.ly_err_clean(self.cdata, ffi.NULL)
            lib.lypy_set_errno(0)
        if not dnode and not force_return_value:
            return None

        if not dnode and parent:
            # This can happen when path points to an already created leaf and
            # its value does not change.
            # In that case, lookup the existing leaf and return it.
            node_set = lib.lyd_find_path(parent.cdata, str2c(path))
            try:
                if not node_set or not node_set.number:
                    raise self.error("cannot find path: %s", path)
                dnode = node_set.set.s[0]
            finally:
                lib.ly_set_free(node_set)

        if not dnode:
            raise self.error("cannot find created path")

        return DNode.new(self, dnode)
示例#2
0
    def error(self, msg, *args):
        msg %= args

        if self._ctx:
            err = lib.ly_err_first(self._ctx)
            if err:
                if err.msg:
                    msg += ': %s' % c2str(err.msg)
                if err.path:
                    msg += ': %s' % c2str(err.path)
            lib.ly_err_clean(self._ctx, ffi.NULL)

        return LibyangError(msg)
示例#3
0
    def error(self, msg: str, *args) -> LibyangError:
        msg %= args

        if self.cdata:
            err = lib.ly_err_first(self.cdata)
            while err:
                if err.msg:
                    msg += ": %s" % c2str(err.msg)
                if err.path:
                    msg += ": %s" % c2str(err.path)
                err = err.next
            lib.ly_err_clean(self.cdata, ffi.NULL)

        return LibyangError(msg)
示例#4
0
    def error(self, msg, *args):
        errors = []
        try:
            err = lib.ly_err_first(self._ctx)
            while err:
                e = []
                if err.path:
                    e.append(c2str(err.path))
                if err.msg:
                    e.append(c2str(err.msg))
                if err.apptag:
                    e.append(c2str(err.apptag))
                if e:
                    errors.append(': '.join(e))
                err = err.next
        finally:
            lib.ly_err_clean(self._ctx, ffi.NULL)

        msg %= args
        if errors:
            msg += ': ' + ' '.join(errors)

        return LibyangError(msg)