Exemple #1
0
def cffconvert(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """

    cffstr = None
    outputformat = None
    url = None
    validate = False
    ignore_suspect_keys = False
    verbose = False
    version = False

    outstr = ''

    if request.args:

        if 'cffstr' in request.args:
            cffstr = request.args.get('cffstr')
        if 'outputformat' in request.args:
            outputformat = request.args.get('outputformat')
        if 'url' in request.args:
            url = request.args.get('url')
        if 'validate' in request.args:
            validate = True
        if 'ignore_suspect_keys' in request.args:
            ignore_suspect_keys = True
        if 'verbose' in request.args:
            verbose = True
        if 'version' in request.args:
            version = True
    else:
        return Response(get_help_text(), mimetype='text/html')

    if version is True:
        outstr += "{0}\n".format(cffconvert_version.__version__)
        return Response(outstr, mimetype='text/plain')

    if verbose is True:
        outstr += "{0} = {1}\n".format("cffstr", cffstr)
        outstr += "{0} = {1}\n".format("outputformat", outputformat)
        outstr += "{0} = {1}\n".format("url", url)
        outstr += "{0} = {1}\n".format("validate", validate)
        outstr += "{0} = {1}\n".format("ignore_suspect_keys",
                                       ignore_suspect_keys)
        outstr += "{0} = {1}\n".format("verbose", verbose)
        outstr += "{0} = {1}\n".format("version", version)

    if url is not None and cffstr is not None:
        outstr += "\n\n{0}\n".format("Error: can't have both url and cffstr.")
        return Response(outstr, mimetype='text/plain')

    remove = None
    override = None
    suspect_keys = None

    try:
        citation = Citation(url=url,
                            cffstr=cffstr,
                            ignore_suspect_keys=ignore_suspect_keys,
                            override=override,
                            remove=remove,
                            suspect_keys=suspect_keys,
                            validate=validate,
                            raise_exception=True)
    except Exception as e:
        if str(e) == "Provided CITATION.cff does not seem valid YAML.":
            outstr += "\n\nError: Provided 'cffstr' does not seem valid YAML."
        else:
            outstr += "\n\nError: " + str(e)
        return Response(outstr, mimetype='text/plain')

    acceptable_output_formats = [
        "bibtex", "cff", "codemeta", "endnote", "schema.org", "ris", "zenodo"
    ]
    if validate:
        # when validating, there's no need to convert to anything yet
        pass
    elif outputformat not in acceptable_output_formats:
        outstr += "\n\n'outputformat' should be one of [{0}]".format(
            ", ".join(acceptable_output_formats))
        return Response(outstr, mimetype='text/plain')

    if outputformat is None:
        return
    elif outputformat == "bibtex":
        outstr += citation.as_bibtex()
    elif outputformat == "cff":
        outstr += citation.cffstr
    elif outputformat == "codemeta":
        outstr += citation.as_codemeta()
    elif outputformat == "endnote":
        outstr += citation.as_enw()
    elif outputformat == "ris":
        outstr += citation.as_ris()
    elif outputformat == "schema.org":
        outstr += citation.as_schema_dot_org()
    elif outputformat == "zenodo":
        outstr += citation.as_zenodojson()

    return Response(outstr, mimetype='text/plain')
def cli(infile, outfile, outputformat, url, validate, ignore_suspect_keys, verbose, version):

    if version is True:
        print("{0}".format(cffconvert_version.__version__))
        return

    if verbose is True:
        click.echo("{0} = {1}".format("infile", infile))
        click.echo("{0} = {1}".format("outfile", outfile))
        click.echo("{0} = {1}".format("outputformat", outputformat))
        click.echo("{0} = {1}".format("url", url))
        click.echo("{0} = {1}".format("validate", validate))
        click.echo("{0} = {1}".format("ignore_suspect_keys", ignore_suspect_keys))
        click.echo("{0} = {1}".format("verbose", verbose))
        click.echo("{0} = {1}".format("version", version))

    if infile is None and url is None:
        infile = "CITATION.cff"
    elif infile is not None and url is not None:
        raise ValueError("You need to specify either \'--infile\' or \'url\' but not both.")

    if infile is None:
        cffstr = None
    elif infile == '-':
        cffstr = sys.stdin.read()
    else:
        with open(infile, "r") as f:
            cffstr = f.read()

    # TODO currently there is no way to provide values for these 3 arguments from the command line
    remove = None
    override = None
    suspect_keys = None

    citation = Citation(url=url, cffstr=cffstr, ignore_suspect_keys=ignore_suspect_keys, override=override,
                        remove=remove, suspect_keys=suspect_keys, validate=validate)

    acceptable_output_formats = ["bibtex", "cff", "codemeta", "endnote", "ris", "schema.org", "zenodo"]
    if validate:
        # when validating, there's no need to convert to anything yet
        pass
    elif outputformat not in acceptable_output_formats:
        raise ValueError("'--outputformat' should be one of [{0}]".format(", ".join(acceptable_output_formats)))

    if outputformat is None:
        return
    elif outputformat == "bibtex":
        outstr = citation.as_bibtex()
    elif outputformat == "cff":
        outstr = citation.as_cff()
    elif outputformat == "codemeta":
        outstr = citation.as_codemeta()
    elif outputformat == "endnote":
        outstr = citation.as_enw()
    elif outputformat == "ris":
        outstr = citation.as_ris()
    elif outputformat == "schema.org":
        outstr = citation.as_schema_dot_org()
    elif outputformat == "zenodo":
        outstr = citation.as_zenodojson()

    if outfile is None:
        print(outstr, end='')
    else:
        with open(outfile, "w") as f:
            f.write(outstr)