From c7831a75e1f61ee97da07751430f41b95112af4f Mon Sep 17 00:00:00 2001 From: Brad House Date: Sun, 16 Feb 2025 10:18:20 -0500 Subject: [PATCH] data: option to allow json int/bool as strings Depends on https://github.com/CESNET/libyang/pull/2344 Add support for new option to python bindings. Signed-off-by: Brad House --- cffi/cdefs.h | 1 + cffi/source.c | 4 ++-- libyang/context.py | 6 ++++++ libyang/data.py | 3 +++ pylintrc | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cffi/cdefs.h b/cffi/cdefs.h index aa750042..de05f4a1 100644 --- a/cffi/cdefs.h +++ b/cffi/cdefs.h @@ -321,6 +321,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_JSON_STRING_DATATYPES ... #define LYD_VALIDATE_NO_STATE ... #define LYD_VALIDATE_PRESENT ... diff --git a/cffi/source.c b/cffi/source.c index b54ba0de..34e18b0e 100644 --- a/cffi/source.c +++ b/cffi/source.c @@ -6,6 +6,6 @@ #include #include -#if (LY_VERSION_MAJOR != 3) -#error "This version of libyang bindings only works with libyang 3.x" +#if LY_VERSION_MAJOR * 10000 + LY_VERSION_MINOR * 100 + LY_VERSION_MICRO < 30801 +#error "This version of libyang bindings only works with libyang soversion 3.8.1+" #endif diff --git a/libyang/context.py b/libyang/context.py index f9bd5a57..b50600de 100644 --- a/libyang/context.py +++ b/libyang/context.py @@ -533,6 +533,7 @@ def parse_data( validate_multi_error: bool = False, store_only: bool = False, json_null: bool = False, + json_string_datatypes: bool = False, ) -> Optional[DNode]: if self.cdata is None: raise RuntimeError("context already destroyed") @@ -545,6 +546,7 @@ def parse_data( strict=strict, store_only=store_only, json_null=json_null, + json_string_datatypes=json_string_datatypes, ) validation_flgs = validation_flags( no_state=no_state, @@ -604,6 +606,7 @@ def parse_data_mem( validate_multi_error: bool = False, store_only: bool = False, json_null: bool = False, + json_string_datatypes: bool = False, ) -> Optional[DNode]: return self.parse_data( fmt, @@ -620,6 +623,7 @@ def parse_data_mem( validate_multi_error=validate_multi_error, store_only=store_only, json_null=json_null, + json_string_datatypes=json_string_datatypes, ) def parse_data_file( @@ -637,6 +641,7 @@ def parse_data_file( validate_multi_error: bool = False, store_only: bool = False, json_null: bool = False, + json_string_datatypes: bool = False, ) -> Optional[DNode]: return self.parse_data( fmt, @@ -653,6 +658,7 @@ def parse_data_file( validate_multi_error=validate_multi_error, store_only=store_only, json_null=json_null, + json_string_datatypes=json_string_datatypes, ) def __iter__(self) -> Iterator[Module]: diff --git a/libyang/data.py b/libyang/data.py index 0d63d3c9..0d6f6cfd 100644 --- a/libyang/data.py +++ b/libyang/data.py @@ -117,6 +117,7 @@ def parser_flags( strict: bool = False, store_only: bool = False, json_null: bool = False, + json_string_datatypes: bool = False, ) -> int: flags = 0 if lyb_mod_update: @@ -135,6 +136,8 @@ def parser_flags( flags |= lib.LYD_PARSE_STORE_ONLY if json_null: flags |= lib.LYD_PARSE_JSON_NULL + if json_string_datatypes: + flags |= lib.LYD_PARSE_JSON_STRING_DATATYPES return flags diff --git a/pylintrc b/pylintrc index acf27338..cd4638b7 100644 --- a/pylintrc +++ b/pylintrc @@ -494,7 +494,7 @@ valid-metaclass-classmethod-first-arg=mcs [DESIGN] # Maximum number of arguments for function / method. -max-args=15 +max-args=20 # Maximum number of attributes for a class (see R0902). max-attributes=20