Skip to content

Commit

Permalink
Merge pull request #98 from StackStorm/fix-unicode-context
Browse files Browse the repository at this point in the history
Convert strings in context to unicode
  • Loading branch information
m4dcoder authored Nov 5, 2018
2 parents ad06833 + 363ab08 commit 73e06ae
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 39 deletions.
13 changes: 9 additions & 4 deletions orquesta/expressions/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from orquesta.expressions import base
from orquesta.expressions.functions import base as functions
from orquesta.utils import expression as utils
from orquesta.utils import strings as strings_utils


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -139,7 +140,7 @@ def validate(cls, text):

@classmethod
def _evaluate_and_expand(cls, text, data=None):
output = text
output = strings_utils.unicode(text)
exprs = cls._regex_parser.findall(text)
block_exprs = cls._regex_block_parser.findall(text)
ctx = cls.contextualize(data)
Expand All @@ -164,9 +165,9 @@ def _evaluate_and_expand(cls, text, data=None):
# raise an exception with error description.
if not isinstance(result, jinja2.runtime.StrictUndefined):
if len(exprs) > 1 or block_exprs or len(output) > len(expr):
output = output.replace(expr, str(result))
output = output.replace(expr, strings_utils.unicode(result, force=True))
else:
output = result
output = strings_utils.unicode(result)

# Evaluate jinja block(s) after inline expressions are evaluated.
if block_exprs and isinstance(output, six.string_types):
Expand All @@ -177,7 +178,11 @@ def _evaluate_and_expand(cls, text, data=None):
output = cls._evaluate_and_expand(output, data)

except jinja2.exceptions.UndefinedError as e:
raise JinjaEvaluationException(str(getattr(e, 'message', e)))
msg = "Unable to evaluate expression '%s'. %s: %s"
raise JinjaEvaluationException(msg % (expr, e.__class__.__name__, str(e)))
except Exception as e:
msg = "Unable to evaluate expression '%s'. %s: %s"
raise JinjaEvaluationException(msg % (expr, e.__class__.__name__, str(e)))

return output

Expand Down
20 changes: 12 additions & 8 deletions orquesta/expressions/yql.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from orquesta.expressions import base
from orquesta.expressions.functions import base as functions
from orquesta.utils import expression as utils
from orquesta.utils import strings as strings_utils


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,7 +113,7 @@ def evaluate(cls, text, data=None):
if data and not isinstance(data, dict):
raise ValueError('Provided data is not typeof dict.')

output = text
output = strings_utils.unicode(text)
exprs = cls._regex_parser.findall(text)
ctx = cls.contextualize(data)

Expand All @@ -128,17 +129,20 @@ def evaluate(cls, text, data=None):
result = cls.evaluate(result, data)

if len(exprs) > 1 or len(output) > len(expr):
output = output.replace(expr, str(result))
output = output.replace(expr, strings_utils.unicode(result, force=True))
else:
output = result
output = strings_utils.unicode(result)

except KeyError as e:
raise YaqlEvaluationException(
"Unable to resolve key '%s' in expression '%s' from context." %
(str(getattr(e, 'message', e)).strip("'"), expr)
)
error = str(getattr(e, 'message', e)).strip("'")
msg = "Unable to resolve key '%s' in expression '%s' from context."
raise YaqlEvaluationException(msg % (error, expr))
except (yaql_exc.YaqlException, ValueError, TypeError) as e:
raise YaqlEvaluationException(str(getattr(e, 'message', e)).strip("'"))
msg = "Unable to evaluate expression '%s'. %s: %s"
raise YaqlEvaluationException(msg % (expr, e.__class__.__name__, str(e).strip("'")))
except Exception as e:
msg = "Unable to evaluate expression '%s'. %s: %s"
raise YaqlEvaluationException(msg % (expr, e.__class__.__name__, str(e)))

return output

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def test_bad_item_key(self):

expected_errors = [
{
'message': 'Item does not have key "y".',
'message': (
'Unable to evaluate expression \'<% item(y) %>\'. '
'ExpressionEvaluationException: Item does not have key "y".'
),
'task_id': 'task1'
}
]
Expand Down Expand Up @@ -73,7 +76,10 @@ def test_bad_item_type(self):

expected_errors = [
{
'message': 'Item is not type of dict.',
'message': (
'Unable to evaluate expression \'<% item(x) %>\'. '
'ExpressionEvaluationException: Item is not type of dict.'
),
'task_id': 'task1'
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ def test_workflow_input_seq_ref_error(self):
"""

expected_errors = [
{'message': 'Unknown function "#property#value"'}
{
'message': (
'Unable to evaluate expression \'<% ctx().y.value %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#value"'
)
}
]

spec = specs.WorkflowSpec(wf_def)
Expand Down Expand Up @@ -104,7 +109,12 @@ def test_workflow_vars_seq_ref_error(self):
"""

expected_errors = [
{'message': 'Unknown function "#property#value"'}
{
'message': (
'Unable to evaluate expression \'<% ctx().y.value %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#value"'
)
}
]

spec = specs.WorkflowSpec(wf_def)
Expand Down Expand Up @@ -150,7 +160,10 @@ def test_task_transition_criteria_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1',
'task_transition_id': 'task2__0'
}
Expand Down Expand Up @@ -210,12 +223,18 @@ def test_multiple_task_transition_criteria_errors(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1',
'task_transition_id': 'task3__0'
},
{
'message': 'Unknown function "#property#foobar"',
'message': (
'Unable to evaluate expression \'<% ctx().fubar.foobar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#foobar"'
),
'task_id': 'task2',
'task_transition_id': 'task3__0'
}
Expand Down Expand Up @@ -272,7 +291,10 @@ def test_task_transition_publish_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1',
'task_transition_id': 'task2__0'
}
Expand Down Expand Up @@ -325,7 +347,10 @@ def test_task_transition_publish_seq_ref_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#value"',
'message': (
'Unable to evaluate expression \'<% ctx().y.value %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#value"'
),
'task_id': 'task1',
'task_transition_id': 'task2__0'
}
Expand Down Expand Up @@ -370,7 +395,10 @@ def test_get_start_tasks_with_task_action_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
}
]
Expand Down Expand Up @@ -410,7 +438,10 @@ def test_get_start_tasks_via_get_next_tasks_with_task_action_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
}
]
Expand Down Expand Up @@ -450,7 +481,10 @@ def test_get_next_tasks_with_task_action_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task2'
}
]
Expand Down Expand Up @@ -497,7 +531,10 @@ def test_get_start_tasks_with_task_input_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
}
]
Expand Down Expand Up @@ -539,7 +576,10 @@ def test_get_start_tasks_via_get_next_tasks_with_task_input_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
}
]
Expand Down Expand Up @@ -581,7 +621,10 @@ def test_get_next_tasks_with_task_input_error(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task2'
}
]
Expand Down Expand Up @@ -633,11 +676,17 @@ def test_get_start_tasks_with_multiple_task_action_and_input_errors(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
},
{
'message': 'Unknown function "#property#foobar"',
'message': (
'Unable to evaluate expression \'<% ctx().fubar.foobar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#foobar"'
),
'task_id': 'task2'
}
]
Expand Down Expand Up @@ -684,11 +733,17 @@ def test_get_start_tasks_via_get_next_tasks_with_multiple_task_action_and_input_

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task1'
},
{
'message': 'Unknown function "#property#foobar"',
'message': (
'Unable to evaluate expression \'<% ctx().fubar.foobar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#foobar"'
),
'task_id': 'task2'
}
]
Expand Down Expand Up @@ -731,11 +786,17 @@ def test_get_next_tasks_with_multiple_task_action_and_input_errors(self):

expected_errors = [
{
'message': 'Unknown function "#property#fubar"',
'message': (
'Unable to evaluate expression \'<% ctx().foobar.fubar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#fubar"'
),
'task_id': 'task2'
},
{
'message': 'Unknown function "#property#foobar"',
'message': (
'Unable to evaluate expression \'<% ctx().fubar.foobar %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#foobar"'
),
'task_id': 'task3'
}
]
Expand Down Expand Up @@ -800,7 +861,12 @@ def test_workflow_output_seq_ref_error(self):
"""

expected_errors = [
{'message': 'Unknown function "#property#value"'}
{
'message': (
'Unable to evaluate expression \'<% ctx().y.value %>\'. '
'NoFunctionRegisteredException: Unknown function "#property#value"'
)
}
]

spec = specs.WorkflowSpec(wf_def)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from orquesta import exceptions as exc
from orquesta.expressions import jinja
from orquesta.tests.unit import base
from orquesta.utils import plugin
Expand Down Expand Up @@ -45,7 +44,7 @@ def test_basic_eval_undefined(self):
data = {}

self.assertRaises(
exc.VariableUndefinedError,
jinja.JinjaEvaluationException,
self.evaluator.evaluate,
expr,
data
Expand Down Expand Up @@ -104,7 +103,7 @@ def test_eval_recursive_undefined(self):
}

self.assertRaises(
exc.VariableUndefinedError,
jinja.JinjaEvaluationException,
self.evaluator.evaluate,
expr,
data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from orquesta import exceptions as exc
from orquesta.expressions import yql
from orquesta.tests.unit import base
from orquesta.utils import plugin
Expand Down Expand Up @@ -45,7 +44,7 @@ def test_basic_eval_undefined(self):
data = {}

self.assertRaises(
exc.VariableUndefinedError,
yql.YaqlEvaluationException,
self.evaluator.evaluate,
expr,
data
Expand Down
Loading

0 comments on commit 73e06ae

Please sign in to comment.