Skip to content

Commit d1a62b0

Browse files
author
Andrei Neagu
committed
added proper escaping
1 parent 985dfe1 commit d1a62b0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

packages/service-integration/src/service_integration/cli/_escaping.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from pathlib import Path
23
from typing import Annotated
34

@@ -6,6 +7,24 @@
67
from ..osparc_config import OSPARC_CONFIG_DIRNAME
78

89

10+
def escape_dollar_brace(text: str) -> str:
11+
"""
12+
Replaces all '$${' sequences with '$$$${' unless they are part of an
13+
existing '$$$${' sequence.
14+
15+
Args:
16+
text: The input string.
17+
18+
Returns:
19+
The modified string. This function will NOT return None.
20+
"""
21+
# The pattern finds '$${' that is not preceded by another '$'.
22+
pattern = r"(?<!\$)\$\${"
23+
replacement = "$$$${"
24+
25+
return re.sub(pattern, replacement, text)
26+
27+
928
def legacy_escape(
1029
osparc_config_dirname: Annotated[
1130
Path,
@@ -18,7 +37,7 @@ def legacy_escape(
1837
"""Escapes the `$${` with `$$$${` in all .y*ml files in the osparc config directory."""
1938
for file in osparc_config_dirname.glob("*.y*ml"):
2039
read_text = file.read_text()
21-
replaced_text = read_text.replace("$${", "$$$${")
40+
replaced_text = escape_dollar_brace(read_text)
2241
if read_text != replaced_text:
2342
print(f"Escaped sequnce in {file}")
2443
file.write_text(replaced_text)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from service_integration.cli._escaping import escape_dollar_brace
3+
4+
5+
@pytest.mark.parametrize(
6+
"to_escape, escaped",
7+
[
8+
("some text", "some text"),
9+
("$${escapes}", "$$$${escapes}"),
10+
("$$${preserves}", "$$${preserves}"),
11+
("$$$${preserves}", "$$$${preserves}"),
12+
("$$$$${preserves}", "$$$$${preserves}"),
13+
(
14+
"$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}",
15+
"$$$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}",
16+
),
17+
],
18+
)
19+
def test_escape_dollar_brace(to_escape: str, escaped: str):
20+
assert escape_dollar_brace(to_escape) == escaped

0 commit comments

Comments
 (0)