Skip to content

Commit

Permalink
Merge pull request #24 from citation-file-format/jspaaks-codemeta-export
Browse files Browse the repository at this point in the history
Jspaaks codemeta export
  • Loading branch information
Tommos0 authored Mar 26, 2018
2 parents 8b67f6c + 50766e1 commit 4390ea3
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ ENV/

# mypy
.mypy_cache/

# IDE
.idea

201 changes: 160 additions & 41 deletions cff_converter_python/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _get_baseurl(self):
if self.url[0:18] == "https://github.com":
self.baseurl = "https://raw.githubusercontent.com"
else:
raise Exception("Only GitHub is supported at the moment.")
raise Exception("Only 'https://github.com' URLs are supported at the moment.")

def _retrieve_file(self):

Expand All @@ -29,22 +29,30 @@ def _retrieve_file(self):
"(?P<user>[^/\n]*)/" +
"(?P<repo>[^/\n]*)" +
"(/(?P<branch>[^/\n]*))?", re.IGNORECASE)
matched = re.match(regexp, self.url).groupdict()

matched = re.match(regexp, self.url)
if matched is None:
raise Exception("Error extracting (user|organization) and/or repository " +\
"information from the provided URL ({0}).".format(self.url))
else:
url_parts = matched.groupdict()

self.file_url = "/".join([self.baseurl,
matched["user"],
matched["repo"],
matched["branch"] if matched["branch"] is not None else "master",
url_parts["user"],
url_parts["repo"],
url_parts["branch"] if url_parts["branch"] is not None else "master",
"CITATION.cff"])

r = requests.get(self.file_url)
if r.ok:
self.file_contents = r.text
else:
raise Warning("status not '200 OK'")
raise Exception("Error requesting file: {0}".format(self.file_url))

def _parse_yaml(self):
self.as_yaml = yaml.safe_load(self.file_contents)
if not isinstance(self.as_yaml, dict):
raise Exception("Provided CITATION.cff does not seem valid YAML.")

def as_bibtex(self):

Expand All @@ -61,26 +69,30 @@ def get_author_string():
if "name-suffix" in author:
authors.append(author["name-suffix"])
arr.append(" " * 12 + " ".join(authors))
return " and\n".join(arr) + "\n"
return " and\n".join(arr)

width = 8
s = ""
s += "@misc{"
s += "YourReferenceHere,\n"
s += "author".ljust(width, " ") + " = {\n"
s += get_author_string()
s += " " * 11 + "},\n"
s += "title".ljust(width, " ") + " = {"
s += self.as_yaml["title"] + "},\n"
s += "month".ljust(width, " ") + " = {"
s += str(self.as_yaml["date-released"].month) + "},\n"
s += "year".ljust(width, " ") + " = {"
s += str(self.as_yaml["date-released"].year) + "},\n"
s += "doi".ljust(width, " ") + " = {"
s += self.as_yaml["doi"] + "},\n"
s += "url".ljust(width, " ") + " = {"
s += self.as_yaml["repository-code"] + "}\n"
s += "}\n"
s += "YourReferenceHere"
if "authors" in self.as_yaml:
s += ",\nauthor = {\n"
s += get_author_string()
s += "\n }"
if "title" in self.as_yaml:
s += ",\ntitle = {"
s += self.as_yaml["title"] + "}"
if "date-released" in self.as_yaml:
s += ",\nmonth = {"
s += str(self.as_yaml["date-released"].month) + "}"
s += ",\nyear = {"
s += str(self.as_yaml["date-released"].year) + "}"
if "doi" in self.as_yaml:
s += ",\ndoi = {"
s += self.as_yaml["doi"] + "}"
if "repository-code" in self.as_yaml:
s += ",\nurl = {"
s += self.as_yaml["repository-code"] + "}"
s += "\n}\n"

return s

Expand All @@ -94,7 +106,7 @@ def construct_author_string():
if "family-names" in author:
name += author["family-names"]
if "name-suffix" in author:
name += author["name-suffix"]
name += " " + author["name-suffix"]
if "given-names" in author:
name += ", " + author["given-names"]
name += "\n"
Expand All @@ -115,16 +127,43 @@ def construct_date_string():

s = ""
s += "TY - COMP\n"
s += construct_author_string()
s += "DO - " + self.as_yaml["doi"] + "\n"
s += construct_keywords_string()

if "authors" in self.as_yaml:
s += construct_author_string()
else:
s += "AU -\n"

if "doi" in self.as_yaml:
s += "DO - " + self.as_yaml["doi"] + "\n"
else:
s += "DO -\n"

if "keywords" in self.as_yaml:
s += construct_keywords_string()
else:
s += "KW -\n"

s += "M3 - software\n"
s += "PB - GitHub Inc.\n"
s += "PP - San Francisco, USA\n"
s += construct_date_string()
s += "T1 - " + self.as_yaml["title"] + "\n"
s += "UR - " + self.as_yaml["repository-code"] + "\n"

if "date-released" in self.as_yaml:
s += construct_date_string()
else:
s += "PY -\n"

if "title" in self.as_yaml:
s += "T1 - " + self.as_yaml["title"] + "\n"
else:
s += "T1 -\n"

if "repository-code" in self.as_yaml:
s += "UR - " + self.as_yaml["repository-code"] + "\n"
else:
s += "UR -\n"

s += "ER -\n"

return s

def as_enw(self):
Expand All @@ -138,24 +177,34 @@ def construct_author_string():
if "family-names" in author:
name += author["family-names"]
if "name-suffix" in author:
name += author["name-suffix"]
name += " " + author["name-suffix"]
if "given-names" in author:
name += ", " + author["given-names"]
names.append(name)
return " & ".join(names)

def construct_keywords_string():
if "keywords" in self.as_yaml:
return ", ".join(["\"" + keyword + "\"" for keyword in self.as_yaml["keywords"]])
else:
return ""
return ", ".join(["\"" + keyword + "\"" for keyword in self.as_yaml["keywords"]])

s = ""
s += "%0\n"
s += "%0 Generic\n"
s += "%A " + construct_author_string() + "\n"
s += "%D " + str(self.as_yaml["date-released"].year) + "\n"
s += "%T " + self.as_yaml["title"] + "\n"

if "authors" in self.as_yaml:
s += "%A " + construct_author_string() + "\n"
else:
s += "%A\n"

if "date-released" in self.as_yaml:
s += "%D " + str(self.as_yaml["date-released"].year) + "\n"
else:
s += "%D\n"

if "title" in self.as_yaml:
s += "%T " + self.as_yaml["title"] + "\n"
else:
s += "%T\n"

s += "%E\n"
s += "%B\n"
s += "%C\n"
Expand All @@ -168,7 +217,11 @@ def construct_keywords_string():
s += "%Y\n"
s += "%S\n"
s += "%7\n"
s += "%8 " + str(self.as_yaml["date-released"].month) + "\n"
if "date-released" in self.as_yaml:
s += "%8 " + str(self.as_yaml["date-released"].month) + "\n"
else:
s += "%8\n"

s += "%9\n"
s += "%?\n"
s += "%!\n"
Expand All @@ -187,9 +240,75 @@ def construct_keywords_string():
s += "%#\n"
s += "%$\n"
s += "%F YourReferenceHere\n"
s += "%K " + construct_keywords_string() + "\n"
if "keywords" in self.as_yaml:
s += "%K " + construct_keywords_string() + "\n"
else:
s += "%K\n"

s += "%X\n"
s += "%Z\n"
s += "%U " + self.as_yaml["repository-code"] + "\n"
if "repository-code" in self.as_yaml:
s += "%U " + self.as_yaml["repository-code"] + "\n"
else:
s += "%U\n"

return s

def as_codemeta(self):

def resolve_spdx_license(spdx_license_code):
licenses_url = "https://raw.githubusercontent.com/spdx/license-list-data" + \
"/b541ee8a345aa93b70a08765c7bf5e423bb4d558/json/licenses.json"
r = requests.get(licenses_url)
if r.ok:
data = r.json()
return [license["seeAlso"][0] for license in data["licenses"] if license["licenseId"] == spdx_license_code][0]
else:
raise Warning("status not '200 OK'")

def convert(author):

family_names = list()
for name_part in ["name-particle", "family-names", "name-suffix"]:
if name_part in author.keys() and author[name_part] is not "":
family_names.append(author[name_part])

author_str = ''
author_str += '{\n'
author_str += ' "@type": "Person"'
if "given-names" in author:
author_str += ',\n "givenName": "{0}"'.format(author["given-names"])
author_str += ',\n "familyName": "{0}"'.format(" ".join(family_names))
if "affiliation" in author:
author_str += ',\n'
author_str += ' "affiliation": {\n'
author_str += ' "@type": "Organization",\n'
author_str += ' "legalName": "{0}"\n'.format(author["affiliation"])
author_str += ' }'
author_str += '\n }'
return author_str

s = ''
s += '{\n'
s += ' "@context": "http://schema.org",\n'
s += ' "@type": "SoftwareSourceCode"'
if "repository-code" in self.as_yaml:
s += ',\n "codeRepository": "{0}"'.format(self.as_yaml["repository-code"])
if "date-released" in self.as_yaml:
s += ',\n "datePublished": "{0}"'.format(self.as_yaml["date-released"])
if "authors" in self.as_yaml:
s += ',\n "author": [{0}]'.format(", ".join([convert(author) for author in self.as_yaml["authors"]]))
if "keywords" in self.as_yaml:
s += ',\n "keywords": [{0}]'.format(", ".join(['"{0}"'.format(kw) for kw in self.as_yaml["keywords"]]))
if "license" in self.as_yaml:
s += ',\n "license": "{0}"'.format(resolve_spdx_license(self.as_yaml["license"]))
if "version" in self.as_yaml:
s += ',\n "version": "{0}"'.format(self.as_yaml["version"])
if "doi" in self.as_yaml:
s += ',\n "identifier": "https://doi.org/{0}"'.format(self.as_yaml["doi"])
if "title" in self.as_yaml:
s += ',\n "name": "{0}"'.format(self.as_yaml["title"])
s += '\n}\n'

return s

11 changes: 11 additions & 0 deletions fixtures/bibtex-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@misc{YourReferenceHere,
author = {
Jurriaan H. Spaaks and
Tom Klaver
},
title = {cff-converter-python},
month = {1},
year = {2018},
doi = {10.5281/zenodo.1162057},
url = {https://github.com/citation-file-format/cff-converter-python}
}
6 changes: 6 additions & 0 deletions fixtures/bibtex-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@misc{YourReferenceHere,
author = {
Gonzalo Fernández de Córdoba Jr.
},
title = {example title}
}
10 changes: 10 additions & 0 deletions fixtures/bibtex-3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@misc{YourReferenceHere,
author = {
Dafne van Kuppevelt and
Christiaan Meijer and
Vincent van Hees and
Mateusz Kuzak
},
title = {mcfly},
url = {https://github.com/NLeSC/mcfly}
}
11 changes: 0 additions & 11 deletions fixtures/cff-converter-python-bibtex-1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# YAML 1.2
---
authors:
-
---
authors:
-
affiliation: "Netherlands eScience Center"
family-names: Spaaks
given-names: Jurriaan H.
-
given-names: "Jurriaan H."
-
affiliation: "Netherlands eScience Center"
family-names: Klaver
given-names: Tom
cff-version: "1.0.3"
date-released: 2018-01-16
doi: 10.5281/zenodo.1162057
keywords:
- "citation"
- "bibliography"
- "cff"
- "CITATION.cff"
keywords:
- citation
- bibliography
- cff
- CITATION.cff
license: Apache-2.0
message: "If you use this software, please cite it using these metadata."
repository-code: "https://github.com/citation-file-format/cff-converter-python"
Expand Down
11 changes: 11 additions & 0 deletions fixtures/citationcff-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# YAML 1.2
---
authors:
-
family-names: "Fernández de Córdoba"
given-names: Gonzalo
name-suffix: Jr.
cff-version: "1.0.3"
message: "Example data from the CITATION.cff spec 1.0.3"
title: "example title"
version: "1.0.0"
Loading

0 comments on commit 4390ea3

Please sign in to comment.