Skip to content

Commit

Permalink
Extending pattern options
Browse files Browse the repository at this point in the history
This patch introduces patterns_details and all_patterns_details
functions, which allows user to get also associated error-message
  • Loading branch information
steweg committed Jan 29, 2024
1 parent f26b56a commit a70d349
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,21 @@ struct lysc_must {
struct lysc_ext_instance *exts;
};

struct pcre2_real_code;
typedef struct pcre2_real_code pcre2_code;

struct lysc_pattern {
const char *expr;
pcre2_code *code;
const char *dsc;
const char *ref;
const char *emsg;
const char *eapptag;
struct lysc_ext_instance *exts;
uint32_t inverted : 1;
uint32_t refcount : 31;
};

#define LYSP_RESTR_PATTERN_ACK ...
#define LYSP_RESTR_PATTERN_NACK ...

Expand Down
2 changes: 2 additions & 0 deletions libyang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
IfNotFeature,
IfOrFeatures,
Module,
Pattern,
Revision,
SContainer,
SLeaf,
Expand Down Expand Up @@ -144,6 +145,7 @@
"NodeTypeRemoved",
"OrderedByUserAdded",
"OrderedByUserRemoved",
"Pattern",
"PatternAdded",
"PatternRemoved",
"PresenceAdded",
Expand Down
36 changes: 36 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ class Bit(_EnumBit):
pass


# -------------------------------------------------------------------------------------
class Pattern:
__slots__ = ("context", "cdata")

def __init__(self, context: "libyang.Context", cdata):
self.context = context
self.cdata = cdata # C type: "struct lysc_pattern *"

def expression(self) -> str:
return c2str(self.cdata.expr)

def inverted(self) -> bool:
return self.cdata.inverted

def error_message(self) -> Optional[str]:
return c2str(self.cdata.emsg) if self.cdata.emsg != ffi.NULL else None


# -------------------------------------------------------------------------------------
class Type:
__slots__ = ("context", "cdata", "cdata_parsed", "__dict__")
Expand Down Expand Up @@ -681,6 +699,24 @@ def all_patterns(self) -> Iterator[Tuple[str, bool]]:
else:
yield from self.patterns()

def patterns_details(self) -> Iterator[Pattern]:
if self.cdata.basetype != self.STRING:
return
t = ffi.cast("struct lysc_type_str *", self.cdata)
if t.patterns == ffi.NULL:
return
for p in ly_array_iter(t.patterns):
if not p:
continue
yield Pattern(self.context, p)

def all_patterns_details(self) -> Iterator[Pattern]:
if self.cdata.basetype == lib.LY_TYPE_UNION:
for t in self.union_types():
yield from t.all_patterns_details()
else:
yield from self.patterns_details()

def require_instance(self) -> Optional[bool]:
if self.cdata.basetype != self.LEAFREF:
return None
Expand Down
12 changes: 12 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
IOType,
LibyangError,
Module,
Pattern,
Revision,
SContainer,
SLeaf,
Expand Down Expand Up @@ -438,6 +439,17 @@ def test_leaf_type_pattern(self):
t = leaf.type()
self.assertIsInstance(t, Type)
self.assertEqual(list(t.patterns()), [("[a-z.]+", False), ("1", True)])
patterns = list(t.all_patterns_details())
self.assertEqual(len(patterns), 2)
self.assertIsInstance(patterns[0], Pattern)
self.assertEqual(patterns[0].expression(), "[a-z.]+")
self.assertFalse(patterns[0].inverted())
self.assertEqual(patterns[0].error_message(), "ERROR1")
self.assertEqual(patterns[1].expression(), "1")
self.assertTrue(patterns[1].inverted())
self.assertIsNone(patterns[1].error_message())
# list(t.all_patterns_details()),
# [("[a-z.]+", False, "ERROR1"), ("1", True, None)],

def test_leaf_type_union(self):
leaf = next(self.ctx.find_path("/yolo-system:conf/yolo-system:number"))
Expand Down
4 changes: 3 additions & 1 deletion tests/yang/yolo/yolo-system.yang
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ module yolo-system {
}
leaf host {
type string {
pattern "[a-z.]+";
pattern "[a-z.]+" {
error-message "ERROR1";
}
pattern "1" {
modifier "invert-match";
}
Expand Down

0 comments on commit a70d349

Please sign in to comment.