Updating version to version 0.2
This commit is contained in:
parent
0e2066c180
commit
89b9c1ed44
@ -8,4 +8,4 @@ with scientific papers.
|
|||||||
# Global list of valid paper identifier types. See README.md.
|
# Global list of valid paper identifier types. See README.md.
|
||||||
__valid_identifiers__ = []
|
__valid_identifiers__ = []
|
||||||
|
|
||||||
__version__ = "0.1.3.1"
|
__version__ = "0.2"
|
||||||
|
114
libbmc/bibtex.py
114
libbmc/bibtex.py
@ -3,17 +3,18 @@ This file contains functions to deal with Bibtex files and edit them.
|
|||||||
|
|
||||||
TODO: Unittests
|
TODO: Unittests
|
||||||
"""
|
"""
|
||||||
import bibtexparser
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import bibtexparser
|
||||||
|
|
||||||
from libbmc import tools
|
from libbmc import tools
|
||||||
|
|
||||||
|
|
||||||
default_papers_filename_mask = "{first}_{last}-{journal}-{year}{arxiv_version}"
|
DEFAULT_PAPERS_FILENAME_MASK = "{first}_{last}-{journal}-{year}{arxiv_version}"
|
||||||
default_books_filename_mask = "{authors} - {title}"
|
DEFAULT_BOOKS_FILENAME_MASK = "{authors} - {title}"
|
||||||
|
|
||||||
|
|
||||||
def dict2BibTeX(data):
|
def dict2bibtex(data):
|
||||||
"""
|
"""
|
||||||
Convert a single BibTeX entry dict to a BibTeX string.
|
Convert a single BibTeX entry dict to a BibTeX string.
|
||||||
|
|
||||||
@ -25,11 +26,11 @@ def dict2BibTeX(data):
|
|||||||
|
|
||||||
for field in [i for i in sorted(data) if i not in ['ENTRYTYPE', 'ID']]:
|
for field in [i for i in sorted(data) if i not in ['ENTRYTYPE', 'ID']]:
|
||||||
bibtex += "\t" + field + "={" + data[field] + "},\n"
|
bibtex += "\t" + field + "={" + data[field] + "},\n"
|
||||||
bibtex += "}\n"
|
bibtex += "}\n\n"
|
||||||
return bibtex
|
return bibtex
|
||||||
|
|
||||||
|
|
||||||
def BibDatabase2BibTeX(data):
|
def bibdatabase2bibtex(data):
|
||||||
"""
|
"""
|
||||||
Convert a BibDatabase object to a BibTeX string.
|
Convert a BibDatabase object to a BibTeX string.
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ def write(filename, data):
|
|||||||
:param data: A ``bibtexparser.BibDatabase`` object.
|
:param data: A ``bibtexparser.BibDatabase`` object.
|
||||||
"""
|
"""
|
||||||
with open(filename, 'w') as fh:
|
with open(filename, 'w') as fh:
|
||||||
fh.write(BibDatabase2BibTeX(data))
|
fh.write(bibdatabase2bibtex(data))
|
||||||
|
|
||||||
|
|
||||||
def append(filename, data):
|
def append(filename, data):
|
||||||
@ -58,7 +59,7 @@ def append(filename, data):
|
|||||||
:param data: A ``bibtexparser.BibDatabase`` object.
|
:param data: A ``bibtexparser.BibDatabase`` object.
|
||||||
"""
|
"""
|
||||||
with open(filename, 'a') as fh:
|
with open(filename, 'a') as fh:
|
||||||
fh.write(BibDatabase2BibTeX(data))
|
fh.write(bibdatabase2bibtex(data))
|
||||||
|
|
||||||
|
|
||||||
def edit(filename, identifier, data):
|
def edit(filename, identifier, data):
|
||||||
@ -75,8 +76,8 @@ def edit(filename, identifier, data):
|
|||||||
bibtex = bibtexparser.load(fh)
|
bibtex = bibtexparser.load(fh)
|
||||||
|
|
||||||
# Update it
|
# Update it
|
||||||
for k in data:
|
# TODO: Not working
|
||||||
bibtex.entries_dict[identifier][k] = data[k]
|
bibtex.entries_dict[identifier] = data.entries[0]
|
||||||
|
|
||||||
# Write the resulting BibTeX
|
# Write the resulting BibTeX
|
||||||
write(filename, bibtex)
|
write(filename, bibtex)
|
||||||
@ -96,6 +97,7 @@ def replace(filename, identifier, data):
|
|||||||
bibtex = bibtexparser.load(fh)
|
bibtex = bibtexparser.load(fh)
|
||||||
|
|
||||||
# Use entries_dict representation to update easily
|
# Use entries_dict representation to update easily
|
||||||
|
# TODO: Not working
|
||||||
bibtex.entries_dict[identifier] = data.entries[0]
|
bibtex.entries_dict[identifier] = data.entries[0]
|
||||||
|
|
||||||
# Write the resulting BibTeX
|
# Write the resulting BibTeX
|
||||||
@ -114,8 +116,9 @@ def delete(filename, identifier):
|
|||||||
bibtex = bibtexparser.load(fh)
|
bibtex = bibtexparser.load(fh)
|
||||||
|
|
||||||
# Delete the bibtex entry
|
# Delete the bibtex entry
|
||||||
|
# TODO: Not working
|
||||||
try:
|
try:
|
||||||
del(bibtex.entries_dict[identifier])
|
del bibtex.entries_dict[identifier]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -123,7 +126,7 @@ def delete(filename, identifier):
|
|||||||
write(filename, bibtex)
|
write(filename, bibtex)
|
||||||
|
|
||||||
|
|
||||||
def get(filename, ignore_fields=[]):
|
def get(filename, ignore_fields=None):
|
||||||
"""
|
"""
|
||||||
Get all entries from a BibTeX file.
|
Get all entries from a BibTeX file.
|
||||||
|
|
||||||
@ -134,19 +137,23 @@ def get(filename, ignore_fields=[]):
|
|||||||
:returns: A ``bibtexparser.BibDatabase`` object representing the fetched \
|
:returns: A ``bibtexparser.BibDatabase`` object representing the fetched \
|
||||||
entries.
|
entries.
|
||||||
"""
|
"""
|
||||||
|
# Handle default argument
|
||||||
|
if ignore_fields is None:
|
||||||
|
ignore_fields = []
|
||||||
|
|
||||||
# Open bibtex file
|
# Open bibtex file
|
||||||
with open(filename, 'r') as fh:
|
with open(filename, 'r') as fh:
|
||||||
bibtex = bibtexparser.load(fh)
|
bibtex = bibtexparser.load(fh)
|
||||||
|
|
||||||
# Clean the entry dict if necessary
|
# Clean the entries if necessary
|
||||||
bibtex.entries_dict = [{k: entry[k]
|
bibtex.entries = [{k: entry[k]
|
||||||
for k in entry if k not in ignore_fields}
|
for k in entry if k not in ignore_fields}
|
||||||
for entry in bibtex.entries_dict]
|
for entry in bibtex.entries]
|
||||||
|
|
||||||
return bibtex
|
return bibtex
|
||||||
|
|
||||||
|
|
||||||
def get_entry_by_filter(filename, filter, ignore_fields=[]):
|
def get_entry_by_filter(filename, filter_function, ignore_fields=None):
|
||||||
"""
|
"""
|
||||||
Get an entry from a BibTeX file.
|
Get an entry from a BibTeX file.
|
||||||
|
|
||||||
@ -155,14 +162,18 @@ def get_entry_by_filter(filename, filter, ignore_fields=[]):
|
|||||||
Returns the first matching entry.
|
Returns the first matching entry.
|
||||||
|
|
||||||
:param filename: The name of the BibTeX file.
|
:param filename: The name of the BibTeX file.
|
||||||
:param filter: A function returning ``True`` or ``False`` whether the \
|
:param filter_function: A function returning ``True`` or ``False`` \
|
||||||
entry should be included or not.
|
whether the entry should be included or not.
|
||||||
: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.BibDatabase`` object representing the \
|
:returns: A ``bibtexparser.BibDatabase`` object representing the \
|
||||||
first matching entry. ``None`` if entry was not found.
|
first matching entry. ``None`` if entry was not found.
|
||||||
"""
|
"""
|
||||||
|
# Handle default argument
|
||||||
|
if ignore_fields is None:
|
||||||
|
ignore_fields = []
|
||||||
|
|
||||||
# Open bibtex file
|
# Open bibtex file
|
||||||
with open(filename, 'r') as fh:
|
with open(filename, 'r') as fh:
|
||||||
bibtex = bibtexparser.load(fh)
|
bibtex = bibtexparser.load(fh)
|
||||||
@ -170,13 +181,16 @@ def get_entry_by_filter(filename, filter, ignore_fields=[]):
|
|||||||
matching_entry = None
|
matching_entry = None
|
||||||
try:
|
try:
|
||||||
# Try to fetch the matching entry dict
|
# Try to fetch the matching entry dict
|
||||||
for entry in bibtex.entries.items:
|
for entry in bibtex.entries:
|
||||||
if filter(entry):
|
if filter_function(entry):
|
||||||
matching_entry = entry
|
matching_entry = entry
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# If none found, return None
|
# If none found, return None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if matching_entry is None:
|
||||||
|
return None
|
||||||
|
|
||||||
# Clean the entry dict if necessary
|
# Clean the entry dict if necessary
|
||||||
matching_entry = {k: matching_entry[k]
|
matching_entry = {k: matching_entry[k]
|
||||||
for k in matching_entry if k not in ignore_fields}
|
for k in matching_entry if k not in ignore_fields}
|
||||||
@ -186,7 +200,7 @@ def get_entry_by_filter(filename, filter, ignore_fields=[]):
|
|||||||
return bib_db
|
return bib_db
|
||||||
|
|
||||||
|
|
||||||
def get_entry(filename, identifier, ignore_fields=[]):
|
def get_entry(filename, identifier, ignore_fields=None):
|
||||||
"""
|
"""
|
||||||
Get an entry from a BibTeX file.
|
Get an entry from a BibTeX file.
|
||||||
|
|
||||||
@ -198,18 +212,24 @@ def get_entry(filename, identifier, ignore_fields=[]):
|
|||||||
:returns: A ``bibtexparser.BibDatabase`` object representing the \
|
:returns: A ``bibtexparser.BibDatabase`` object representing the \
|
||||||
fetched entry. ``None`` if entry was not found.
|
fetched entry. ``None`` if entry was not found.
|
||||||
"""
|
"""
|
||||||
|
# Handle default argument
|
||||||
|
if ignore_fields is None:
|
||||||
|
ignore_fields = []
|
||||||
|
|
||||||
return get_entry_by_filter(filename,
|
return get_entry_by_filter(filename,
|
||||||
lambda x: x["ID"] == identifier,
|
lambda x: x["ID"] == identifier,
|
||||||
ignore_fields)
|
ignore_fields)
|
||||||
|
|
||||||
|
|
||||||
def to_filename(bibtex, mask=default_papers_filename_mask):
|
def to_filename(data,
|
||||||
|
mask=DEFAULT_PAPERS_FILENAME_MASK,
|
||||||
|
extra_formatters=None):
|
||||||
"""
|
"""
|
||||||
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 ::
|
.. note ::
|
||||||
|
|
||||||
Available masks out of the box are:
|
Available formatters out of the box are:
|
||||||
- ``journal``
|
- ``journal``
|
||||||
- ``title``
|
- ``title``
|
||||||
- ``year``
|
- ``year``
|
||||||
@ -220,28 +240,46 @@ def to_filename(bibtex, mask=default_papers_filename_mask):
|
|||||||
|
|
||||||
Filename is slugified after applying the masks.
|
Filename is slugified after applying the masks.
|
||||||
|
|
||||||
:param bibtex: A ``bibtexparser.BibDatabase`` object representing a \
|
:param data: A ``bibtexparser.BibDatabase`` object representing a \
|
||||||
BibTeX entry, as the one from ``bibtexparser`` output.
|
BibTeX entry, as the one from ``bibtexparser`` output.
|
||||||
:param mask: A Python format string.
|
:param mask: A Python format string.
|
||||||
|
:param extra_formatters: A dict of format string (in the mask) and \
|
||||||
|
associated lambdas to perform the formatting.
|
||||||
|
|
||||||
:returns: A formatted filename.
|
:returns: A formatted filename.
|
||||||
"""
|
"""
|
||||||
bibtex = bibtex.entries[0]
|
# Handle default argument
|
||||||
authors = re.split(' and ', bibtex['author'])
|
if extra_formatters is None:
|
||||||
|
extra_formatters = {}
|
||||||
|
|
||||||
filename = mask
|
entry = data.entries[0]
|
||||||
filename = filename.format(journal=bibtex.get("journal", default=""))
|
authors = re.split(' and ', entry['author'])
|
||||||
filename = filename.format(title=bibtex.get("title", default=""))
|
|
||||||
filename = filename.format(year=bibtex.get("year", default=""))
|
|
||||||
|
|
||||||
filename = filename.format(first=authors[0].split(',')[0].strip())
|
formatters = {
|
||||||
filename = filename.format(last=authors[-1].split(',')[0].strip())
|
"journal": "",
|
||||||
filename = filename.format(authors=", ".join(
|
"title": "",
|
||||||
[i.split(',')[0].strip() for i in authors]))
|
"year": "",
|
||||||
|
"first": "",
|
||||||
|
"last": "",
|
||||||
|
"authors": "",
|
||||||
|
"arxiv_version": ""
|
||||||
|
}
|
||||||
|
|
||||||
|
formatters["journal"] = entry.get("journal", "")
|
||||||
|
formatters["title"] = entry.get("title", "")
|
||||||
|
formatters["year"] = entry.get("year", "")
|
||||||
|
|
||||||
|
formatters["first"] = authors[0].split(',')[0].strip()
|
||||||
|
formatters["last"] = authors[-1].split(',')[0].strip()
|
||||||
|
formatters["authors"] = ", ".join([i.split(',')[0].strip()
|
||||||
|
for i in authors])
|
||||||
|
|
||||||
|
for extra_formatter in extra_formatters:
|
||||||
|
formatters[extra_formatter] = extra_formatters[extra_formatter](entry)
|
||||||
|
|
||||||
arxiv_version = ""
|
arxiv_version = ""
|
||||||
if "eprint" in bibtex:
|
if "eprint" in entry:
|
||||||
arxiv_version = '-' + bibtex['eprint'][bibtex['eprint'].rfind('v'):]
|
arxiv_version = '-' + entry['eprint'][entry['eprint'].rfind('v'):]
|
||||||
filename = filename.format(arxiv_version=arxiv_version)
|
formatters["arxiv_version"] = arxiv_version
|
||||||
|
|
||||||
return tools.slugify(filename)
|
return tools.slugify(mask.format(**formatters))
|
||||||
|
Loading…
Reference in New Issue
Block a user