Skip to content

Commit

Permalink
schema: add Pattern class
Browse files Browse the repository at this point in the history
This patches introduces a new Pattern class that represents a pattern
statement. A new pattern_details method is added to access the pattern
statements of a node.

Fixes: #92
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 29, 2024
1 parent f5b4e7a commit 0f43838
Show file tree
Hide file tree
Showing 5 changed files with 66 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 @@ -75,6 +75,7 @@
IfOrFeatures,
Module,
Must,
Pattern,
Revision,
SContainer,
SLeaf,
Expand Down Expand Up @@ -146,6 +147,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 pattern_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_pattern_details(self) -> Iterator[Pattern]:
if self.cdata.basetype == lib.LY_TYPE_UNION:
for t in self.union_types():
yield from t.all_pattern_details()
else:
yield from self.pattern_details()

def require_instance(self) -> Optional[bool]:
if self.cdata.basetype != self.LEAFREF:
return None
Expand Down
10 changes: 10 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LibyangError,
Module,
Must,
Pattern,
Revision,
SContainer,
SLeaf,
Expand Down Expand Up @@ -439,6 +440,15 @@ 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_pattern_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())

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 0f43838

Please sign in to comment.