Skip to content

Commit b9c82a3

Browse files
authored
🚸 Raise warning if parser is used multiple times (#312)
1 parent 3ef7b4d commit b9c82a3

5 files changed

Lines changed: 129 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ nosetests.xml
3939

4040
# Vim.
4141
*.swp
42+
43+
# Vscode
44+
.vscode

bibtexparser/bparser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io
1010
import logging
11+
import warnings
1112

1213
from bibtexparser.bibdatabase import (BibDatabase, BibDataString, as_text,
1314
BibDataStringExpression, STANDARD_TYPES)
@@ -82,6 +83,12 @@ def __init__(self, data=None,
8283
:return: parser
8384
:rtype: `BibTexParser`
8485
"""
86+
87+
self._parse_call_count = 0
88+
#: Parsers can be called multiple times, merging databases, but raising a warning.
89+
# Set this to `True` to disable the warning. Default: `False`
90+
self.expect_multiple_parse = False
91+
8592
self.bib_database = BibDatabase()
8693

8794
#: Load common strings such as months abbreviation
@@ -142,6 +149,14 @@ def parse(self, bibtex_str, partial=False):
142149
:return: bibliographic database
143150
:rtype: BibDatabase
144151
"""
152+
153+
self._parse_call_count += 1
154+
if self._parse_call_count == 2 and not self.expect_multiple_parse:
155+
warnings.warn("The parser has been called more than once. "
156+
"Subsequent parse calls lead to a combined BibTeX library. \n"
157+
"To avoid the warning, set property `parser.expect_multiple_parse` to `True`.",
158+
category=UserWarning, stacklevel=2)
159+
145160
bibtex_file_obj = self._bibtex_file_obj(bibtex_str)
146161
try:
147162
self._expr.parseFile(bibtex_file_obj)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@article{FuMetalhalideperovskite2019,
2+
author = {Yongping Fu and Haiming Zhu and Jie Chen and Matthew P. Hautzinger and X.-Y. Zhu and Song Jin},
3+
doi = {10.1038/s41578-019-0080-9},
4+
journal = {Nature Reviews Materials},
5+
month = {feb},
6+
number = {3},
7+
pages = {169-188},
8+
publisher = {Springer Science and Business Media {LLC}},
9+
title = {Metal halide perovskite nanostructures for optoelectronic applications and the study of physical properties},
10+
url = {https://www.nature.com/articles/s41578-019-0080-9},
11+
volume = {4},
12+
year = {2019}
13+
}
14+
15+
@article{SunEnablingSiliconSolar2014,
16+
author = {Ke Sun and Shaohua Shen and Yongqi Liang and Paul E. Burrows and Samuel S. Mao and Deli Wang},
17+
doi = {10.1021/cr300459q},
18+
journal = {Chemical Reviews},
19+
month = {aug},
20+
number = {17},
21+
pages = {8662-8719},
22+
publisher = {American Chemical Society ({ACS})},
23+
title = {Enabling Silicon for Solar-Fuel Production},
24+
url = {http://pubs.acs.org/doi/10.1021/cr300459q},
25+
volume = {114},
26+
year = {2014}
27+
}
28+
29+
@article{LiuPhotocatalytichydrogenproduction2016,
30+
author = {Maochang Liu and Yubin Chen and Jinzhan Su and Jinwen Shi and Xixi Wang and Liejin Guo},
31+
doi = {10.1038/nenergy.2016.151},
32+
impactfactor = {54.000},
33+
journal = {Nature Energy},
34+
month = {sep},
35+
number = {11},
36+
pages = {16151},
37+
publisher = {Springer Science and Business Media {LLC}},
38+
title = {Photocatalytic hydrogen production using twinned nanocrystals and an unanchored {NiSx} co-catalyst},
39+
url = {http://www.nature.com/articles/nenergy2016151},
40+
volume = {1},
41+
year = {2016}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@article{FuMetalhalideperovskite2019,
2+
author = {Yongping Fu and Haiming Zhu and Jie Chen and Matthew P. Hautzinger and X.-Y. Zhu and Song Jin},
3+
doi = {10.1038/s41578-019-0080-9},
4+
journal = {Nature Reviews Materials},
5+
month = {feb},
6+
number = {3},
7+
pages = {169-188},
8+
publisher = {Springer Science and Business Media {LLC}},
9+
title = {Metal halide perovskite nanostructures for optoelectronic applications and the study of physical properties},
10+
url = {https://www.nature.com/articles/s41578-019-0080-9},
11+
volume = {4},
12+
year = {2019}
13+
}

bibtexparser/tests/test_bparser.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import os
77
import unittest
88
import codecs
9+
import warnings
910

11+
import bibtexparser
1012
from bibtexparser.bparser import BibTexParser
1113
from bibtexparser.bibdatabase import (COMMON_STRINGS, BibDataStringExpression)
1214
from bibtexparser.customization import *
@@ -650,6 +652,60 @@ def test_no_citekey_parsed_as_comment(self):
650652
self.assertEqual(bib.strings, {})
651653
self.assertEqual(bib.comments, ['@BOOK{, title = "bla"}'])
652654

655+
def test_parsing_just_once_not_raising_warnings_with_default_settings(self):
656+
parser = BibTexParser()
657+
658+
with open('bibtexparser/tests/data/article_comma_normal_single.bib',
659+
'r', encoding='utf-8') as bibfile:
660+
with warnings.catch_warnings(record=True) as warning:
661+
warnings.simplefilter("always")
662+
bibtexparser.load(bibfile, parser)
663+
assert len(warning) == 0
664+
665+
def test_parsing_twice_raise_warnings_with_default_settings(self):
666+
parser = BibTexParser()
667+
668+
with open('bibtexparser/tests/data/article_comma_normal_single.bib',
669+
'r', encoding='utf-8') as bibfile_first, \
670+
open('bibtexparser/tests/data/article_comma_normal_multiple.bib', 'r', encoding='utf-8') \
671+
as bibfile_second:
672+
with warnings.catch_warnings(record=True) as warning:
673+
warnings.simplefilter("always")
674+
bibtexparser.load(bibfile_first, parser)
675+
bibtexparser.load(bibfile_second, parser)
676+
assert len(warning) != 0
677+
678+
def test_parsing_three_times_raise_warnings_only_once_with_default_settings(self):
679+
parser = BibTexParser()
680+
681+
with open('bibtexparser/tests/data/article_comma_normal_single.bib',
682+
'r', encoding='utf-8') as bibfile_first, \
683+
open('bibtexparser/tests/data/article_comma_normal_multiple.bib', 'r', encoding='utf-8') \
684+
as bibfile_second, \
685+
open('bibtexparser/tests/data/article.bib', 'r', encoding='utf-8') as bibfile_third:
686+
with warnings.catch_warnings(record=True) as warning:
687+
warnings.simplefilter("always")
688+
bibtexparser.load(bibfile_first, parser)
689+
bibtexparser.load(bibfile_second, parser)
690+
bibtexparser.load(bibfile_third, parser)
691+
assert len(warning) == 1
692+
693+
def test_parsing_three_times_not_raising_a_warning_if_expect_multiple_parse_is_true(self):
694+
parser = BibTexParser()
695+
parser.expect_multiple_parse = True
696+
697+
with open('bibtexparser/tests/data/article_comma_normal_single.bib',
698+
'r', encoding='utf-8') as bibfile_first, \
699+
open('bibtexparser/tests/data/article_comma_normal_multiple.bib', 'r', encoding='utf-8') \
700+
as bibfile_second, \
701+
open('bibtexparser/tests/data/article.bib', 'r', encoding='utf-8') as bibfile_third:
702+
with warnings.catch_warnings(record=True) as warning:
703+
warnings.simplefilter("always")
704+
bibtexparser.load(bibfile_first, parser)
705+
bibtexparser.load(bibfile_second, parser)
706+
bibtexparser.load(bibfile_third, parser)
707+
assert len(warning) == 0
708+
653709

654710
if __name__ == '__main__':
655711
unittest.main()

0 commit comments

Comments
 (0)