Export bibtex function

This commit is contained in:
Phyks 2014-05-27 12:17:59 +02:00
parent 479aea4101
commit 2509a69694
3 changed files with 34 additions and 4 deletions

View File

@ -19,7 +19,7 @@ from bibtexparser.bparser import BibTexParser
from codecs import open
def getNewName(src, bibtex, tag=''):
def getNewName(src, bibtex, tag='', override_format=None):
"""
Return the formatted name according to params for the given
bibtex entry
@ -27,13 +27,19 @@ def getNewName(src, bibtex, tag=''):
authors = re.split(' and ', bibtex['author'])
if bibtex['type'] == 'article':
new_name = params.format_articles
if override_format is None:
new_name = params.format_articles
else:
new_name = override_format
try:
new_name = new_name.replace("%j", bibtex['journal'])
except:
pass
elif bibtex['type'] == 'book':
new_name = params.format_books
if override_format is None:
new_name = params.format_books
else:
new_name = override_format
new_name = new_name.replace("%t", bibtex['title'])
try:
@ -222,11 +228,12 @@ def diffFilesIndex():
return index.get_entry_dict()
def getBibtex(entry, file_id='both'):
def getBibtex(entry, file_id='both', clean=False):
"""Returns the bibtex entry corresponding to entry, as a dict
entry is either a filename or a bibtex ident
file_id is file or id or both to search for a file / id / both
clean is to clean the ignored fields specified in params
"""
try:
with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
@ -248,6 +255,12 @@ def getBibtex(entry, file_id='both'):
if os.path.samefile(bibtex[key]['file'], entry):
bibtex_entry = bibtex[key]
break
if clean:
for field in params.ignore_fields:
try:
del(bibtex_entry[field])
except KeyError:
pass
return bibtex_entry

13
bmc.py
View File

@ -473,6 +473,11 @@ if __name__ == '__main__':
help="an identifier")
parser_open.set_defaults(func='open')
parser_export = subparsers.add_parser('export', help="export help")
parser_export.add_argument('ids', metavar='id', nargs='+',
help="an identifier")
parser_export.set_defaults(func='export')
parser_resync = subparsers.add_parser('resync', help="resync help")
parser_resync.set_defaults(func='resync')
@ -556,6 +561,14 @@ if __name__ == '__main__':
sys.exit("Unable to open file associated " +
"to ident "+filename)
sys.exit()
elif args.func == 'export':
bibtex = ''
for id in args.ids:
bibtex += tools.parsed2Bibtex(backend.getBibtex(id,
clean=True))
print(bibtex.strip())
sys.exit
elif args.func == 'resync':
confirm = tools.rawInput("Resync files and bibtex index? [y/N] ")

View File

@ -1,4 +1,5 @@
# -*- coding: utf8 -*-
# The folder in which the papers should be stored
# /!\ Keep the trailing slash /!\
folder = "/home/phyks/Papers/"
@ -24,3 +25,6 @@ format_books = "%a-%t"
# List of lambda functions to apply
# E.g. : format_custom = [lambda x: x.replace('test', 'some_expr')]
format_custom = []
# Fields to ignore when exporting Bibtex entries
ignore_fields = ['file', 'doi', 'tag']