Skip to content

Commit

Permalink
cdefs: refactor to support version 3.0.X
Browse files Browse the repository at this point in the history
This patch perform the following:
- refactor the code to be able to use libyang 3.0.X
- adds additional parsing options

Signed-off-by: Stefan Gula <[email protected]>
  • Loading branch information
steweg committed Feb 28, 2024
1 parent 52da1c4 commit 3bd79f8
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 65 deletions.
46 changes: 30 additions & 16 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ int ly_log_options(int);

LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL);
extern "Python" void lypy_log_cb(LY_LOG_LEVEL, const char *, const char *);
void ly_set_log_clb(void (*)(LY_LOG_LEVEL, const char *, const char *), int);
struct ly_err_item *ly_err_first(const struct ly_ctx *);
void ly_set_log_clb(void (*)(LY_LOG_LEVEL, const char *, const char *, const char *, uint64_t));
const struct ly_err_item *ly_err_first(const struct ly_ctx *);
const struct ly_err_item *ly_err_last(const struct ly_ctx *);
void ly_err_clean(struct ly_ctx *, struct ly_err_item *);
LY_VECODE ly_vecode(const struct ly_ctx *);

#define LYS_UNKNOWN ...
#define LYS_CONTAINER ...
Expand Down Expand Up @@ -238,14 +238,15 @@ struct lysc_node {

struct ly_err_item {
LY_LOG_LEVEL level;
LY_ERR no;
LY_ERR err;
LY_VECODE vecode;
char *msg;
char *path;
char *data_path;
char *schema_path;
uint64_t line;
char *apptag;
struct ly_err_item *next;
struct ly_err_item *prev;
...;
};

struct lyd_node {
Expand All @@ -261,11 +262,15 @@ struct lyd_node {

LY_ERR lys_set_implemented(struct lys_module *, const char **);

#define LYD_NEW_VAL_OUTPUT ...
#define LYD_NEW_VAL_STORE_ONLY ...
#define LYD_NEW_VAL_BIN ...
#define LYD_NEW_VAL_CANON ...
#define LYD_NEW_META_CLEAR_DFLT ...
#define LYD_NEW_PATH_UPDATE ...
#define LYD_NEW_PATH_OUTPUT ...
#define LYD_NEW_PATH_OPAQ ...
#define LYD_NEW_PATH_BIN_VALUE ...
#define LYD_NEW_PATH_CANON_VALUE ...
#define LYD_NEW_PATH_OPAQ ...
#define LYD_NEW_PATH_WITH_OPAQ ...
#define LYD_NEW_ANY_USE_VALUE ...
LY_ERR lyd_new_path(struct lyd_node *, const struct ly_ctx *, const char *, const char *, uint32_t, struct lyd_node **);
LY_ERR lyd_find_xpath(const struct lyd_node *, const char *, struct ly_set **);
void lyd_unlink_siblings(struct lyd_node *node);
Expand Down Expand Up @@ -310,6 +315,7 @@ LY_ERR lyd_print_all(struct ly_out *, const struct lyd_node *, LYD_FORMAT, uint3
#define LYD_PARSE_OPTS_MASK ...
#define LYD_PARSE_ORDERED ...
#define LYD_PARSE_STRICT ...
#define LYD_PARSE_STORE_ONLY ...

#define LYD_VALIDATE_NO_STATE ...
#define LYD_VALIDATE_PRESENT ...
Expand Down Expand Up @@ -614,6 +620,7 @@ struct lysp_node_list {
};

struct lysc_type {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand Down Expand Up @@ -682,7 +689,6 @@ struct lysc_ext {
struct lysc_ext_instance *exts;
struct lyplg_ext *plugin;
struct lys_module *module;
uint32_t refcount;
uint16_t flags;
};

Expand All @@ -703,11 +709,10 @@ typedef enum {
LYD_PATH_STD_NO_LAST_PRED
} LYD_PATH_TYPE;

LY_ERR lyd_new_term(struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_node **);
LY_ERR lyd_new_term(struct lyd_node *, const struct lys_module *, const char *, const char *, uint32_t, struct lyd_node **);
char* lyd_path(const struct lyd_node *, LYD_PATH_TYPE, char *, size_t);
LY_ERR lyd_new_inner(struct lyd_node *, const struct lys_module *, const char *, ly_bool, struct lyd_node **);
LY_ERR lyd_new_list(struct lyd_node *, const struct lys_module *, const char *, ly_bool, struct lyd_node **, ...);
LY_ERR lyd_new_list2(struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_node **);
LY_ERR lyd_new_list(struct lyd_node *, const struct lys_module *, const char *, uint32_t, struct lyd_node **node, ...);

struct lyd_node_inner {
union {
Expand Down Expand Up @@ -821,6 +826,7 @@ struct lysp_restr {
};

struct lysc_type_num {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -829,6 +835,7 @@ struct lysc_type_num {
};

struct lysc_type_dec {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -838,6 +845,7 @@ struct lysc_type_dec {
};

struct lysc_type_str {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -859,6 +867,7 @@ struct lysc_type_bitenum_item {
};

struct lysc_type_enum {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -867,6 +876,7 @@ struct lysc_type_enum {
};

struct lysc_type_bits {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -875,18 +885,19 @@ struct lysc_type_bits {
};

struct lysc_type_leafref {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
uint32_t refcount;
struct lyxp_expr *path;
struct lysc_prefix *prefixes;
const struct lys_module *cur_mod;
struct lysc_type *realtype;
uint8_t require_instance;
};

struct lysc_type_identityref {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -895,6 +906,7 @@ struct lysc_type_identityref {
};

struct lysc_type_instanceid {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -903,6 +915,7 @@ struct lysc_type_instanceid {
};

struct lysc_type_union {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand All @@ -911,6 +924,7 @@ struct lysc_type_union {
};

struct lysc_type_bin {
const char *name;
struct lysc_ext_instance *exts;
struct lyplg_type *plugin;
LY_DATA_TYPE basetype;
Expand Down Expand Up @@ -1053,7 +1067,7 @@ LY_ERR lyd_merge_module(struct lyd_node **, const struct lyd_node *, const struc
LY_ERR lyd_new_implicit_tree(struct lyd_node *, uint32_t, struct lyd_node **);
LY_ERR lyd_new_implicit_all(struct lyd_node **, const struct ly_ctx *, uint32_t, struct lyd_node **);

LY_ERR lyd_new_meta(const struct ly_ctx *, struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_meta **);
LY_ERR lyd_new_meta(const struct ly_ctx *, struct lyd_node *, const struct lys_module *, const char *, const char *, uint32_t, struct lyd_meta **);

struct ly_opaq_name {
const char *name;
Expand Down
8 changes: 4 additions & 4 deletions cffi/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <libyang/libyang.h>
#include <libyang/version.h>

#if (LY_VERSION_MAJOR != 2)
#error "This version of libyang bindings only works with libyang 2.x"
#if (LY_VERSION_MAJOR != 3)
#error "This version of libyang bindings only works with libyang 3.x"
#endif
#if (LY_VERSION_MINOR < 37)
#error "Need at least libyang 2.37"
#if (LY_VERSION_MINOR < 0)
#error "Need at least libyang 3.0"
#endif
19 changes: 11 additions & 8 deletions libyang/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
DNode,
data_format,
data_type,
new_path_flags,
parser_flags,
path_flags,
validation_flags,
)
from .schema import Module, SNode, schema_in_format
Expand Down Expand Up @@ -117,8 +117,12 @@ def error(self, msg: str, *args) -> LibyangError:
while err:
if err.msg:
msg += ": %s" % c2str(err.msg)
if err.path:
msg += ": %s" % c2str(err.path)
if err.data_path:
msg += ": Data path: %s" % c2str(err.data_path)
if err.schema_path:
msg += ": Schema path: %s" % c2str(err.schema_path)
if err.line != 0:
msg += " (line %u)" % err.line
err = err.next
lib.ly_err_clean(self.cdata, ffi.NULL)

Expand Down Expand Up @@ -234,7 +238,7 @@ def create_data_path(
parent: Optional[DNode] = None,
value: Any = None,
update: bool = True,
no_parent_ret: bool = True,
store_only: bool = False,
rpc_output: bool = False,
force_return_value: bool = True,
) -> Optional[DNode]:
Expand All @@ -245,9 +249,7 @@ def create_data_path(
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
)
flags = new_path_flags(update=update, store_only=store_only, output=rpc_output)
dnode = ffi.new("struct lyd_node **")
ret = lib.lyd_new_path(
parent.cdata if parent else ffi.NULL,
Expand All @@ -259,7 +261,8 @@ def create_data_path(
)
dnode = dnode[0]
if ret != lib.LY_SUCCESS:
if lib.ly_vecode(self.cdata) != lib.LYVE_SUCCESS:
err = lib.ly_err_last(self.cdata)
if err != ffi.NULL and err.vecode != lib.LYVE_SUCCESS:
raise self.error("cannot create data path: %s", path)
lib.ly_err_clean(self.cdata, ffi.NULL)
if not dnode and not force_return_value:
Expand Down
Loading

0 comments on commit 3bd79f8

Please sign in to comment.