From dfee3f50048467b31c1fc57e38b3a5d463e43e2e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 6 Jan 2018 12:40:22 +0100 Subject: [PATCH] Recursively expand the context of a request over the nested arguments --- platypush/event/hook.py | 26 -------------- platypush/message/request/__init__.py | 51 +++++++++++++++++---------- 2 files changed, 32 insertions(+), 45 deletions(-) diff --git a/platypush/event/hook.py b/platypush/event/hook.py index ca2bb6807..605c384ae 100644 --- a/platypush/event/hook.py +++ b/platypush/event/hook.py @@ -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) diff --git a/platypush/message/request/__init__.py b/platypush/message/request/__init__.py index 3e711b065..d3854b781 100644 --- a/platypush/message/request/__init__.py +++ b/platypush/message/request/__init__.py @@ -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): - parsed_value = '' - while value: - m = re.match('([^\\\]*)\$([\w\d_-]+)(.*)', value) - if m: - context_name = m.group(2) - value = m.group(3) - if context_name in context: - parsed_value += m.group(1) + context[context_name] - else: - parsed_value += m.group(1) + '$' + m.group(2) - else: - parsed_value += value - value = '' + value = self._expand_value_from_context(value, **context) + elif isinstance(value, dict) or isinstance(value, list): + self._expand_context(event_args=value, **context) - value = parsed_value + event_args[key] = value - args[name] = value + return event_args - return args + def _expand_value_from_context(self, value, **context): + 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 = '' + + return parsed_value def _send_response(self, response):