Remove useless TODOs

This commit is contained in:
Lucas Verney 2016-01-30 17:03:12 +01:00
parent 6c5b69a23d
commit 9853469f3c
6 changed files with 58 additions and 35 deletions

View File

@ -1,7 +1,7 @@
""" """
This file contains functions to deal with Bibtex files and edit them. This file contains functions to deal with Bibtex files and edit them.
TODO: Unittests TODO: Unittests + use bibtexparser writer
""" """
import bibtexparser import bibtexparser
import re import re
@ -111,7 +111,10 @@ def delete(filename, identifier):
bibtex = bibtex.entries_dict bibtex = bibtex.entries_dict
# Delete the bibtex entry # Delete the bibtex entry
del(bibtex[identifier]) try:
del(bibtex[identifier])
except KeyError:
pass
# Write the resulting BibTeX # Write the resulting BibTeX
write(filename, bibtex) write(filename, bibtex)
@ -140,6 +143,41 @@ def get(filename, ignore_fields=[]):
return bibtex return bibtex
def get_entry_by_filter(filename, filter, ignore_fields=[]):
"""
Get an entry from a BibTeX file.
:param filename: The name of the BibTeX file.
:param filter: A function returning ``True`` or ``False`` whether the \
entry should be included or not.
:param ignore_fields: An optional list of fields to strip from the BibTeX \
file.
:returns: A ``bibtexparser.BibDatabase`` object representing the \
first matching entry. ``None`` if entry was not found.
"""
# Open bibtex file
with open(filename, 'r', encoding="utf-8") as fh:
bibtex = bibtexparser.load(fh)
bibtex = bibtex.entries
matching_entry = None
try:
# Try to fetch the matching entry dict
for entry in bibtex.items:
if filter(entry):
matching_entry = entry
except KeyError:
# If none found, return None
return None
# Clean the entry dict if necessary
matching_entry = {k: matching_entry[k]
for k in matching_entry if k not in ignore_fields}
return matching_entry
def get_entry(filename, identifier, ignore_fields=[]): def get_entry(filename, identifier, ignore_fields=[]):
""" """
Get an entry from a BibTeX file. Get an entry from a BibTeX file.
@ -149,31 +187,31 @@ def get_entry(filename, identifier, ignore_fields=[]):
:param ignore_fields: An optional list of fields to strip from the BibTeX \ :param ignore_fields: An optional list of fields to strip from the BibTeX \
file. file.
:returns: A ``bibtexparser`` dict representing the fetched entry. \ :returns: A ``bibtexparser.BibDatabase`` object representing the \
``None`` if entry was not found. fetched entry. ``None`` if entry was not found.
""" """
# Open bibtex file return get_entry_by_filter(filename,
with open(filename, 'r', encoding="utf-8") as fh: lambda x: x["ID"] == identifier,
bibtex = bibtexparser.load(fh) ignore_fields)
bibtex = bibtex.entries_dict
try:
# Try to fetch the matching entry dict
entry = bibtex[identifier]
except KeyError:
# If none found, return None
return None
# Clean the entry dict if necessary
entry = {k: entry[k] for k in entry if k not in ignore_fields}
return entry
def to_filename(bibtex, mask=default_papers_filename_mask): def to_filename(bibtex, mask=default_papers_filename_mask):
""" """
Convert a bibtex entry to a formatted filename according to a given mask. Convert a bibtex entry to a formatted filename according to a given mask.
.. note ::
Available masks out of the box are:
- ``journal``
- ``title``
- ``year``
- ``first`` for the first author
- ``last`` for the last author
- ``authors`` for the list of authors
- ``arxiv_version`` (discarded if no arXiv version in the BibTeX)
Filename is slugified after applying the masks.
:param bibtex: A dict representing a BibTeX entry, as the one from \ :param bibtex: A dict representing a BibTeX entry, as the one from \
``bibtexparser`` output. ``bibtexparser`` output.
:param mask: A Python format string. :param mask: A Python format string.

View File

@ -1,8 +1,6 @@
""" """
This files contains all the functions to extract DOIs of citations from .bbl This files contains all the functions to extract DOIs of citations from .bbl
files. files.
# TODO: Unittests
""" """
import os import os
import re import re

View File

@ -1,8 +1,6 @@
""" """
This files contains all the functions to extract DOIs of citations from This files contains all the functions to extract DOIs of citations from
BibTeX files. BibTeX files.
# TODO: Unittests
""" """
import bibtexparser import bibtexparser
import os import os

View File

@ -1,8 +1,6 @@
""" """
This files contains all the functions to extract DOIs of citations from This files contains all the functions to extract DOIs of citations from
PDF files. PDF files.
# TODO: Unittests
""" """
import os import os
import requests import requests

View File

@ -1,8 +1,6 @@
""" """
This files contains all the functions to extract DOIs of citations from This files contains all the functions to extract DOIs of citations from
plaintext files. plaintext files.
# TODO: Unittests
""" """
import os import os
import requests import requests

View File

@ -443,8 +443,6 @@ def get_sources(arxiv_id):
canonical form. canonical form.
:returns: A ``TarFile`` object of the sources of the arXiv preprint or \ :returns: A ``TarFile`` object of the sources of the arXiv preprint or \
``None``. ``None``.
# TODO: Unittests
""" """
try: try:
r = requests.get(ARXIV_EPRINT_URL.format(arxiv_id=arxiv_id)) r = requests.get(ARXIV_EPRINT_URL.format(arxiv_id=arxiv_id))
@ -468,8 +466,6 @@ def get_bbl(arxiv_id):
a canonical form. a canonical form.
:returns: A list of the full text of the ``.bbl`` files (if any) \ :returns: A list of the full text of the ``.bbl`` files (if any) \
or ``None``. or ``None``.
# TODO: Unittests
""" """
tf = get_sources(arxiv_id) tf = get_sources(arxiv_id)
bbl_files = [i for i in tf.getmembers() if i.name.endswith(".bbl")] bbl_files = [i for i in tf.getmembers() if i.name.endswith(".bbl")]
@ -490,9 +486,6 @@ def get_citations(arxiv_id):
:param arxiv_id: The arXiv id (e.g. ``1401.2910`` or ``1401.2910v1``) in \ :param arxiv_id: The arXiv id (e.g. ``1401.2910`` or ``1401.2910v1``) in \
a canonical form. a canonical form.
:returns: A dict of cleaned plaintext citations and their associated DOI. :returns: A dict of cleaned plaintext citations and their associated DOI.
>>> get_citations("1401.2910")
# TODO: Unittests
""" """
dois = {} dois = {}
# Get the list of bbl files for this preprint # Get the list of bbl files for this preprint