@@ -35,8 +35,14 @@ def __init__(self):
3535 # Dict mapping citations keys to BibTex entries
3636 self ._all_citations : dict [str , str ] = dict ()
3737
38- self .read_citations ()
39- self ._reset ()
38+ # store citation error
39+ self ._citation_err_msg = None
40+
41+ try :
42+ self .read_citations ()
43+ self ._reset ()
44+ except Exception as e : # pragma: no cover
45+ self ._citation_err_msg = e
4046
4147 def _reset (self ):
4248 """Reset citations to default only (only for testing purposes)"""
@@ -91,27 +97,27 @@ def register(self, key):
9197 - The citation key for an entry in `pybamm/CITATIONS.txt` or
9298 - One or more BibTex formatted citations
9399 """
94-
95- # Check if citation is a known key
96- if key in self ._all_citations :
97- self ._papers_to_cite .add (key )
98- return
99-
100- # Try to parse the citation using pybtex
101- try :
102- # Parse string as a bibtex citation, and check that a citation was found
103- bib_data = parse_string (key , bib_format = "bibtex" )
104- if not bib_data .entries :
105- raise PybtexError ("no entries found" )
106-
107- # Add and register all citations
108- for key , entry in bib_data .entries .items ():
109- self ._add_citation (key , entry )
110- self .register (key )
100+ if self ._citation_err_msg is None :
101+ # Check if citation is a known key
102+ if key in self ._all_citations :
103+ self ._papers_to_cite .add (key )
111104 return
112- except PybtexError :
113- # Unable to parse / unknown key
114- raise KeyError (f"Not a bibtex citation or known citation: { key } " )
105+
106+ # Try to parse the citation using pybtex
107+ try :
108+ # Parse string as a bibtex citation, and check that a citation was found
109+ bib_data = parse_string (key , bib_format = "bibtex" )
110+ if not bib_data .entries :
111+ raise PybtexError ("no entries found" )
112+
113+ # Add and register all citations
114+ for key , entry in bib_data .entries .items ():
115+ self ._add_citation (key , entry )
116+ self .register (key )
117+ return
118+ except PybtexError :
119+ # Unable to parse / unknown key
120+ raise KeyError (f"Not a bibtex citation or known citation: { key } " )
115121
116122 def print (self , filename = None , output_format = "text" ):
117123 """Print all citations that were used for running simulations.
@@ -143,7 +149,17 @@ def print(self, filename=None, output_format="text"):
143149
144150def print_citations (filename = None , output_format = "text" ):
145151 """See :meth:`Citations.print`"""
146- pybamm .citations .print (filename , output_format )
152+ if citations ._citation_err_msg is not None :
153+ raise ImportError (
154+ f"Citations could not be registered. If you are on Google Colab - "
155+ "pybtex does not work with Google Colab due to a known bug - "
156+ "https://bitbucket.org/pybtex-devs/pybtex/issues/148/. "
157+ "Please manually cite all the references."
158+ "\n Error encountered -\n "
159+ f"{ citations ._citation_err_msg } "
160+ )
161+ else :
162+ pybamm .citations .print (filename , output_format )
147163
148164
149165citations = Citations ()
0 commit comments