From 49821eab51659e1e241880dcbcc8d5ce8632633b Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Thu, 21 Feb 2013 10:01:20 +0100 Subject: [PATCH] Improves parser selection. --- papers/files.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/papers/files.py b/papers/files.py index d943a95..779539a 100644 --- a/papers/files.py +++ b/papers/files.py @@ -27,6 +27,11 @@ except ImportError: _papersdir = None BIB_EXTENSIONS = ['.bib', '.bibyaml', '.bibml', '.yaml'] +FORMATS = {'bib': pybtex.database.input.bibtex, + 'xml': pybtex.database.input.bibtexml, + 'yml': pybtex.database.input.bibyaml, + 'yaml': pybtex.database.input.bibyaml, + 'bibyaml': pybtex.database.input.bibyaml} def clean_path(path): @@ -129,35 +134,34 @@ def load_externalbibfile(fullbibpath): check_file(fullbibpath) filename, ext = os.path.splitext(os.path.split(fullbibpath)[1]) - if ext == '.bib': - parser = pybtex.database.input.bibtex.Parser() - bib_data = parser.parse_file(fullbibpath) - elif ext == '.xml' or ext == '.bibtexml': - parser = pybtex.database.input.bibtexml.Parser() - bib_data = parser.parse_file(fullbibpath) - elif ext == '.yaml' or ext == '.bibyaml': - parser = pybtex.database.input.bibyaml.Parser() - bib_data = parser.parse_file(fullbibpath) + if ext[1:] in FORMATS.keys(): + with open(fullbibpath) as f: + return parse_bibdata(f, ext[1:]) else: print(colored('error', 'error') + ': {} not recognized format for bibliography'.format( colored(ext, 'cyan'))) exit(-1) - return bib_data + +def parse_bibdata(content, format_): + """Parse bib data from string. + + :content: stream + :param format_: (bib|xml|yml) + """ + parser = FORMATS[format_].Parser() + return parser.parse_stream(content) def editor_input(editor, initial=""): """Use an editor to get input""" - with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file: tfile_name = temp_file.name temp_file.write(initial) temp_file.flush() subprocess.call([editor, tfile_name]) - with open(tfile_name) as temp_file: content = temp_file.read() os.remove(tfile_name) - return content