Ejemplo n.º 1
0
    def render(self, in_ctx):
        action_specs = []

        if not self.has_items():
            action_spec = {
                "action": expr_base.evaluate(self.action, in_ctx),
                "input": expr_base.evaluate(getattr(self, "input", {}),
                                            in_ctx),
            }

            action_specs.append(action_spec)
        else:
            items_spec = self.get_items_spec()

            if " in " not in items_spec.items:
                items_expr = items_spec.items.strip()
            else:
                start_idx = items_spec.items.index(" in ") + 4
                items_expr = items_spec.items[start_idx:].strip()

            items = expr_base.evaluate(items_expr, in_ctx)

            if not isinstance(items, list):
                raise TypeError('The value of "%s" is not type of list.' %
                                items_expr)

            item_keys = (
                None if " in " not in items_spec.items else
                items_spec.items[:items_spec.items.index(" in ")].replace(
                    " ", "").split(","))

            for idx, item in enumerate(items):
                if item_keys and (isinstance(item, tuple)
                                  or isinstance(item, list)):
                    item = dict(zip(item_keys, list(item)))
                elif item_keys and len(item_keys) == 1:
                    item = {item_keys[0]: item}

                item_ctx_value = ctx_util.set_current_item(in_ctx, item)

                action_spec = {
                    "action":
                    expr_base.evaluate(self.action, item_ctx_value),
                    "input":
                    expr_base.evaluate(getattr(self, "input", {}),
                                       item_ctx_value),
                    "item_id":
                    idx,
                }

                action_specs.append(action_spec)

        return self, action_specs
Ejemplo n.º 2
0
    def render(self, in_ctx):
        action_specs = []

        if not self.has_items():
            action_spec = {
                'action': expr_base.evaluate(self.action, in_ctx),
                'input': expr_base.evaluate(getattr(self, 'input', {}), in_ctx)
            }

            action_specs.append(action_spec)
        else:
            items_spec = self.get_items_spec()

            items_expr = (items_spec.items.strip()
                          if ' in ' not in items_spec.items else
                          items_spec.items[items_spec.items.index(' in ') +
                                           4:].strip())

            items = expr_base.evaluate(items_expr, in_ctx)

            if not isinstance(items, list):
                raise TypeError('The value of "%s" is not type of list.' %
                                items_expr)

            item_keys = (
                None if ' in ' not in items_spec.items else
                items_spec.items[:items_spec.items.index(' in ')].replace(
                    ' ', '').split(','))

            for idx, item in enumerate(items):
                if item_keys and (isinstance(item, tuple)
                                  or isinstance(item, list)):
                    item = dict(zip(item_keys, list(item)))
                elif item_keys and len(item_keys) == 1:
                    item = {item_keys[0]: item}

                item_ctx_value = ctx_util.set_current_item(in_ctx, item)

                action_spec = {
                    'action':
                    expr_base.evaluate(self.action, item_ctx_value),
                    'input':
                    expr_base.evaluate(getattr(self, 'input', {}),
                                       item_ctx_value),
                    'item_id':
                    idx
                }

                action_specs.append(action_spec)

        return self, action_specs