-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmatches.py
More file actions
66 lines (56 loc) · 2.52 KB
/
matches.py
File metadata and controls
66 lines (56 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2023-2025 Buf Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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
invalid_patterns = [
r"\\[1-9]", # backreference
r"\\k<\w+>", # backreference
r"\(\?\=", # lookahead
r"\(\?\!", # negative lookahead
r"\(\?\<\=", # lookbehind
r"\(\?\<\!", # negative lookbehind
r"\\c[A-Z]", # control character
r"\\u[0-9a-fA-F]{4}", # UTF-16 code-unit
r"\\0(?!\d)", # NUL
r"\[\\b.*\]", # Backspace eg: [\b]
]
def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result:
"""Return True if the given pattern matches text. False otherwise.
CEL uses RE2 syntax which diverges from Python re in various ways. Ideally, we
would use the google-re2 package, which is an extra dep in celpy, but at press
time it does not provide a pre-built binary for the latest version of Python (3.13)
which means those using this version will run into many issues.
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)
# Simulate re2 by failing on any patterns not compatible with re2 syntax
for invalid_pattern in invalid_patterns:
r = re.search(invalid_pattern, pattern)
if r is not None:
msg = f"error evaluating pattern {pattern}, invalid RE2 syntax"
raise celpy.CELEvalError(msg)
try:
m = re.search(pattern, text)
except re.error as ex:
return celpy.CELEvalError("match error", ex.__class__, ex.args)
return celtypes.BoolType(m is not None)