support for non-standard bibtex types, fix #210 #218

main
Fabien C. Y. Benureau 5 years ago
parent a30e75a5e6
commit 7bd475378c
No known key found for this signature in database
GPG Key ID: C3FB5E831A249A9A

@ -119,18 +119,18 @@ class EnDecoder(object):
keyword for keyword in entry['keyword']) keyword for keyword in entry['keyword'])
return entry return entry
def decode_bibdata(self, bibdata): def decode_bibdata(self, bibstr):
"""Decodes bibdata from string. """Decodes bibdata from string.
If the decoding fails, returns a BibDecodingError. If the decoding fails, returns a BibDecodingError.
""" """
if len(bibdata) == 0: if len(bibstr) == 0:
error_msg = 'parsing error: the provided string has length zero.' error_msg = 'parsing error: the provided string has length zero.'
raise self.BibDecodingError(error_msg, bibdata) raise self.BibDecodingError(error_msg, bibstr)
try: try:
entries = bp.bparser.BibTexParser( entries = bp.bparser.BibTexParser(
bibdata, common_strings=True, customization=customizations, bibstr, common_strings=True, customization=customizations,
homogenize_fields=True).get_entry_dict() homogenize_fields=True, ignore_nonstandard_types=False).get_entry_dict()
# Remove id from bibtexparser attribute which is stored as citekey # Remove id from bibtexparser attribute which is stored as citekey
for e in entries: for e in entries:
entries[e].pop(BP_ID_KEY) entries[e].pop(BP_ID_KEY)
@ -147,13 +147,13 @@ class EnDecoder(object):
return entries return entries
else: else:
raise self.BibDecodingError(('no valid entry found in the provided data: ' raise self.BibDecodingError(('no valid entry found in the provided data: '
' {}').format(bibdata), bibdata) ' {}').format(bibstr), bibstr)
except (pyparsing.ParseException, pyparsing.ParseSyntaxException) as e: except (pyparsing.ParseException, pyparsing.ParseSyntaxException) as e:
error_msg = self._format_parsing_error(e) error_msg = self._format_parsing_error(e)
raise self.BibDecodingError(error_msg, bibdata) raise self.BibDecodingError(error_msg, bibstr)
except bibtexparser.bibdatabase.UndefinedString as e: except bibtexparser.bibdatabase.UndefinedString as e:
error_msg = 'parsing error: undefined string in provided data: {}'.format(e) error_msg = 'parsing error: undefined string in provided data: {}'.format(e)
raise self.BibDecodingError(error_msg, bibdata) raise self.BibDecodingError(error_msg, bibstr)
@classmethod @classmethod
def _format_parsing_error(cls, e): def _format_parsing_error(cls, e):

@ -0,0 +1,9 @@
@collection{Geometric_phases,
title = {Geometric phases in physics},
editor = {Shapere, Alfred and Wilczek, Frank},
year = {1989},
series = {Advanced Series in Mathematical Physics},
volume = {5},
publisher = {World Scientific},
isbn = {9789971506216}
}

@ -0,0 +1,7 @@
@software{hadoop,
author = {{Apache Software Foundation}},
title = {Hadoop},
url = {https://hadoop.apache.org},
version = {0.20.2},
date = {2010-02-19},
}

@ -43,8 +43,8 @@ def capture(f, verbose=False):
""" """
def newf(*args, **kwargs): def newf(*args, **kwargs):
old_stderr, old_stdout = sys.stderr, sys.stdout old_stderr, old_stdout = sys.stderr, sys.stdout
sys.stdout = _fake_stdio(additional_out=old_stderr if verbose else None) sys.stdout = _fake_stdio(additional_out=old_stdout if verbose else None)
sys.stderr = _fake_stdio(additional_out=old_stderr if False else None) sys.stderr = _fake_stdio(additional_out=old_stderr if verbose else None)
try: try:
return f(*args, **kwargs), _get_fake_stdio_ucontent(sys.stdout), _get_fake_stdio_ucontent(sys.stderr) return f(*args, **kwargs), _get_fake_stdio_ucontent(sys.stdout), _get_fake_stdio_ucontent(sys.stderr)
finally: finally:

@ -1091,6 +1091,22 @@ class TestUsecase(DataCommandTestCase):
actual = self.execute_cmds(cmds, capture_output=True) actual = self.execute_cmds(cmds, capture_output=True)
self.assertEqual(correct, actual) self.assertEqual(correct, actual)
def test_add_non_standard(self):
"""Test that non-standard bibtex are correctly added"""
self.fs.add_real_directory(os.path.join(self.rootpath, 'data_non_standard'), read_only=False)
correct = ['Initializing pubs in /pubs\n',
'added to pubs:\n[Geometric_phases] "Geometric phases in physics" (1989) \n',
'added to pubs:\n[hadoop] Foundation, Apache Software "Hadoop" \n',
]
cmds = ['pubs init -p /pubs',
'pubs add data_non_standard/non_standard_collection.bib',
'pubs add data_non_standard/non_standard_software.bib',
# 'pubs list',
]
actual = self.execute_cmds(cmds, capture_output=True)
self.assertEqual(correct, actual)
@mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get) @mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
def test_readme(self, reqget): def test_readme(self, reqget):
"""Test that the readme example work.""" """Test that the readme example work."""
@ -1099,7 +1115,7 @@ class TestUsecase(DataCommandTestCase):
self.fs.add_real_file(os.path.join(self.rootpath, 'data/pagerank.pdf'), target_path='data/Knuth1995.pdf') self.fs.add_real_file(os.path.join(self.rootpath, 'data/pagerank.pdf'), target_path='data/Knuth1995.pdf')
cmds = ['pubs init', cmds = ['pubs init',
'pubs import data/collection.bib', 'pubs import data/three_articles.bib',
'pubs add data/pagerank.bib -d data/pagerank.pdf', 'pubs add data/pagerank.bib -d data/pagerank.pdf',
#'pubs add -D 10.1007/s00422-012-0514-6 -d data/pagerank.pdf', #'pubs add -D 10.1007/s00422-012-0514-6 -d data/pagerank.pdf',
'pubs add -I 978-0822324669 -d data/oyama2000the.pdf', 'pubs add -I 978-0822324669 -d data/oyama2000the.pdf',

Loading…
Cancel
Save