A less convoluted and robust way to expand the context as variables

This commit is contained in:
Fabio Manganiello 2018-06-06 17:14:44 +02:00
parent 9984b75895
commit 850a4f2146
2 changed files with 20 additions and 33 deletions

View file

@ -100,6 +100,9 @@ class Request(Message):
@classmethod @classmethod
def expand_value_from_context(cls, value, **context): def expand_value_from_context(cls, value, **context):
for (k, v) in context.items():
exec('{}={}'.format(k, v))
parsed_value = '' parsed_value = ''
while value: while value:
m = re.match('([^\$]*)(\${\s*(.+?)\s*})(.*)', value) m = re.match('([^\$]*)(\${\s*(.+?)\s*})(.*)', value)
@ -107,16 +110,7 @@ class Request(Message):
prefix = m.group(1); expr = m.group(2); prefix = m.group(1); expr = m.group(2);
inner_expr = m.group(3); value = m.group(4) inner_expr = m.group(3); value = m.group(4)
m = re.match('([^.\[\]()]+)(.*)', inner_expr)
context_argname = m.group(1)
path = m.group(2)
if context_argname in context:
try: try:
try:
context_value = eval("context['{}']{}".format(
context_argname, path if path else ''))
except:
context_value = eval(inner_expr) context_value = eval(inner_expr)
if callable(context_value): if callable(context_value):
@ -132,16 +126,6 @@ class Request(Message):
if isinstance(context_value, list) if isinstance(context_value, list)
or isinstance(context_value, dict) or isinstance(context_value, dict)
else str(context_value)) else str(context_value))
else:
try:
expanded_expr = eval(inner_expr)
parsed_value += prefix
if expanded_expr is not None:
parsed_value += expanded_expr
except Exception as e:
logging.exception(e)
parsed_value += prefix + expr
else: else:
parsed_value += value parsed_value += value
value = '' value = ''

View file

@ -210,6 +210,9 @@ class IfProcedure(Procedure):
def execute(self, **context): def execute(self, **context):
for (k, v) in context.items():
exec('{}={}'.format(k, v))
condition_true = eval(self.condition) condition_true = eval(self.condition)
response = Response() response = Response()