diff --git a/backend.py b/backend.py index da2b466..e93b4e6 100644 --- a/backend.py +++ b/backend.py @@ -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 diff --git a/bmc.py b/bmc.py index 0ab2a05..a494b1c 100755 --- a/bmc.py +++ b/bmc.py @@ -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] ") diff --git a/params.py.example b/params.py.example index 21bbf57..2d283fe 100644 --- a/params.py.example +++ b/params.py.example @@ -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']