Skip to content

Commit

Permalink
Merge branch 'main' into feat-implement-throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
pehlicd authored Apr 14, 2024
2 parents 7e6f9a3 + f25d009 commit b4eef09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
25 changes: 16 additions & 9 deletions keep/iohandler/iohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ def extract_keep_functions(self, text):
elif (
text[i] == quote_char
and not escape_next
and str(text[i + 1]).isalpha()
== False # end of statement, arg, etc. if it's a letter, we need to escape it
and (
str(text[i + 1]).isalnum() == False
and str(text[i + 1]) != " "
) # end of statement, arg, etc. if its alpha numeric or whitespace, we just need to escape it
):
in_string = False
quote_char = ""
Expand Down Expand Up @@ -166,9 +168,15 @@ def parse(self, string, safe=False, default=""):
token, escapes = tokens[0]
token_to_replace = token
try:
escapes_counter = 0
if escapes:
for escape in escapes:
token = token[:escape] + "\\" + token[escape:]
token = (
token[: escape + escapes_counter]
+ "\\"
+ token[escape + escapes_counter :]
)
escapes_counter += 1 # we need to increment the counter because we added a character
val = self._parse_token(token)
except Exception as e:
# trim stacktrace since we have limitation on the error message
Expand Down Expand Up @@ -263,7 +271,9 @@ def _parse(self, tree):
# https://github.com/keephq/keep/issues/137
import html

tree = ast.parse(html.unescape(token))
tree = ast.parse(
html.unescape(token.replace("\r\n", "").replace("\n", ""))
)
else:
# for strings such as "45%\n", we need to escape
tree = ast.parse(token.encode("unicode_escape"))
Expand Down Expand Up @@ -416,12 +426,9 @@ def __get_short_urls(self, urls: list) -> dict:
if __name__ == "__main__":
# debug & test
context_manager = ContextManager("keep")
context_manager.event_context = {
"notexist": "it actually exists",
"name": "this is a test",
}
context_manager.event_context = {"name": ""}
iohandler = IOHandler(context_manager)
iohandler.parse(
"{{#alert.notexist}}{{.}}{{/alert.notexist}}{{^alert.notexist}}{{alert.name}}{{/alert.notexist}}",
"keep.slice('{{alert.name}}', 0, 254)",
safe=True,
)
20 changes: 20 additions & 0 deletions tests/test_iohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,23 @@ def test_if_else_in_template_not_existing(mocked_context_manager):
safe=True,
)
assert rendered == "this is a test"


def test_escaped_quotes_with_with_space(context_manager):
iohandler = IOHandler(context_manager)
template = "keep.split('some string with 'quotes and with space' after', ',')"
extracted_functions = iohandler.extract_keep_functions(template)
# Assuming the method can handle escaped quotes within function arguments
assert (
len(extracted_functions) == 1
), "Expected one function to be extracted with escaped quotes inside arguments."


def test_escaped_quotes_with_with_newlines(context_manager):
iohandler = IOHandler(context_manager)
template = "keep.split('some string with 'quotes and with space' \r\n after', ',')"
extracted_functions = iohandler.extract_keep_functions(template)
# Assuming the method can handle escaped quotes within function arguments
assert (
len(extracted_functions) == 1
), "Expected one function to be extracted with escaped quotes inside arguments."

0 comments on commit b4eef09

Please sign in to comment.