-
Notifications
You must be signed in to change notification settings - Fork 9
Add ability to specify custom regex matcher #325
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
| import re | ||
|
|
||
| import celpy | ||
| from celpy import celtypes | ||
|
|
||
| # Patterns that are supported in Python's re package and not in re2. | ||
| # RE2: https://github.com/google/re2/wiki/syntax | ||
|
|
@@ -30,10 +29,11 @@ | |
| r"\\u[0-9a-fA-F]{4}", # UTF-16 code-unit | ||
| r"\\0(?!\d)", # NUL | ||
| r"\[\\b.*\]", # Backspace eg: [\b] | ||
| r"\\Z", # End of text (only lowercase z is supported in re2) | ||
| ] | ||
|
|
||
|
|
||
| def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result: | ||
| def matches(text: str, pattern: str) -> bool: | ||
| """Return True if the given pattern matches text. False otherwise. | ||
|
|
||
| CEL uses RE2 syntax which diverges from Python re in various ways. Ideally, we | ||
|
|
@@ -43,14 +43,13 @@ def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result: | |
|
|
||
| Instead of foisting this issue on users, we instead mimic re2 syntax by failing | ||
| to compile the regex for patterns not compatible with re2. | ||
| """ | ||
| if not isinstance(text, celtypes.StringType): | ||
| msg = "invalid argument for text, expected string" | ||
| raise celpy.CELEvalError(msg) | ||
| if not isinstance(pattern, celtypes.StringType): | ||
| msg = "invalid argument for pattern, expected string" | ||
| raise celpy.CELEvalError(msg) | ||
|
|
||
| Users can choose to override this behavior by providing their own custom matches | ||
| function via the Config. | ||
|
|
||
| Raises: | ||
| celpy.CELEvalError: If pattern contains invalid re2 syntax. | ||
|
||
| """ | ||
| # Simulate re2 by failing on any patterns not compatible with re2 syntax | ||
| for invalid_pattern in invalid_patterns: | ||
| r = re.search(invalid_pattern, pattern) | ||
|
|
@@ -61,6 +60,7 @@ def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result: | |
| try: | ||
| m = re.search(pattern, text) | ||
| except re.error as ex: | ||
| return celpy.CELEvalError("match error", ex.__class__, ex.args) | ||
| msg = "match error" | ||
| raise celpy.CELEvalError(msg, ex.__class__, ex.args) from ex | ||
|
|
||
| return celtypes.BoolType(m is not None) | ||
| return m is not None | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,7 @@ | |
| class StringFormat: | ||
| """An implementation of string.format() in CEL.""" | ||
|
|
||
| def __init__(self, locale: str): | ||
| self.locale = locale | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| def __init__(self): | ||
| self.fmt = None | ||
|
|
||
| def format(self, fmt: celtypes.Value, args: celtypes.Value) -> celpy.Result: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
honestly haven't kept up with type hints in Python but should we be using
collections.abc.Callableinstead, sincetyping.Callableseems to be a deprecated alias as of 3.9?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice. Thanks. Wonder why the linter didn't catch that. 🤷 . Fixed.