Skip to content

Commit 39c399a

Browse files
authored
Add citations management functions (#14)
* Add citations management functions * Add a test
1 parent 122de37 commit 39c399a

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

petsctools/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# is not available then attempting to access these attributes will raise an
1515
# informative error.
1616
if PETSC4PY_INSTALLED:
17+
from .citation import add_citation, cite, print_citations_at_exit # noqa: F401
1718
from .config import get_blas_library # noqa: F401
1819
from .init import ( # noqa: F401
1920
InvalidEnvironmentException,
@@ -37,6 +38,9 @@
3738

3839
def __getattr__(name):
3940
petsc4py_attrs = {
41+
"add_citation",
42+
"cite",
43+
"print_citations_at_exit",
4044
"get_blas_library",
4145
"InvalidEnvironmentException",
4246
"InvalidPetscVersionException",

petsctools/citation.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Module containing functions for registering citations through PETSc.
2+
3+
The functions in this module may be used to record Bibtex citation
4+
information and then register that a particular citation is
5+
relevant for a particular computation. It hooks up with PETSc's
6+
citation registration mechanism, so that running with
7+
``-citations`` does the right thing.
8+
9+
Example usage::
10+
11+
petsctools.add_citation("key", "bibtex-entry-for-my-funky-method")
12+
13+
...
14+
15+
if using_funky_method:
16+
petsctools.cite("key")
17+
18+
"""
19+
20+
from petsc4py import PETSc
21+
22+
23+
_citations_database = {}
24+
25+
26+
def add_citation(cite_key: str, entry: str) -> None:
27+
"""Add a paper to the database of possible citations.
28+
29+
Parameters
30+
----------
31+
cite_key :
32+
The key to use.
33+
entry :
34+
The bibtex entry.
35+
36+
"""
37+
_citations_database[cite_key] = entry
38+
39+
40+
def cite(cite_key: str) -> None:
41+
"""Cite a paper.
42+
43+
The paper should already have been added to the citations database using
44+
`add_citation`.
45+
46+
Parameters
47+
----------
48+
cite_key :
49+
The key of the relevant citation.
50+
51+
Raises
52+
------
53+
KeyError :
54+
If no such citation is found in the database.
55+
56+
"""
57+
if cite_key in _citations_database:
58+
citation = _citations_database[cite_key]
59+
PETSc.Sys.registerCitation(citation)
60+
else:
61+
raise KeyError(
62+
f"Did not find a citation for '{cite_key}', please add it to the "
63+
"citations database"
64+
)
65+
66+
67+
def print_citations_at_exit() -> None:
68+
"""Print citations at the end of the program."""
69+
# We devolve to PETSc for actually printing citations.
70+
PETSc.Options()["citations"] = None

tests/test_citations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
import petsctools
4+
5+
6+
@pytest.mark.skipnopetsc4py
7+
def test_cannot_cite_nonexistent_citation():
8+
with pytest.raises(KeyError):
9+
petsctools.cite("nonexistent")
10+
11+
12+
@pytest.mark.skipnopetsc4py
13+
def test_cite_citation():
14+
petsctools.add_citation("mykey", "myentry")
15+
petsctools.cite("mykey")

0 commit comments

Comments
 (0)