Skip to content

Commit 7ff2938

Browse files
authored
BlockMiddleware: default allow_parallel_execution=True (#460)
1 parent 5e00d18 commit 7ff2938

3 files changed

Lines changed: 100 additions & 18 deletions

File tree

bibtexparser/middlewares/middleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class Middleware(abc.ABC):
2121
or LibraryMiddleware"""
2222

2323
def __init__(
24-
self, allow_parallel_execution: bool, allow_inplace_modification: bool = True
24+
self,
25+
allow_parallel_execution: bool = True,
26+
allow_inplace_modification: bool = True,
2527
):
2628
"""
2729

docs/source/customize.rst

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,43 @@ by selecting the corresponding argument when calling :code:`bibtexparser.parse`
6868

6969
Core Middleware
7070
^^^^^^^^^^^^^^^
71-
The following middleware layers are part of the core functionality of bibtexparser and maintained as part of the main repository.
72-
The functionality is straightforward from the class names, so we will not go into detail here and refer to the
73-
docstrings of the classes instead.
7471

75-
Middleware Layers Regarding Encoding and Enclosing of Values
72+
bibtexparser comes with a number of middleware options:
73+
74+
.. _middleware_encoding:
75+
76+
Encoding and Enclosing of Values
77+
::::::::::::::::::::::::::::::::
7678

7779
* :mod:`bibtexparser.middlewares.AddEnclosingMiddleware`
7880
* :mod:`bibtexparser.middlewares.RemoveEnclosingMiddleware`
7981
* :mod:`bibtexparser.middlewares.LatexEncodingMiddleware`
8082
* :mod:`bibtexparser.middlewares.LatexDecodingMiddleware`
8183

82-
Middleware Layers Regarding Value References and Representation
84+
.. _middleware_references:
85+
86+
Value References and Representation
87+
:::::::::::::::::::::::::::::::::::
8388

8489
* :mod:`bibtexparser.middlewares.ResolveStringReferencesMiddleware`
8590
* :mod:`bibtexparser.middlewares.MonthIntMiddleware`
8691
* :mod:`bibtexparser.middlewares.MonthAbbreviationMiddleware`
8792
* :mod:`bibtexparser.middlewares.MonthLongStringMiddleware`
8893

89-
Middleware Layers Regarding Names
94+
.. _middleware_names:
95+
96+
Names
97+
:::::
9098

9199
* :mod:`bibtexparser.middlewares.SeparateCoAuthors`
92100
* :mod:`bibtexparser.middlewares.MergeCoAuthors`
93101
* :mod:`bibtexparser.middlewares.SplitNameParts` (requires SeperateCoAuthors to be applied first)
94102
* :mod:`bibtexparser.middlewares.MergeNameParts`
95103

96-
Sorting Middleware Layers
104+
.. _middleware_sorting:
105+
106+
Sorting
107+
:::::::
97108

98109
* :mod:`bibtexparser.middlewares.SortBlocksByTypeAndKeyMiddleware`
99110
* :mod:`bibtexparser.middlewares.SortFieldsAlphabeticallyMiddleware`
@@ -106,17 +117,86 @@ Sorting Middleware Layers
106117
even if it comes at the cost of slightly reduced functionality and performance.
107118
See the migration docs, if you are migrating from bibtexparser v1.
108119

120+
Write your own Middleware
121+
^^^^^^^^^^^^^^^^^^^^^^^^^
122+
123+
Functions working on blocks individually
124+
::::::::::::::::::::::::::::::::::::::::
125+
126+
Should extend the :class:`bibtexparser.middlewares.BlockMiddleware` class.
127+
This includes functionalities similar to
128+
:ref:`middleware_encoding`, :ref:`middleware_references`, and :ref:`middleware_names`.
129+
130+
* Basic example:
131+
132+
.. code-block:: python
133+
134+
from bibtexparser.middlewares import BlockMiddleware
135+
136+
class MyMiddleware(BlockMiddleware):
137+
def transform_entry(self, entry, *args, **kwargs):
138+
# Do something with the entry, e.g.
139+
entry["title"] = entry["title"].lower()
140+
# Return the transformed entry
141+
return entry
142+
143+
* Initialize the middleware with some parameters:
144+
145+
.. code-block:: python
146+
147+
from bibtexparser.middlewares import BlockMiddleware
148+
149+
class MyMiddleware(BlockMiddleware):
150+
def __init__(self, my_param):
151+
self.my_param = my_param
152+
super().__init__()
153+
154+
def transform_entry(self, entry, *args, **kwargs):
155+
# Do something with the entry, e.g.
156+
entry["title"] = entry["title"].lower()
157+
# Return the transformed entry
158+
return entry
159+
160+
Library-wide transformations
161+
::::::::::::::::::::::::::::
162+
163+
Should extend the :class:`bibtexparser.middlewares.LibraryMiddleware` class.
164+
This includes functionalities similar to sorting blocks
165+
(e.g. :mod:`bibtexparser.middlewares.SortBlocksByTypeAndKeyMiddleware`).
166+
167+
Warning
168+
:::::::
169+
170+
:class:`bibtexparser.middlewares.BlockMiddleware` and :class:`bibtexparser.middlewares.LibraryMiddleware`
171+
have two default arguments:
172+
173+
* ``allow_parallel_execution=True``, see :py:meth:`bibtexparser.middlewares.Middleware.allow_inplace_modification`.
174+
* ``allow_inplace_modification=True``, see :py:meth:`bibtexparser.middlewares.Middleware.allow_parallel_execution`.
175+
176+
If you want to change these defaults, specify them in the call to the super constructor. E.g.:
177+
178+
.. code-block:: python
179+
180+
from bibtexparser.middlewares import BlockMiddleware
181+
182+
class MyMiddleware(BlockMiddleware):
183+
def __init__(self, my_param):
184+
self.my_param = my_param
185+
super().__init__(
186+
allow_parallel_execution = False,
187+
allow_inplace_modification = False,
188+
)
189+
190+
def transform_entry(self, entry, *args, **kwargs):
191+
# Do something with the entry, e.g.
192+
entry["title"] = entry["title"].lower()
193+
# Return the transformed entry
194+
return entry
195+
109196
Community-Provided Middleware
110197
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111-
Aiming to keep the core functionality of bibtexparser simple, we encourage users to provide their own middleware layers
112-
and share them with the community.
113-
We will be happy to provide a list of community-provided middleware layers here, so please let us know if you have written one!
114-
115-
.. note::
116-
To write your own middleware, simply extend the :class:`bibtexparser.middlewares.BlockMiddleware`
117-
(for functions working on blocks individually, such as encoding) or :class:`bibtexparser.middlewares.LibraryMiddleware`
118-
(for library-wide transformations, such as sorting blocks) and implement the superclass methods
119-
according to the python docstrings. Make sure to check out some core middleware layers for examples.
198+
We encourage users to provide their own middleware layers and share them with the community.
199+
We are happy to provide a list of community-provided middleware layers here, so please let us know if you have written one!
120200

121201
Metadata Fields
122202
^^^^^^^^^^^^^^^

tests/middleware_tests/test_custom_middleware_smoke.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ def transform_entry(self, entry, *args, **kwargs):
2828
def test_custom_middleware_smoke():
2929
"""Test that the very simple custom middleware above works."""
3030
library = bibtexparser.parse_string(
31-
bibtex_str, append_middleware=[JournalAbbreviate(True)]
31+
bibtex_str, append_middleware=[JournalAbbreviate()]
3232
)
3333
assert library.entries[0]["journal"] == "NJ"

0 commit comments

Comments
 (0)