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

13
bmc.py
View File

@ -473,6 +473,11 @@ if __name__ == '__main__':
help="an identifier") help="an identifier")
parser_open.set_defaults(func='open') 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 = subparsers.add_parser('resync', help="resync help")
parser_resync.set_defaults(func='resync') parser_resync.set_defaults(func='resync')
@ -557,6 +562,14 @@ if __name__ == '__main__':
"to ident "+filename) "to ident "+filename)
sys.exit() 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': elif args.func == 'resync':
confirm = tools.rawInput("Resync files and bibtex index? [y/N] ") confirm = tools.rawInput("Resync files and bibtex index? [y/N] ")
if confirm.lower() == 'y': if confirm.lower() == 'y':

View File

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