Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def write(od, file_handle, delimeter_type, tab_indent):
# Write header to the output file.
segments = [
"%comment{{This file was created with betterbib v{}.}}\n".format(__version__)
]
brace_delimeters = delimeter_type == "braces"
# Add segments for each bibtex entry in order
segments.extend(
[
pybtex_to_bibtex_string(
d, bib_id, brace_delimeters=brace_delimeters, tab_indent=tab_indent,
)
for bib_id, d in od.items()
]
)
# Write all segments to the file at once
file_handle.write("\n\n".join(segments) + "\n")
def get_by_doi(self, doi):
headers = {
"User-Agent": "betterbib/{} ({}; mailto:{})".format(
__version__, __website__, __author_email__
)
}
# https://api.crossref.org/works/10.1137/110820713
r = requests.get(self.api_url + "/" + doi, headers=headers)
assert r.ok
data = r.json()
result = data["message"]
return self._crossref_to_pybtex(result)
# Get the containing book, and see if it has authors or editors. If
# authors, consider it a monograph and use inbook; otherwise
# incollection.
# To get the book, make use of the fact that the chapter DOIs are
# the book DOI plus some appendix, e.g., .ch3, _3, etc. In other
# words: Strip the last digits plus whatever is nondigit before it.
a = re.match("(.*?)([^0-9]+[0-9]+)$", entry["DOI"])
if a is None:
# The DOI doesn't have that structure; assume 'incollection'.
return "incollection"
book_doi = a.group(1)
headers = {
"User-Agent": "betterbib/{} ({}; mailto:{})".format(
__version__, __website__, __author_email__
)
}
# Try to get the book data
r = requests.get(self.api_url + "/" + book_doi, headers=headers)
if r.ok:
book_data = r.json()
if "author" in book_data["message"]:
return "inbook"
# else
return "incollection"
# All other cases
_crossref_to_bibtex_type = {
"book": "book",
"dataset": "misc",