Skip to content

Commit 387f56c

Browse files
committed
Simplify the YAQL expression context regexes
Copy the simpliciation for Jinja evaluator to the YAQL evaluator. Add more test cases to ensure correctness.
1 parent 8f3584f commit 387f56c

10 files changed

+36
-26
lines changed

orquesta/expressions/jinja.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ class JinjaEvaluator(expr_base.Evaluator):
5454
_regex_pattern = '{{.*?}}'
5555
_regex_parser = re.compile(_regex_pattern)
5656

57-
_regex_reference_pattern = r'[][a-zA-Z0-9_\'"\.()]*'
57+
_regex_ctx_ref_pattern = r'[][a-zA-Z0-9_\'"\.()]*'
5858
# match any of:
5959
# word boundary ctx(*)
6060
# word boundary ctx()*
6161
# word boundary ctx().*
6262
# word boundary ctx(*)*
6363
# word boundary ctx(*).*
64-
_regex_ctx_pattern = r'\bctx\([\'"]?{0}[\'"]?\)\.?{0}'.format(_regex_reference_pattern)
64+
_regex_ctx_pattern = r'\bctx\([\'"]?{0}[\'"]?\)\.?{0}'.format(_regex_ctx_ref_pattern)
6565
_regex_ctx_var_parser = re.compile(_regex_ctx_pattern)
6666

6767
_regex_var = r'[a-zA-Z0-9_-]+'

orquesta/expressions/yql.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ class YAQLEvaluator(expr_base.Evaluator):
5454
_regex_pattern = '<%.*?%>'
5555
_regex_parser = re.compile(_regex_pattern)
5656

57-
_regex_ctx_pattern = r'[a-zA-Z0-9_\'"\.\[\]\(\)]*'
58-
_regex_ctx_patterns = [
59-
r'^ctx\(\)\.%s' % _regex_ctx_pattern, # line start ctx().*
60-
r'^ctx\([\'|"]?{0}[\'|"]?\)[\.{0}]?'.format(_regex_ctx_pattern), # line start ctx(*).*
61-
r'[\s]ctx\(\)\.%s' % _regex_ctx_pattern, # whitespace ctx().*
62-
r'[\s]ctx\([\'|"]?{0}[\'|"]?\)[\.{0}]?'.format(_regex_ctx_pattern) # whitespace ctx(*).*
63-
]
64-
_regex_ctx_var = r'.*?(%s).*?' % '|'.join(_regex_ctx_patterns)
65-
_regex_ctx_var_parser = re.compile(_regex_ctx_var)
66-
67-
_regex_var = r'[a-zA-Z0-9_\-]+'
57+
_regex_ctx_ref_pattern = r'[][a-zA-Z0-9_\'"\.()]*'
58+
# match any of:
59+
# word boundary ctx(*)
60+
# word boundary ctx()*
61+
# word boundary ctx().*
62+
# word boundary ctx(*)*
63+
# word boundary ctx(*).*
64+
_regex_ctx_pattern = r'\bctx\([\'"]?{0}[\'"]?\)\.?{0}'.format(_regex_ctx_ref_pattern)
65+
_regex_ctx_var_parser = re.compile(_regex_ctx_pattern)
66+
67+
_regex_var = r'[a-zA-Z0-9_-]+'
6868
_regex_var_extracts = [
69-
r'(?<=^ctx\(\)\.)(\b%s\b)(?!\()\.?' % _regex_var, # extract x in ctx().x
70-
r'(?<=^ctx\()(\b%s\b)(?=\))\.?' % _regex_var, # extract x in ctx(x)
71-
r'(?<=^ctx\(\')(\b%s\b)(?=\'\))\.?' % _regex_var, # extract x in ctx('x')
72-
r'(?<=^ctx\(")(\b%s\b)(?="\))\.?' % _regex_var # extract x in ctx("x")
69+
r'(?<=\bctx\(\)\.)({})\b(?!\()\.?'.format(_regex_var), # extract x in ctx().x
70+
r'(?:\bctx\(({})\))'.format(_regex_var), # extract x in ctx(x)
71+
r'(?:\bctx\(\'({})\'\))'.format(_regex_var), # extract x in ctx('x')
72+
r'(?:\bctx\("({})"\))'.format(_regex_var) # extract x in ctx("x")
7373
]
7474

7575
_engine = yaql.language.factory.YaqlFactory().create()

orquesta/tests/unit/expressions/test_facade_jinja_ctx_by_dot_notation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class JinjaFacadeVariableExtractionTest(test_base.ExpressionFacadeEvaluatorTest)
2020

2121
def test_empty_extraction(self):
2222
expr = (
23-
'{{ just_text and _not_a_var and fooctx(foo) and fooctx("bar") and fooctx(\'fu\') '
24-
'and ctx(). and ctx().() and ctx().-foobar and ctx().foobar() }}')
23+
'{{ just_text and $not_a_var and notctx().bar and '
24+
'ctx(). and ctx().() and ctx().-foobar and ctx().foobar() }}'
25+
)
2526

2627
self.assertListEqual([], expr_base.extract_vars(expr))
2728

orquesta/tests/unit/expressions/test_facade_jinja_ctx_by_function_arg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def test_empty_extraction(self):
2323
'{{ just_text and $not_a_var and '
2424
'notctx(foo) and notctx("bar") and notctx(\'fu\') '
2525
'ctx("foo\') and ctx(\'foo") and ctx(foo") and '
26-
'ctx("foo) and ctx(foo\') and ctx(\'foo) and ctx(-foobar) }}'
26+
'ctx("foo) and ctx(foo\') and ctx(\'foo) and '
27+
'ctx(-foo) and ctx("-bar") and ctx(\'-fu\') and '
28+
'ctx(foo.bar) and ctx("foo.bar") and ctx(\'foo.bar\') and '
29+
'ctx(foo()) and ctx("foo()") and ctx(\'foo()\') }}'
2730
)
2831

2932
self.assertListEqual([], expr_base.extract_vars(expr))

orquesta/tests/unit/expressions/test_facade_yaql_ctx_by_dot_notation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
class YAQLFacadeVariableExtractionTest(test_base.ExpressionFacadeEvaluatorTest):
2020

2121
def test_empty_extraction(self):
22-
expr = '<% just_text and $not_a_var and fooctx(foo) and fooctx("bar") and fooctx(\'fu\') %>'
22+
expr = (
23+
'<% just_text and $not_a_var and notctx().bar and '
24+
'ctx(). and ctx().() and ctx().-foobar and ctx().foobar() %>'
25+
)
2326

2427
self.assertListEqual([], expr_base.extract_vars(expr))
2528

orquesta/tests/unit/expressions/test_facade_yaql_ctx_by_function_arg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def test_empty_extraction(self):
2323
'<% just_text and $not_a_var and '
2424
'notctx(foo) and notctx("bar") and notctx(\'fu\') '
2525
'ctx("foo\') and ctx(\'foo") and ctx(foo") and '
26-
'ctx("foo) and ctx(foo\') and ctx(\'foo) %>'
26+
'ctx("foo) and ctx(foo\') and ctx(\'foo) and '
27+
'ctx(-foo) and ctx("-bar") and ctx(\'-fu\') and '
28+
'ctx(foo.bar) and ctx("foo.bar") and ctx(\'foo.bar\') and '
29+
'ctx(foo()) and ctx("foo()") and ctx(\'foo()\') %>'
2730
)
2831

2932
self.assertListEqual([], expr_base.extract_vars(expr))

orquesta/tests/unit/expressions/test_jinja_ctx_by_dot_notation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setUpClass(cls):
2323
super(JinjaVariableExtractionTest, cls).setUpClass()
2424

2525
def test_empty_extraction(self):
26-
expr = '{{ just_text and _not_a_var and fooctx().bar }}'
26+
expr = '{{ just_text and $not_a_var and notctx().bar }}'
2727

2828
self.assertListEqual([], self.evaluator.extract_vars(expr))
2929

orquesta/tests/unit/expressions/test_jinja_ctx_by_function_arg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setUpClass(cls):
2323
super(JinjaVariableExtractionTest, cls).setUpClass()
2424

2525
def test_empty_extraction(self):
26-
expr = '{{ just_text and _not_a_var and fooctx(foo) and fooctx("bar") and fooctx(\'fu\') }}'
26+
expr = '{{ just_text and $not_a_var and notctx(foo) and notctx("bar") and notctx(\'fu\') }}'
2727

2828
self.assertListEqual([], self.evaluator.extract_vars(expr))
2929

orquesta/tests/unit/expressions/test_yaql_ctx_by_dot_notation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setUpClass(cls):
2323
super(YAQLVariableExtractionTest, cls).setUpClass()
2424

2525
def test_empty_extraction(self):
26-
expr = '<% just_text and $not_a_var and fooctx().bar %>'
26+
expr = '<% just_text and $not_a_var and notctx().bar %>'
2727

2828
self.assertListEqual([], self.evaluator.extract_vars(expr))
2929

orquesta/tests/unit/expressions/test_yaql_ctx_by_function_arg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setUpClass(cls):
2323
super(YAQLVariableExtractionTest, cls).setUpClass()
2424

2525
def test_empty_extraction(self):
26-
expr = '<% just_text and $not_a_var and fooctx(foo) and fooctx("bar") and fooctx(\'fu\') %>'
26+
expr = '<% just_text and $not_a_var and notctx(foo) and notctx("bar") and notctx(\'fu\') %>'
2727

2828
self.assertListEqual([], self.evaluator.extract_vars(expr))
2929

0 commit comments

Comments
 (0)