forked from platypush/platypush
Recursively expand the context of a request over the nested arguments
This commit is contained in:
parent
6b31ed3b1a
commit
dfee3f5004
2 changed files with 32 additions and 45 deletions
|
@ -77,32 +77,6 @@ class EventAction(Request):
|
|||
super().__init__(target=target, action=action, **args_copy)
|
||||
|
||||
|
||||
def execute(self, **context):
|
||||
event_args = context.pop('event').args if 'event' in context else {}
|
||||
|
||||
for (argname, value) in self.args.items():
|
||||
if isinstance(value, str):
|
||||
parsed_value = ''
|
||||
while value:
|
||||
m = re.match('([^\\\]*)\$([\w\d_-]+)(.*)', value)
|
||||
if m:
|
||||
context_argname = m.group(2)
|
||||
value = m.group(3)
|
||||
if context_argname in context:
|
||||
parsed_value += m.group(1) + context[context_argname]
|
||||
else:
|
||||
parsed_value += m.group(1) + '$' + m.group(2)
|
||||
else:
|
||||
parsed_value += value
|
||||
value = ''
|
||||
|
||||
value = parsed_value
|
||||
|
||||
self.args[argname] = value
|
||||
|
||||
super().execute()
|
||||
|
||||
|
||||
@classmethod
|
||||
def build(cls, action):
|
||||
action = super().parse(action)
|
||||
|
|
|
@ -67,30 +67,43 @@ class Request(Message):
|
|||
return proc.execute(*args, **kwargs)
|
||||
|
||||
|
||||
def _expand_context(self, **context):
|
||||
args = {}
|
||||
for (name, value) in self.args.items():
|
||||
def _expand_context(self, event_args=None, **context):
|
||||
if event_args is None: event_args = self.args
|
||||
|
||||
keys = []
|
||||
if isinstance(event_args, dict):
|
||||
keys = event_args.keys()
|
||||
elif isinstance(event_args, list):
|
||||
keys = range(0, len(event_args))
|
||||
|
||||
for key in keys:
|
||||
value = event_args[key]
|
||||
if isinstance(value, str):
|
||||
value = self._expand_value_from_context(value, **context)
|
||||
elif isinstance(value, dict) or isinstance(value, list):
|
||||
self._expand_context(event_args=value, **context)
|
||||
|
||||
event_args[key] = value
|
||||
|
||||
return event_args
|
||||
|
||||
|
||||
def _expand_value_from_context(self, value, **context):
|
||||
parsed_value = ''
|
||||
while value:
|
||||
m = re.match('([^\\\]*)\$([\w\d_-]+)(.*)', value)
|
||||
if m:
|
||||
context_name = m.group(2)
|
||||
context_argname = m.group(2)
|
||||
value = m.group(3)
|
||||
if context_name in context:
|
||||
parsed_value += m.group(1) + context[context_name]
|
||||
if context_argname in context:
|
||||
parsed_value += m.group(1) + context[context_argname]
|
||||
else:
|
||||
parsed_value += m.group(1) + '$' + m.group(2)
|
||||
else:
|
||||
parsed_value += value
|
||||
value = ''
|
||||
|
||||
value = parsed_value
|
||||
|
||||
args[name] = value
|
||||
|
||||
return args
|
||||
|
||||
return parsed_value
|
||||
|
||||
|
||||
def _send_response(self, response):
|
||||
|
|
Loading…
Reference in a new issue