def generate_files(self):
     for release in self.releases:
         try:
             override = {
                 "doi":
                 release["doi"],
                 "date-released":
                 datetime.strptime(release["datePublished"],
                                   "%Y-%m-%d").date(),
                 "version":
                 release["tag"]
             }
             remove = ["commit"]
             citation = Citation(url=release["url"],
                                 override=override,
                                 remove=remove)
             try:
                 release["files"] = dict({
                     "bibtex":
                     citation.as_bibtex(),
                     "codemeta":
                     citation.as_codemeta(),
                     "cff":
                     citation.cffstr,
                     "schema_dot_org":
                     citation.as_schema_dot_org(),
                     "endnote":
                     citation.as_enw(),
                     "ris":
                     citation.as_ris()
                 })
                 release["citability"] = "full"
             except Exception:
                 continue
         except Exception as e:
             if str(e).startswith("Error requesting file: "):
                 continue
             elif str(e).startswith(
                     "Provided CITATION.cff does not seem valid YAML."):
                 continue
             elif type(e).__name__ == "ScannerError":
                 continue
             else:
                 raise e
     return self
class CitationTest3(unittest.TestCase):

    def setUp(self):
        fixture = os.path.join("fixtures", "3", "CITATION.cff")
        with open(fixture, "r") as f:
            cffstr = f.read()
        self.citation = Citation(cffstr=cffstr, suspect_keys=[])

    def test_printing_as_bibtex(self):
        fixture = os.path.join("fixtures", "3", "bibtex.bib")
        with open(fixture, "r") as f:
            expected_bibtex = f.read()
        actual_bibtex = self.citation.as_bibtex()
        self.assertEqual(expected_bibtex, actual_bibtex)

    def test_printing_as_codemeta(self):
        fixture = os.path.join("fixtures", "3", "codemeta.json")
        with open(fixture, "r") as f:
            expected_codemeta = f.read()
        actual_codemeta = self.citation.as_codemeta()
        self.assertEqual(expected_codemeta, actual_codemeta)

    def test_printing_as_enw(self):
        fixture = os.path.join("fixtures", "3", "endnote.enw")
        with open(fixture, "r") as f:
            expected_endnote = f.read()
        actual_endnote = self.citation.as_enw()
        self.assertEqual(expected_endnote, actual_endnote)

    def test_printing_as_ris(self):
        fixture = os.path.join("fixtures", "3", "ris.txt")
        with open(fixture, "r") as f:
            expected_ris = f.read()
        actual_ris = self.citation.as_ris()
        self.assertEqual(expected_ris, actual_ris)

    def test_printing_as_zenodojson(self):
        fixture = os.path.join("fixtures", "3", "zenodo.json")
        with open(fixture, "r") as f:
            expected_zenodojson = f.read()
        actual_zenodojson = self.citation.as_zenodojson()
        self.assertEqual(expected_zenodojson, actual_zenodojson)
class CitationTestOverride(unittest.TestCase):

    def setUp(self):
        # with override
        override = {
            "doi": "thebestdoi.23678237",
            "date-released": datetime.datetime.strptime("2018-03-05", "%Y-%m-%d").date()
        }
        fixture = os.path.join("fixtures", "1", "CITATION.cff")
        with open(fixture, "r") as f:
            cffstr = f.read()
        self.citation = Citation(cffstr=cffstr, instantiate_empty=True, override=override)
        self.citation._parse_yaml()
        self.citation._override_suspect_keys()

    def test_printing_as_bibtex(self):
        fixture = os.path.join("fixtures", "5", "bibtex.bib")
        with open(fixture, "r") as f:
            expected_bibtex = f.read()
        actual_bibtex = self.citation.as_bibtex()
        self.assertEqual(expected_bibtex, actual_bibtex)

    def test_printing_as_codemeta(self):
        fixture = os.path.join("fixtures", "5", "codemeta.json")
        with open(fixture, "r") as f:
            expected_codemeta = f.read()
        actual_codemeta = self.citation.as_codemeta()
        self.assertEqual(expected_codemeta, actual_codemeta)

    def test_printing_as_enw(self):
        fixture = os.path.join("fixtures", "5", "endnote.enw")
        with open(fixture, "r") as f:
            expected_endnote = f.read()
        actual_endnote = self.citation.as_enw()
        self.assertEqual(expected_endnote, actual_endnote)

    def test_printing_as_ris(self):
        fixture = os.path.join("fixtures", "5", "ris.txt")
        with open(fixture, "r") as f:
            expected_ris = f.read()
        actual_ris = self.citation.as_ris()
        self.assertEqual(expected_ris, actual_ris)
class CitationTestRemoveNonExistentKey(unittest.TestCase):

    def setUp(self):
        # trying to remove a key that doesn't exist
        remove = ["hjjshbdjsu3933"]
        fixture = os.path.join("fixtures", "1", "CITATION.cff")
        with open(fixture, "r") as f:
            cffstr = f.read()
        self.citation = Citation(cffstr=cffstr, instantiate_empty=True, remove=remove)
        self.citation._parse_yaml()
        self.citation._remove_suspect_keys()

    def test_printing_as_bibtex(self):
        fixture = os.path.join("fixtures", "1", "bibtex.bib")
        with open(fixture, "r") as f:
            expected_bibtex = f.read()
        actual_bibtex = self.citation.as_bibtex()
        self.assertEqual(expected_bibtex, actual_bibtex)

    def test_printing_as_codemeta(self):
        fixture = os.path.join("fixtures", "1", "codemeta.json")
        with open(fixture, "r") as f:
            expected_codemeta = f.read()
        actual_codemeta = self.citation.as_codemeta()
        self.assertEqual(expected_codemeta, actual_codemeta)

    def test_printing_as_enw(self):
        fixture = os.path.join("fixtures", "1", "endnote.enw")
        with open(fixture, "r") as f:
            expected_endnote = f.read()
        actual_endnote = self.citation.as_enw()
        self.assertEqual(expected_endnote, actual_endnote)

    def test_printing_as_ris(self):
        fixture = os.path.join("fixtures", "1", "ris.txt")
        with open(fixture, "r") as f:
            expected_ris = f.read()
        actual_ris = self.citation.as_ris()
        self.assertEqual(expected_ris, actual_ris)
class CitationTestRemoveSuspectKeyVersion(unittest.TestCase):

    def setUp(self):
        # with removal of suspect key "version"
        remove = ["version"]
        fixture = os.path.join("fixtures", "6", "CITATION.cff")
        with open(fixture, "r") as f:
            cffstr = f.read()
        self.citation = Citation(cffstr=cffstr, instantiate_empty=True, remove=remove)
        self.citation._parse_yaml()
        self.citation._remove_suspect_keys()

    def test_printing_as_bibtex(self):
        fixture = os.path.join("fixtures", "6", "bibtex-no-version.bib")
        with open(fixture, "r") as f:
            expected_bibtex = f.read()
        actual_bibtex = self.citation.as_bibtex()
        self.assertEqual(expected_bibtex, actual_bibtex)

    def test_printing_as_codemeta(self):
        fixture = os.path.join("fixtures", "6", "codemeta-no-version.json")
        with open(fixture, "r") as f:
            expected_codemeta = f.read()
        actual_codemeta = self.citation.as_codemeta()
        self.assertEqual(expected_codemeta, actual_codemeta)

    def test_printing_as_enw(self):
        fixture = os.path.join("fixtures", "6", "endnote-no-version.enw")
        with open(fixture, "r") as f:
            expected_endnote = f.read()
        actual_endnote = self.citation.as_enw()
        self.assertEqual(expected_endnote, actual_endnote)

    def test_printing_as_ris(self):
        fixture = os.path.join("fixtures", "6", "ris-no-version.txt")
        with open(fixture, "r") as f:
            expected_ris = f.read()
        actual_ris = self.citation.as_ris()
        self.assertEqual(expected_ris, actual_ris)
class CitationTestIgnoreSuspectKeyVersion(unittest.TestCase):

    def setUp(self):
        # ignore suspect key "version"
        suspect_keys = ["version"]
        fixture = os.path.join("fixtures", "6", "CITATION.cff")
        with open(fixture, "r") as f:
            cffstr = f.read()
        self.citation = Citation(cffstr=cffstr, ignore_suspect_keys=True, suspect_keys=suspect_keys)

    def test_printing_as_bibtex(self):
        fixture = os.path.join("fixtures", "6", "bibtex-no-version.bib")
        with open(fixture, "r") as f:
            expected_bibtex = f.read()
        actual_bibtex = self.citation.as_bibtex()
        self.assertEqual(expected_bibtex, actual_bibtex)

    def test_printing_as_codemeta(self):
        fixture = os.path.join("fixtures", "6", "codemeta-no-version.json")
        with open(fixture, "r") as f:
            expected_codemeta = f.read()
        actual_codemeta = self.citation.as_codemeta()
        self.assertEqual(expected_codemeta, actual_codemeta)

    def test_printing_as_enw(self):
        fixture = os.path.join("fixtures", "6", "endnote-no-version.enw")
        with open(fixture, "r") as f:
            expected_endnote = f.read()
        actual_endnote = self.citation.as_enw()
        self.assertEqual(expected_endnote, actual_endnote)

    def test_printing_as_ris(self):
        fixture = os.path.join("fixtures", "6", "ris-no-version.txt")
        with open(fixture, "r") as f:
            expected_ris = f.read()
        actual_ris = self.citation.as_ris()
        self.assertEqual(expected_ris, actual_ris)
Ejemplo n.º 7
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')
Ejemplo n.º 8
0
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)