Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending pattern options #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@ -12,6 +12,7 @@
IOType,
LibyangError,
Module,
Pattern,
Revision,
SContainer,
SLeaf,
Expand Down Expand Up @@ -438,6 +439,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
Loading