Exemplo n.º 1
0
        def inner_decorator(ctx, model_stdin, model_editor, model, *args,
                            **kwargs):
            if not model:
                if model_stdin:
                    stream = click.get_text_stream("stdin")
                    model = "".join(stream)

                elif model_editor:
                    fetch_function = getattr(ctx.obj["client"],
                                             fetch_method_name)
                    model = fetch_function(kwargs[item_id])
                    if "data" in model:
                        updated_data = utils.json_dumps(model["data"])
                        updated_data = click.edit(updated_data)
                        if not updated_data:
                            return
                        updated_data = utils.json_loads(updated_data)
                        model = fetch_function(kwargs[item_id])
                        model["data"] = updated_data
                        model = utils.json_dumps(model)
                    else:
                        model = utils.json_dumps(model)
                        model = click.edit(model)

                if (model_stdin or model_editor) and not model:
                    return

            if model and parse_json and not isinstance(model, dict):
                if isinstance(model, bytes):
                    model = model.decode("utf-8")
                model = utils.json_loads(model)

            kwargs["model"] = model

            return func(*args, **kwargs)
Exemplo n.º 2
0
    def convert(self, value, param, ctx):
        if not value:
            return None

        try:
            return utils.json_loads(
                super(JSONParamType, self).convert(value, param, ctx))
        except Exception as exc:
            self.fail("{0} is not valid JSON string.".format(value))