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 setUp(self): # trying to ignore a key that doesn't exist suspect_keys = ["hjjshbdjsu3933"] fixture = os.path.join("fixtures", "1", "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 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 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 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()
class CitationTest4(unittest.TestCase): def setUp(self): fixture = os.path.join("fixtures", "4", "CITATION.cff") with open(fixture, "r") as f: cffstr = f.read() self.citation = Citation(cffstr=cffstr, instantiate_empty=True) def test_conversion_to_yaml(self): with self.assertRaises(ValueError) as context: self.citation._parse_yaml() self.assertTrue('Provided CITATION.cff does not seem valid YAML.' in str(context.exception))
def test_on_github_but_over_http(self): url = "http://github.com" with self.assertRaises(Exception) as context: self.citation = Citation(url) self.assertTrue( "Only 'https://github.com' URLs are supported at the moment." == str(context.exception))
def test_on_github_but_no_repo_specified(self): url = "https://github.com/citation-file-format" with self.assertRaises(Exception) as context: self.citation = Citation(url) self.assertTrue('Error extracting (user|organization) and/or repository ' +\ 'information from the provided URL' in str(context.exception))
def test_google(self): url = "www.google.com" with self.assertRaises(Exception) as context: self.citation = Citation(url=url) self.assertTrue( "Only 'https://github.com' URLs are supported at the moment." == str(context.exception))
def test_on_github_with_url_without_tree_element(self): url = "https://github.com/xenon-middleware/xenon/sha/134624562456" with self.assertRaises(ValueError) as context: self.citation = Citation(url) self.assertTrue( str(context.exception).startswith( "Expected 'https://github.com/" + "<org>/<repo>/tree/...' but instead found"))
def test_retrieval_via_tag(self): # https://github.com/<org>/<repo>/tree/<tagname> url = "https://github.com/citation-file-format/cff-converter-python/tree/0.0.1" citation = Citation(url=url) fixture = os.path.join("fixtures", "1", "CITATION.cff") with open(fixture) as f: expected_cffstr = f.read() actual_cffstr = citation.cffstr self.assertEqual(expected_cffstr, actual_cffstr)
def test_retrieval_of_latest_master(self): # https://github.com/<org>/<repo> url = "https://github.com/citation-file-format/cff-converter-python" citation = Citation(url=url) fixture = os.path.join("fixtures", "7", "CITATION.cff") with open(fixture) as f: expected_cffstr = f.read() actual_cffstr = citation.cffstr self.assertEqual(expected_cffstr, actual_cffstr)
def test_retrieval_via_sha(self): # https://github.com/<org>/<repo>/tree/<sha> url = "https://github.com/citation-file-format/cff-converter-python/tree/" + \ "b7505591cf421ab33156ec0bffb0af43fd7d2cd1" citation = Citation(url=url) fixture = os.path.join("fixtures", "1", "CITATION.cff") with open(fixture) as f: expected_cffstr = f.read() actual_cffstr = citation.cffstr self.assertEqual(expected_cffstr, actual_cffstr)
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)
def get_bibtex_from_citation_file(package_name): """ Fetch BibTeX information directly from the package if available in either of the files CITATION.cff or CITATION (if installed). Parameters ---------- package_name : str Name of the package. Returns ------- bibtex : str or None Returns the BibTeX string or None if the package is not installed or doesn't provide one. """ package = importlib.import_module(package_name) cff_file = os.path.join(package.__path__[0], 'CITATION.cff') citation_file = os.path.join(package.__path__[0], 'CITATION') if os.path.exists(cff_file): with open(cff_file) as f: cffstr = f.read() citation = Citation(cffstr=cffstr) bibtex = citation.as_bibtex() elif os.path.exists(citation_file): with open(citation_file) as f: citestr = f.read() match = re.search(cite_tag_pattr, citestr) if match is None: raise ValueError( 'Could not find any BibTeX entries in CITATION file') for i_end in range(match.end(), len(citestr)): bibtex = citestr[match.start():i_end] if bibtex.count('{') == bibtex.count('}'): break else: raise ValueError( 'Mismatched braces in BibTeX entry in CITATION file') else: bibtex = None return bibtex
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)
class CitationTestIgnoreNonExistentKey(unittest.TestCase): def setUp(self): # trying to ignore a key that doesn't exist suspect_keys = ["hjjshbdjsu3933"] fixture = os.path.join("fixtures", "1", "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", "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 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)
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 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)
def test1(cffstr): citation = Citation(cffstr=cffstr, suspect_keys=[], validate=True, raise_exception=True) assert citation.yaml is not None
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 test_retrieval_of_latest_master(self): # this test checks if cffconvert can behave similar to a simple curl or wget # https://github.com/<org>/<repo> url = "https://github.com/citation-file-format/cff-converter-python" citation = Citation(url=url) self.assertIsNotNone(citation)
def test_retrieval_via_branchname(self): # https://github.com/<org>/<repo>/tree/<branchname> # this test checks if cffconvert can behave similar to a simple curl or wget url = "https://github.com/citation-file-format/cff-converter-python/tree/master" citation = Citation(url=url) self.assertIsNotNone(citation)
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_neither_url_nor_cffstr(self): with self.assertRaises(ValueError): self.citation = Citation()
def test_both_url_and_cffstr(self): with self.assertRaises(ValueError) as context: self.citation = Citation(url="some url", cffstr="some cffstr") assert "You should specify either \'url\' or \'cffstr\'." in str(context.exception)
def test_on_github_but_repo_doesnt_exist(self): url = "https://github.com/org_1122334455/repo_0123456789" with self.assertRaises(Exception) as context: self.citation = Citation(url) self.assertTrue('Error requesting file: ' in str(context.exception))
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)
def setUp(self): fixture = os.path.join("fixtures", "4", "CITATION.cff") with open(fixture, "r") as f: cffstr = f.read() self.citation = Citation(cffstr=cffstr, instantiate_empty=True)