Skip to content

Commit ec6b19d

Browse files
committed
cdefs: refactor to support version 3.0.X
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 <steweg@gmail.com>
1 parent 52da1c4 commit ec6b19d

File tree

7 files changed

+184
-66
lines changed

7 files changed

+184
-66
lines changed

cffi/cdefs.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ int ly_log_options(int);
179179

180180
LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL);
181181
extern "Python" void lypy_log_cb(LY_LOG_LEVEL, const char *, const char *);
182-
void ly_set_log_clb(void (*)(LY_LOG_LEVEL, const char *, const char *), int);
183-
struct ly_err_item *ly_err_first(const struct ly_ctx *);
182+
void ly_set_log_clb(void (*)(LY_LOG_LEVEL, const char *, const char *, const char *, uint64_t));
183+
const struct ly_err_item *ly_err_first(const struct ly_ctx *);
184+
const struct ly_err_item *ly_err_last(const struct ly_ctx *);
184185
void ly_err_clean(struct ly_ctx *, struct ly_err_item *);
185-
LY_VECODE ly_vecode(const struct ly_ctx *);
186186

187187
#define LYS_UNKNOWN ...
188188
#define LYS_CONTAINER ...
@@ -238,14 +238,15 @@ struct lysc_node {
238238

239239
struct ly_err_item {
240240
LY_LOG_LEVEL level;
241-
LY_ERR no;
241+
LY_ERR err;
242242
LY_VECODE vecode;
243243
char *msg;
244-
char *path;
244+
char *data_path;
245+
char *schema_path;
246+
uint64_t line;
245247
char *apptag;
246248
struct ly_err_item *next;
247249
struct ly_err_item *prev;
248-
...;
249250
};
250251

251252
struct lyd_node {
@@ -261,11 +262,15 @@ struct lyd_node {
261262

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

265+
#define LYD_NEW_VAL_OUTPUT ...
266+
#define LYD_NEW_VAL_STORE_ONLY ...
267+
#define LYD_NEW_VAL_BIN ...
268+
#define LYD_NEW_VAL_CANON ...
269+
#define LYD_NEW_META_CLEAR_DFLT ...
264270
#define LYD_NEW_PATH_UPDATE ...
265-
#define LYD_NEW_PATH_OUTPUT ...
266-
#define LYD_NEW_PATH_OPAQ ...
267-
#define LYD_NEW_PATH_BIN_VALUE ...
268-
#define LYD_NEW_PATH_CANON_VALUE ...
271+
#define LYD_NEW_PATH_OPAQ ...
272+
#define LYD_NEW_PATH_WITH_OPAQ ...
273+
#define LYD_NEW_ANY_USE_VALUE ...
269274
LY_ERR lyd_new_path(struct lyd_node *, const struct ly_ctx *, const char *, const char *, uint32_t, struct lyd_node **);
270275
LY_ERR lyd_find_xpath(const struct lyd_node *, const char *, struct ly_set **);
271276
void lyd_unlink_siblings(struct lyd_node *node);
@@ -310,6 +315,7 @@ LY_ERR lyd_print_all(struct ly_out *, const struct lyd_node *, LYD_FORMAT, uint3
310315
#define LYD_PARSE_OPTS_MASK ...
311316
#define LYD_PARSE_ORDERED ...
312317
#define LYD_PARSE_STRICT ...
318+
#define LYD_PARSE_STORE_ONLY ...
313319

314320
#define LYD_VALIDATE_NO_STATE ...
315321
#define LYD_VALIDATE_PRESENT ...
@@ -614,6 +620,7 @@ struct lysp_node_list {
614620
};
615621

616622
struct lysc_type {
623+
const char *name;
617624
struct lysc_ext_instance *exts;
618625
struct lyplg_type *plugin;
619626
LY_DATA_TYPE basetype;
@@ -641,6 +648,7 @@ struct lysp_type {
641648
struct lysp_qname {
642649
const char *str;
643650
const struct lysp_module *mod;
651+
...;
644652
};
645653

646654
struct lysp_node {
@@ -682,7 +690,6 @@ struct lysc_ext {
682690
struct lysc_ext_instance *exts;
683691
struct lyplg_ext *plugin;
684692
struct lys_module *module;
685-
uint32_t refcount;
686693
uint16_t flags;
687694
};
688695

@@ -703,11 +710,10 @@ typedef enum {
703710
LYD_PATH_STD_NO_LAST_PRED
704711
} LYD_PATH_TYPE;
705712

706-
LY_ERR lyd_new_term(struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_node **);
713+
LY_ERR lyd_new_term(struct lyd_node *, const struct lys_module *, const char *, const char *, uint32_t, struct lyd_node **);
707714
char* lyd_path(const struct lyd_node *, LYD_PATH_TYPE, char *, size_t);
708715
LY_ERR lyd_new_inner(struct lyd_node *, const struct lys_module *, const char *, ly_bool, struct lyd_node **);
709-
LY_ERR lyd_new_list(struct lyd_node *, const struct lys_module *, const char *, ly_bool, struct lyd_node **, ...);
710-
LY_ERR lyd_new_list2(struct lyd_node *, const struct lys_module *, const char *, const char *, ly_bool, struct lyd_node **);
716+
LY_ERR lyd_new_list(struct lyd_node *, const struct lys_module *, const char *, uint32_t, struct lyd_node **node, ...);
711717

712718
struct lyd_node_inner {
713719
union {
@@ -821,6 +827,7 @@ struct lysp_restr {
821827
};
822828

823829
struct lysc_type_num {
830+
const char *name;
824831
struct lysc_ext_instance *exts;
825832
struct lyplg_type *plugin;
826833
LY_DATA_TYPE basetype;
@@ -829,6 +836,7 @@ struct lysc_type_num {
829836
};
830837

831838
struct lysc_type_dec {
839+
const char *name;
832840
struct lysc_ext_instance *exts;
833841
struct lyplg_type *plugin;
834842
LY_DATA_TYPE basetype;
@@ -838,6 +846,7 @@ struct lysc_type_dec {
838846
};
839847

840848
struct lysc_type_str {
849+
const char *name;
841850
struct lysc_ext_instance *exts;
842851
struct lyplg_type *plugin;
843852
LY_DATA_TYPE basetype;
@@ -859,6 +868,7 @@ struct lysc_type_bitenum_item {
859868
};
860869

861870
struct lysc_type_enum {
871+
const char *name;
862872
struct lysc_ext_instance *exts;
863873
struct lyplg_type *plugin;
864874
LY_DATA_TYPE basetype;
@@ -867,6 +877,7 @@ struct lysc_type_enum {
867877
};
868878

869879
struct lysc_type_bits {
880+
const char *name;
870881
struct lysc_ext_instance *exts;
871882
struct lyplg_type *plugin;
872883
LY_DATA_TYPE basetype;
@@ -875,18 +886,19 @@ struct lysc_type_bits {
875886
};
876887

877888
struct lysc_type_leafref {
889+
const char *name;
878890
struct lysc_ext_instance *exts;
879891
struct lyplg_type *plugin;
880892
LY_DATA_TYPE basetype;
881893
uint32_t refcount;
882894
struct lyxp_expr *path;
883895
struct lysc_prefix *prefixes;
884-
const struct lys_module *cur_mod;
885896
struct lysc_type *realtype;
886897
uint8_t require_instance;
887898
};
888899

889900
struct lysc_type_identityref {
901+
const char *name;
890902
struct lysc_ext_instance *exts;
891903
struct lyplg_type *plugin;
892904
LY_DATA_TYPE basetype;
@@ -895,6 +907,7 @@ struct lysc_type_identityref {
895907
};
896908

897909
struct lysc_type_instanceid {
910+
const char *name;
898911
struct lysc_ext_instance *exts;
899912
struct lyplg_type *plugin;
900913
LY_DATA_TYPE basetype;
@@ -903,6 +916,7 @@ struct lysc_type_instanceid {
903916
};
904917

905918
struct lysc_type_union {
919+
const char *name;
906920
struct lysc_ext_instance *exts;
907921
struct lyplg_type *plugin;
908922
LY_DATA_TYPE basetype;
@@ -911,6 +925,7 @@ struct lysc_type_union {
911925
};
912926

913927
struct lysc_type_bin {
928+
const char *name;
914929
struct lysc_ext_instance *exts;
915930
struct lyplg_type *plugin;
916931
LY_DATA_TYPE basetype;
@@ -1053,7 +1068,7 @@ LY_ERR lyd_merge_module(struct lyd_node **, const struct lyd_node *, const struc
10531068
LY_ERR lyd_new_implicit_tree(struct lyd_node *, uint32_t, struct lyd_node **);
10541069
LY_ERR lyd_new_implicit_all(struct lyd_node **, const struct ly_ctx *, uint32_t, struct lyd_node **);
10551070

1056-
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 **);
1071+
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 **);
10571072

10581073
struct ly_opaq_name {
10591074
const char *name;

cffi/source.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include <libyang/libyang.h>
77
#include <libyang/version.h>
88

9-
#if (LY_VERSION_MAJOR != 2)
10-
#error "This version of libyang bindings only works with libyang 2.x"
9+
#if (LY_VERSION_MAJOR != 3)
10+
#error "This version of libyang bindings only works with libyang 3.x"
1111
#endif
12-
#if (LY_VERSION_MINOR < 37)
13-
#error "Need at least libyang 2.37"
12+
#if (LY_VERSION_MINOR < 0)
13+
#error "Need at least libyang 3.0"
1414
#endif

libyang/context.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
DNode,
1212
data_format,
1313
data_type,
14+
new_path_flags,
1415
parser_flags,
15-
path_flags,
1616
validation_flags,
1717
)
1818
from .schema import Module, SNode, schema_in_format
@@ -117,8 +117,12 @@ def error(self, msg: str, *args) -> LibyangError:
117117
while err:
118118
if err.msg:
119119
msg += ": %s" % c2str(err.msg)
120-
if err.path:
121-
msg += ": %s" % c2str(err.path)
120+
if err.data_path:
121+
msg += ": Data path: %s" % c2str(err.data_path)
122+
if err.schema_path:
123+
msg += ": Schema path: %s" % c2str(err.schema_path)
124+
if err.line != 0:
125+
msg += " (line %u)" % err.line
122126
err = err.next
123127
lib.ly_err_clean(self.cdata, ffi.NULL)
124128

@@ -234,7 +238,7 @@ def create_data_path(
234238
parent: Optional[DNode] = None,
235239
value: Any = None,
236240
update: bool = True,
237-
no_parent_ret: bool = True,
241+
store_only: bool = False,
238242
rpc_output: bool = False,
239243
force_return_value: bool = True,
240244
) -> Optional[DNode]:
@@ -245,9 +249,7 @@ def create_data_path(
245249
value = str(value).lower()
246250
elif not isinstance(value, str):
247251
value = str(value)
248-
flags = path_flags(
249-
update=update, no_parent_ret=no_parent_ret, rpc_output=rpc_output
250-
)
252+
flags = new_path_flags(update=update, store_only=store_only, output=rpc_output)
251253
dnode = ffi.new("struct lyd_node **")
252254
ret = lib.lyd_new_path(
253255
parent.cdata if parent else ffi.NULL,
@@ -259,7 +261,8 @@ def create_data_path(
259261
)
260262
dnode = dnode[0]
261263
if ret != lib.LY_SUCCESS:
262-
if lib.ly_vecode(self.cdata) != lib.LYVE_SUCCESS:
264+
err = lib.ly_err_last(self.cdata)
265+
if err != ffi.NULL and err.vecode != lib.LYVE_SUCCESS:
263266
raise self.error("cannot create data path: %s", path)
264267
lib.ly_err_clean(self.cdata, ffi.NULL)
265268
if not dnode and not force_return_value:
@@ -340,6 +343,7 @@ def parse_data(
340343
strict: bool = False,
341344
validate_present: bool = False,
342345
validate_multi_error: bool = False,
346+
store_only: bool = False,
343347
) -> Optional[DNode]:
344348
if self.cdata is None:
345349
raise RuntimeError("context already destroyed")
@@ -350,6 +354,7 @@ def parse_data(
350354
opaq=opaq,
351355
ordered=ordered,
352356
strict=strict,
357+
store_only=store_only,
353358
)
354359
validation_flgs = validation_flags(
355360
no_state=no_state,
@@ -407,6 +412,7 @@ def parse_data_mem(
407412
strict: bool = False,
408413
validate_present: bool = False,
409414
validate_multi_error: bool = False,
415+
store_only: bool = False,
410416
) -> Optional[DNode]:
411417
return self.parse_data(
412418
fmt,
@@ -421,6 +427,7 @@ def parse_data_mem(
421427
strict=strict,
422428
validate_present=validate_present,
423429
validate_multi_error=validate_multi_error,
430+
store_only=store_only,
424431
)
425432

426433
def parse_data_file(
@@ -436,6 +443,7 @@ def parse_data_file(
436443
strict: bool = False,
437444
validate_present: bool = False,
438445
validate_multi_error: bool = False,
446+
store_only: bool = False,
439447
) -> Optional[DNode]:
440448
return self.parse_data(
441449
fmt,
@@ -450,6 +458,7 @@ def parse_data_file(
450458
strict=strict,
451459
validate_present=validate_present,
452460
validate_multi_error=validate_multi_error,
461+
store_only=store_only,
453462
)
454463

455464
def __iter__(self) -> Iterator[Module]:

0 commit comments

Comments
 (0)