|
5 | 5 | #
|
6 | 6 | import pybamm
|
7 | 7 | import os
|
8 |
| -import sys |
9 | 8 | import warnings
|
10 | 9 | import pybtex
|
11 | 10 | from pybtex.database import parse_file, parse_string, Entry
|
@@ -36,9 +35,14 @@ def __init__(self):
|
36 | 35 | # Dict mapping citations keys to BibTex entries
|
37 | 36 | self._all_citations: dict[str, str] = dict()
|
38 | 37 |
|
39 |
| - if "google.colab" not in sys.modules: |
| 38 | + # store citation error |
| 39 | + self._citation_err_msg = None |
| 40 | + |
| 41 | + try: |
40 | 42 | self.read_citations()
|
41 | 43 | self._reset()
|
| 44 | + except Exception as e: # pragma: no cover |
| 45 | + self._citation_err_msg = e |
42 | 46 |
|
43 | 47 | def _reset(self):
|
44 | 48 | """Reset citations to default only (only for testing purposes)"""
|
@@ -93,27 +97,27 @@ def register(self, key):
|
93 | 97 | - The citation key for an entry in `pybamm/CITATIONS.txt` or
|
94 | 98 | - One or more BibTex formatted citations
|
95 | 99 | """
|
96 |
| - |
97 |
| - # Check if citation is a known key |
98 |
| - if key in self._all_citations: |
99 |
| - self._papers_to_cite.add(key) |
100 |
| - return |
101 |
| - |
102 |
| - # Try to parse the citation using pybtex |
103 |
| - try: |
104 |
| - # Parse string as a bibtex citation, and check that a citation was found |
105 |
| - bib_data = parse_string(key, bib_format="bibtex") |
106 |
| - if not bib_data.entries: |
107 |
| - raise PybtexError("no entries found") |
108 |
| - |
109 |
| - # Add and register all citations |
110 |
| - for key, entry in bib_data.entries.items(): |
111 |
| - self._add_citation(key, entry) |
112 |
| - 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) |
113 | 104 | return
|
114 |
| - except PybtexError: |
115 |
| - # Unable to parse / unknown key |
116 |
| - 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}") |
117 | 121 |
|
118 | 122 | def print(self, filename=None, output_format="text"):
|
119 | 123 | """Print all citations that were used for running simulations.
|
@@ -145,11 +149,14 @@ def print(self, filename=None, output_format="text"):
|
145 | 149 |
|
146 | 150 | def print_citations(filename=None, output_format="text"):
|
147 | 151 | """See :meth:`Citations.print`"""
|
148 |
| - if "google.colab" in sys.modules: |
149 |
| - raise ImportWarning( |
150 |
| - "pybtex does not work with Google Colab due to a known bug -" |
151 |
| - "https://bitbucket.org/pybtex-devs/pybtex/issues/148/." |
| 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/. " |
152 | 157 | "Please manually cite all the references."
|
| 158 | + "\nError encountered -\n" |
| 159 | + f"{citations._citation_err_msg}" |
153 | 160 | )
|
154 | 161 | else:
|
155 | 162 | pybamm.citations.print(filename, output_format)
|
|
0 commit comments