forked from platypush/platypush
A less convoluted and robust way to expand the context as variables
This commit is contained in:
parent
9984b75895
commit
850a4f2146
2 changed files with 20 additions and 33 deletions
|
@ -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,41 +110,22 @@ 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)
|
try:
|
||||||
context_argname = m.group(1)
|
context_value = eval(inner_expr)
|
||||||
path = m.group(2)
|
|
||||||
|
|
||||||
if context_argname in context:
|
if callable(context_value):
|
||||||
try:
|
context_value = context_value()
|
||||||
try:
|
if isinstance(context_value, datetime.date):
|
||||||
context_value = eval("context['{}']{}".format(
|
context_value = context_value.isoformat()
|
||||||
context_argname, path if path else ''))
|
except Exception as e:
|
||||||
except:
|
logging.exception(e)
|
||||||
context_value = eval(inner_expr)
|
context_value = expr
|
||||||
|
|
||||||
if callable(context_value):
|
parsed_value += prefix + (
|
||||||
context_value = context_value()
|
json.dumps(context_value)
|
||||||
if isinstance(context_value, datetime.date):
|
if isinstance(context_value, list)
|
||||||
context_value = context_value.isoformat()
|
or isinstance(context_value, dict)
|
||||||
except Exception as e:
|
else str(context_value))
|
||||||
logging.exception(e)
|
|
||||||
context_value = expr
|
|
||||||
|
|
||||||
parsed_value += prefix + (
|
|
||||||
json.dumps(context_value)
|
|
||||||
if isinstance(context_value, list)
|
|
||||||
or isinstance(context_value, dict)
|
|
||||||
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 = ''
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue