Skip to content

Commit 749bb15

Browse files
hf-kkleinKonstantin
andauthored
chore: apply same sanitizing logic to both AHB and Condition expression (#573)
* chore: apply same sanitizing logic to both AHB and Condition expression * the tests are right. --------- Co-authored-by: Konstantin <[email protected]>
1 parent 0472735 commit 749bb15

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/ahbicht/expressions/ahb_expression_parser.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# pylint: disable=anomalous-backslash-in-string
1414
from ahbicht.expressions import parsing_logger
15+
from ahbicht.expressions.sanitizer import sanitize_expression
1516
from ahbicht.utility_functions import tree_copy
1617

1718
GRAMMAR = """
@@ -31,12 +32,6 @@
3132
# and CTRL+F for "Mus[2]" in the unittest that fails if you remove the lookahead.
3233
_parser = Lark(GRAMMAR, start="ahb_expression")
3334

34-
_replacements: dict[str, str] = {
35-
"\u00a0": " ", # no-break space,
36-
"V": "∨", # Vogel-V != logical OR
37-
"v": "∨",
38-
}
39-
4035

4136
@tree_copy
4237
@lru_cache(maxsize=1024)
@@ -51,9 +46,7 @@ def parse_ahb_expression_to_single_requirement_indicator_expressions(ahb_express
5146
:return parsed_tree:
5247
"""
5348
try:
54-
if ahb_expression is not None:
55-
for key, value in _replacements.items():
56-
ahb_expression = ahb_expression.replace(key, value)
49+
ahb_expression = sanitize_expression(ahb_expression)
5750
parsed_tree = _parser.parse(ahb_expression)
5851
parsing_logger.debug("Successfully parsed '%s' as AHB expression", ahb_expression)
5952
except (UnexpectedEOF, UnexpectedCharacters, TypeError) as eof:

src/ahbicht/expressions/condition_expression_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
from ahbicht.condition_node_distinction import derive_condition_node_type
1616
from ahbicht.expressions import parsing_logger
17+
from ahbicht.expressions.sanitizer import sanitize_expression
1718
from ahbicht.models.categorized_key_extract import CategorizedKeyExtract
1819
from ahbicht.models.condition_node_type import ConditionNodeType
1920
from ahbicht.utility_functions import tree_copy
2021

2122
GRAMMAR = r"""
2223
?expression: expression "O"i expression -> or_composition
23-
| expression "∨" expression -> or_composition
24+
| expression "∨" expression -> or_composition // the logical or
25+
| expression "V"i expression -> or_composition // a 'v' for those who first chose to introduce logical symbols but now can't find them on their keyboard
2426
| expression "X"i expression -> xor_composition
2527
| expression "⊻" expression -> xor_composition
2628
| expression "U"i expression -> and_composition
@@ -59,6 +61,7 @@ def parse_condition_expression_to_tree(condition_expression: str) -> Tree[Token]
5961
:return parsed_tree: Tree
6062
"""
6163
try:
64+
condition_expression = sanitize_expression(condition_expression)
6265
parsed_tree = _parser.parse(condition_expression)
6366
parsing_logger.debug("Successfully parsed '%s' as condition expression", condition_expression)
6467
except (UnexpectedEOF, UnexpectedCharacters, TypeError) as eof:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""contains a function to sanitize user input/expressions from the AHBs"""
2+
3+
from typing import Literal, Optional, overload
4+
5+
_replacements: dict[str, str] = {
6+
"\u00a0": " ", # no-break space,
7+
"V": "∨", # Vogel-V != logical OR
8+
"v": "∨",
9+
}
10+
11+
12+
@overload
13+
def sanitize_expression(expression: Literal[None]) -> Literal[None]: ...
14+
@overload
15+
def sanitize_expression(expression: str) -> str: ...
16+
17+
18+
def sanitize_expression(expression: Optional[str]) -> Optional[str]:
19+
"""
20+
fixes some common issues with expressions from the AHBs
21+
"""
22+
if expression is None:
23+
return None
24+
for key, value in _replacements.items():
25+
expression = expression.replace(key, value)
26+
return expression.strip()
27+
28+
29+
__all__ = ["sanitize_expression"]

0 commit comments

Comments
 (0)