pyeasyphd 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyeasyphd might be problematic. Click here for more details.
- pyeasyphd/.python-version +1 -1
- pyeasyphd/main/__init__.py +0 -4
- pyeasyphd/main/basic_input.py +7 -63
- pyeasyphd/main/python_run_md.py +3 -3
- pyeasyphd/main/python_run_tex.py +1 -1
- pyeasyphd/pyeasyphd.sublime-settings +2 -160
- pyeasyphd/tools/__init__.py +1 -16
- pyeasyphd/tools/generate/generate_from_bibs.py +54 -330
- pyeasyphd/tools/generate/generate_html.py +122 -0
- pyeasyphd/tools/generate/generate_library.py +188 -0
- pyeasyphd/tools/generate/generate_links.py +13 -4
- pyeasyphd/tools/py_run_bib_md_tex.py +12 -13
- pyeasyphd/tools/search/search_base.py +8 -5
- pyeasyphd/tools/search/search_core.py +4 -3
- pyeasyphd/tools/search/search_keywords.py +1 -1
- pyeasyphd/tools/search/search_writers.py +8 -5
- {pyeasyphd-0.1.0.dist-info → pyeasyphd-0.1.2.dist-info}/METADATA +3 -6
- pyeasyphd-0.1.2.dist-info/RECORD +27 -0
- pyeasyphd/bib/__init__.py +0 -1
- pyeasyphd/bib/bibtexbase/__init__.py +0 -7
- pyeasyphd/bib/bibtexbase/standardize/_base.py +0 -36
- pyeasyphd/bib/bibtexbase/standardize/default_data.py +0 -97
- pyeasyphd/bib/bibtexbase/standardize/do_on_bib.py +0 -54
- pyeasyphd/bib/bibtexbase/standardize/do_on_comment_block.py +0 -38
- pyeasyphd/bib/bibtexbase/standardize/do_on_entry_block.py +0 -310
- pyeasyphd/bib/bibtexbase/standardize/do_on_preamble_block.py +0 -35
- pyeasyphd/bib/bibtexbase/standardize/do_on_string_block.py +0 -34
- pyeasyphd/bib/bibtexbase/standardize_bib.py +0 -75
- pyeasyphd/bib/bibtexparser/__init__.py +0 -47
- pyeasyphd/bib/bibtexparser/bibtex_format.py +0 -87
- pyeasyphd/bib/bibtexparser/exceptions.py +0 -64
- pyeasyphd/bib/bibtexparser/library.py +0 -207
- pyeasyphd/bib/bibtexparser/middlewares/block/add.py +0 -94
- pyeasyphd/bib/bibtexparser/middlewares/block/authors.py +0 -22
- pyeasyphd/bib/bibtexparser/middlewares/block/doi_url.py +0 -62
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_field_keys_normalize.py +0 -47
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_field_keys_replace.py +0 -31
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py +0 -222
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_fields_delete.py +0 -34
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_fields_keep.py +0 -33
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_fields_sort.py +0 -70
- pyeasyphd/bib/bibtexparser/middlewares/block/entry_types.py +0 -15
- pyeasyphd/bib/bibtexparser/middlewares/block/journal_booktitle.py +0 -113
- pyeasyphd/bib/bibtexparser/middlewares/block/month_year.py +0 -34
- pyeasyphd/bib/bibtexparser/middlewares/block/number_volume.py +0 -21
- pyeasyphd/bib/bibtexparser/middlewares/block/pages.py +0 -28
- pyeasyphd/bib/bibtexparser/middlewares/block/title.py +0 -20
- pyeasyphd/bib/bibtexparser/middlewares/library/generating_entrykeys.py +0 -98
- pyeasyphd/bib/bibtexparser/middlewares/library/keeping_blocks.py +0 -29
- pyeasyphd/bib/bibtexparser/middlewares/library/sorting_blocks.py +0 -124
- pyeasyphd/bib/bibtexparser/middlewares/middleware.py +0 -222
- pyeasyphd/bib/bibtexparser/middlewares/parsestack.py +0 -13
- pyeasyphd/bib/bibtexparser/middlewares/utils.py +0 -226
- pyeasyphd/bib/bibtexparser/middlewares_library_to_library.py +0 -414
- pyeasyphd/bib/bibtexparser/middlewares_library_to_str.py +0 -42
- pyeasyphd/bib/bibtexparser/middlewares_str_to_library.py +0 -35
- pyeasyphd/bib/bibtexparser/middlewares_str_to_str.py +0 -29
- pyeasyphd/bib/bibtexparser/model.py +0 -481
- pyeasyphd/bib/bibtexparser/splitter.py +0 -151
- pyeasyphd/bib/core/__init__.py +0 -18
- pyeasyphd/bib/core/convert_library_to_library.py +0 -31
- pyeasyphd/bib/core/convert_library_to_str.py +0 -199
- pyeasyphd/bib/core/convert_str_to_library.py +0 -34
- pyeasyphd/bib/core/convert_str_to_str.py +0 -27
- pyeasyphd/main/python_run_bib.py +0 -73
- pyeasyphd/main/python_writers.py +0 -212
- pyeasyphd/tools/compare/compare_bibs.py +0 -234
- pyeasyphd/tools/experiments_base.py +0 -203
- pyeasyphd/tools/format_save_bibs.py +0 -178
- pyeasyphd/tools/replace/replace.py +0 -81
- pyeasyphd/tools/spider/process_spider_bib.py +0 -247
- pyeasyphd/tools/spider/process_spider_url.py +0 -75
- pyeasyphd/tools/spider/process_spider_url_bib.py +0 -62
- pyeasyphd-0.1.0.dist-info/RECORD +0 -80
- {pyeasyphd-0.1.0.dist-info → pyeasyphd-0.1.2.dist-info}/WHEEL +0 -0
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
from typing import Any, Dict
|
|
2
|
-
|
|
3
|
-
from .library import Library
|
|
4
|
-
from .model import ImplicitComment
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class MiddlewaresStrToLibrary(object):
|
|
8
|
-
"""Middlewares for converting a string to a library.
|
|
9
|
-
|
|
10
|
-
Args:
|
|
11
|
-
options (dict): Options for the middlewares.
|
|
12
|
-
|
|
13
|
-
Attributes:
|
|
14
|
-
is_display_implicit_comments (bool): Display implicit comments. Default is True.
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
def __init__(self, options: Dict[str, Any]):
|
|
18
|
-
self.is_display_implicit_comments = options.get("is_display_implicit_comments", True)
|
|
19
|
-
|
|
20
|
-
def functions(self, library: Library) -> Library:
|
|
21
|
-
# Display implicit comments
|
|
22
|
-
if self.is_display_implicit_comments:
|
|
23
|
-
other_blocks, implicit_comment_blocks = [], []
|
|
24
|
-
for block in library.blocks:
|
|
25
|
-
if isinstance(block, ImplicitComment):
|
|
26
|
-
implicit_comment_blocks.append(block)
|
|
27
|
-
else:
|
|
28
|
-
other_blocks.append(block)
|
|
29
|
-
|
|
30
|
-
library = Library(other_blocks)
|
|
31
|
-
|
|
32
|
-
if implicit_comment_blocks:
|
|
33
|
-
print(implicit_comment_blocks)
|
|
34
|
-
|
|
35
|
-
return library
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
from typing import Any, Dict, List
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class MiddlewaresStrToStr(object):
|
|
6
|
-
r"""Middlewares for converting a string to a string.
|
|
7
|
-
|
|
8
|
-
Args:
|
|
9
|
-
options (dict): Options for the middlewares.
|
|
10
|
-
|
|
11
|
-
Attributes:
|
|
12
|
-
substitute_in_bib (bool): Substitute in the bib. Default is True.
|
|
13
|
-
substitute_old_list (list): List of old strings to substitute. Default is [].
|
|
14
|
-
substitute_new_list (list): List of new strings to substitute. Default is [].
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
def __init__(self, options: Dict[str, Any]):
|
|
18
|
-
self.substitute_in_bib = options.get("substitute_in_bib", True)
|
|
19
|
-
self.substitute_old_list = options.get("substitute_old_list", [])
|
|
20
|
-
self.substitute_new_list = options.get("substitute_new_list", [])
|
|
21
|
-
|
|
22
|
-
def functions(self, data_list: List[str]) -> List[str]:
|
|
23
|
-
# Substitute
|
|
24
|
-
if self.substitute_in_bib:
|
|
25
|
-
for i in range(len(data_list)):
|
|
26
|
-
for old, new in zip(self.substitute_old_list, self.substitute_new_list):
|
|
27
|
-
data_list[i] = re.sub(old, new, data_list[i])
|
|
28
|
-
|
|
29
|
-
return data_list
|
|
@@ -1,481 +0,0 @@
|
|
|
1
|
-
import abc
|
|
2
|
-
from typing import Any, Dict, List, Optional, Set
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Block(abc.ABC):
|
|
6
|
-
"""A abstract superclass of all top-level building blocks of a bibtex file.
|
|
7
|
-
|
|
8
|
-
E.g. a ``@string`` block, a ``@preamble`` block, an ``@entry`` block, a comment, etc.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
def __init__(
|
|
12
|
-
self,
|
|
13
|
-
start_line: Optional[int] = None,
|
|
14
|
-
raw: Optional[str] = None,
|
|
15
|
-
parser_metadata: Optional[Dict[str, Any]] = None,
|
|
16
|
-
):
|
|
17
|
-
self._start_line = start_line
|
|
18
|
-
self._raw = raw
|
|
19
|
-
self._parser_metadata: Dict[str, Any] = {}
|
|
20
|
-
if parser_metadata is not None:
|
|
21
|
-
self._parser_metadata: Dict[str, Any] = parser_metadata
|
|
22
|
-
|
|
23
|
-
@property
|
|
24
|
-
def start_line(self) -> Optional[int]:
|
|
25
|
-
"""The line number of the first line of this block in the parsed string."""
|
|
26
|
-
return self._start_line
|
|
27
|
-
|
|
28
|
-
@property
|
|
29
|
-
def raw(self) -> Optional[str]:
|
|
30
|
-
"""The raw, unmodified string (bibtex) representation of this block.
|
|
31
|
-
|
|
32
|
-
Note: Middleware does not update this field, hence, after applying middleware
|
|
33
|
-
to a library, this field may be outdated.
|
|
34
|
-
"""
|
|
35
|
-
return self._raw
|
|
36
|
-
|
|
37
|
-
@property
|
|
38
|
-
def parser_metadata(self) -> Dict[str, Any]:
|
|
39
|
-
"""EXPERIMENTAL: field for middleware to store auxiliary information.
|
|
40
|
-
|
|
41
|
-
As an end-user, as long as you are not writing middleware, you probably
|
|
42
|
-
do not need to use this field.
|
|
43
|
-
|
|
44
|
-
** Warning (experimental) **
|
|
45
|
-
The content of this field is undefined and may change at any time.
|
|
46
|
-
|
|
47
|
-
This field is intended for middleware to store auxiliary information.
|
|
48
|
-
It is a key-value store, where the key is a string and the value is any
|
|
49
|
-
python object.
|
|
50
|
-
This allows for example to pass information between different middleware.
|
|
51
|
-
"""
|
|
52
|
-
return self._parser_metadata
|
|
53
|
-
|
|
54
|
-
def get_parser_metadata(self, key: str) -> Optional[Any]:
|
|
55
|
-
"""EXPERIMENTAL: get auxiliary information stored in ``parser_metadata``.
|
|
56
|
-
|
|
57
|
-
See attribute ``parser_metadata`` for more information.
|
|
58
|
-
"""
|
|
59
|
-
return self._parser_metadata.get(key, None)
|
|
60
|
-
|
|
61
|
-
def set_parser_metadata(self, key: str, value: Any):
|
|
62
|
-
"""EXPERIMENTAL: set auxiliary information stored in ``parser_metadata``.
|
|
63
|
-
|
|
64
|
-
See attribute ``parser_metadata`` for more information.
|
|
65
|
-
"""
|
|
66
|
-
self._parser_metadata[key] = value
|
|
67
|
-
|
|
68
|
-
def __eq__(self, other):
|
|
69
|
-
# make sure they have the same type and same content
|
|
70
|
-
return (
|
|
71
|
-
isinstance(other, self.__class__)
|
|
72
|
-
and isinstance(self, other.__class__)
|
|
73
|
-
and self.__dict__ == other.__dict__
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
class String(Block):
|
|
78
|
-
"""Bibtex Blocks of the ``@string`` type, e.g. ``@string{me = "My Name"}``."""
|
|
79
|
-
|
|
80
|
-
def __init__(
|
|
81
|
-
self,
|
|
82
|
-
key: str,
|
|
83
|
-
value: str,
|
|
84
|
-
start_line: Optional[int] = None,
|
|
85
|
-
raw: Optional[str] = None,
|
|
86
|
-
):
|
|
87
|
-
super().__init__(start_line, raw)
|
|
88
|
-
self._key = key
|
|
89
|
-
self._value = value
|
|
90
|
-
|
|
91
|
-
@property
|
|
92
|
-
def key(self) -> str:
|
|
93
|
-
"""The key of the string, e.g. ``me`` in ``@string{me = "My Name"}``."""
|
|
94
|
-
return self._key
|
|
95
|
-
|
|
96
|
-
@key.setter
|
|
97
|
-
def key(self, value: str) -> None:
|
|
98
|
-
self._key = value
|
|
99
|
-
|
|
100
|
-
@property
|
|
101
|
-
def value(self) -> str:
|
|
102
|
-
"""The value of the string, e.g. ``"My Name"`` in ``@string{me = "My Name"}``."""
|
|
103
|
-
return self._value
|
|
104
|
-
|
|
105
|
-
@value.setter
|
|
106
|
-
def value(self, value: str) -> None:
|
|
107
|
-
self._value = value
|
|
108
|
-
|
|
109
|
-
def __str__(self) -> str:
|
|
110
|
-
return f"String (line: {self.start_line}, key: `{self.key}`): `{self.value}`"
|
|
111
|
-
|
|
112
|
-
def __repr__(self) -> str:
|
|
113
|
-
return (
|
|
114
|
-
f"String(key=`{self.key}`, value=`{self.value}`, "
|
|
115
|
-
f"start_line={self.start_line}, raw=`{self.raw}`)"
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
class Preamble(Block):
|
|
120
|
-
"""Bibtex Blocks of the ``@preamble`` type, e.g. ``@preamble{This is a preamble}``."""
|
|
121
|
-
|
|
122
|
-
def __init__(self, value: str, start_line: Optional[int] = None, raw: Optional[str] = None):
|
|
123
|
-
super().__init__(start_line, raw)
|
|
124
|
-
self._value = value
|
|
125
|
-
|
|
126
|
-
@property
|
|
127
|
-
def value(self) -> str:
|
|
128
|
-
"""The value of the preamble, e.g. ``blabla`` in ``@preamble{blabla}``."""
|
|
129
|
-
return self._value
|
|
130
|
-
|
|
131
|
-
@value.setter
|
|
132
|
-
def value(self, value: str) -> None:
|
|
133
|
-
self._value = value
|
|
134
|
-
|
|
135
|
-
def __str__(self) -> str:
|
|
136
|
-
return f"Preamble (line: {self.start_line}): `{self.value}`"
|
|
137
|
-
|
|
138
|
-
def __repr__(self) -> str:
|
|
139
|
-
return f"Preamble(value=`{self.value}`, " f"start_line={self.start_line}, raw=`{self.raw}`)"
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
class ExplicitComment(Block):
|
|
143
|
-
"""Bibtex Blocks of the ``@comment`` type, e.g. ``@comment{This is a comment}``."""
|
|
144
|
-
|
|
145
|
-
def __init__(self, comment: str, start_line: Optional[int] = None, raw: Optional[str] = None):
|
|
146
|
-
super().__init__(start_line, raw)
|
|
147
|
-
self._comment = comment
|
|
148
|
-
|
|
149
|
-
@property
|
|
150
|
-
def comment(self) -> str:
|
|
151
|
-
"""The value of the comment, e.g. ``blabla`` in ``@comment{blabla}``."""
|
|
152
|
-
return self._comment
|
|
153
|
-
|
|
154
|
-
@comment.setter
|
|
155
|
-
def comment(self, value: str) -> None:
|
|
156
|
-
self._comment = value
|
|
157
|
-
|
|
158
|
-
def __str__(self) -> str:
|
|
159
|
-
return f"ExplicitComment (line: {self.start_line}): `{self.comment}`"
|
|
160
|
-
|
|
161
|
-
def __repr__(self) -> str:
|
|
162
|
-
return (
|
|
163
|
-
f"ExplicitComment(comment=`{self.comment}`, "
|
|
164
|
-
f"start_line={self.start_line}, raw=`{self.raw}`)"
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
class ImplicitComment(Block):
|
|
169
|
-
"""Bibtex outside of an ``@{...}`` block, which is treated as a comment."""
|
|
170
|
-
|
|
171
|
-
def __init__(self, comment: str, start_line: Optional[int] = None, raw: Optional[str] = None):
|
|
172
|
-
super().__init__(start_line, raw)
|
|
173
|
-
self._comment = comment
|
|
174
|
-
|
|
175
|
-
@property
|
|
176
|
-
def comment(self) -> str:
|
|
177
|
-
"""The (possibly multi-line) comment."""
|
|
178
|
-
return self._comment
|
|
179
|
-
|
|
180
|
-
@comment.setter
|
|
181
|
-
def comment(self, value: str) -> None:
|
|
182
|
-
self._comment = value
|
|
183
|
-
|
|
184
|
-
def __str__(self) -> str:
|
|
185
|
-
return f"ImplicitComment (line: {self.start_line}): `{self.comment}`"
|
|
186
|
-
|
|
187
|
-
def __repr__(self) -> str:
|
|
188
|
-
return (
|
|
189
|
-
f"ImplicitComment(comment=`{self.comment}`, "
|
|
190
|
-
f"start_line={self.start_line}, raw=`{self.raw}`)"
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
class Field(object):
|
|
195
|
-
"""A field of a Bibtex entry, e.g. ``author = {John Doe}``."""
|
|
196
|
-
|
|
197
|
-
def __init__(self, key: str, value: Any, start_line: Optional[int] = None):
|
|
198
|
-
self._start_line = start_line
|
|
199
|
-
self._key = key
|
|
200
|
-
self._value = value
|
|
201
|
-
|
|
202
|
-
@property
|
|
203
|
-
def key(self) -> str:
|
|
204
|
-
"""The key of the field, e.g. ``author`` in ``author = {John Doe}``."""
|
|
205
|
-
return self._key
|
|
206
|
-
|
|
207
|
-
@key.setter
|
|
208
|
-
def key(self, value: str) -> None:
|
|
209
|
-
self._key = value
|
|
210
|
-
|
|
211
|
-
@property
|
|
212
|
-
def value(self) -> Any:
|
|
213
|
-
"""The value of the field, e.g. ``{John Doe}`` in ``author = {John Doe}``."""
|
|
214
|
-
return self._value
|
|
215
|
-
|
|
216
|
-
@value.setter
|
|
217
|
-
def value(self, value: Any) -> None:
|
|
218
|
-
self._value = value
|
|
219
|
-
|
|
220
|
-
@property
|
|
221
|
-
def start_line(self) -> Optional[int]:
|
|
222
|
-
"""The line number of the first line of this field in the originally parsed string."""
|
|
223
|
-
return self._start_line
|
|
224
|
-
|
|
225
|
-
def __eq__(self, other):
|
|
226
|
-
# make sure they have the same type and same content
|
|
227
|
-
return (
|
|
228
|
-
isinstance(other, self.__class__)
|
|
229
|
-
and isinstance(self, other.__class__)
|
|
230
|
-
and self.__dict__ == other.__dict__
|
|
231
|
-
)
|
|
232
|
-
|
|
233
|
-
def __str__(self) -> str:
|
|
234
|
-
return f"Field (line: {self.start_line}, key: `{self.key}`): `{self.value}`"
|
|
235
|
-
|
|
236
|
-
def __repr__(self) -> str:
|
|
237
|
-
return f"Field(key=`{self.key}`, value=`{self.value}`, " f"start_line={self.start_line})"
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
class Entry(Block):
|
|
241
|
-
"""Bibtex Blocks of the ``@entry`` type, e.g. ``@article{Cesar2013, ...}``."""
|
|
242
|
-
|
|
243
|
-
def __init__(
|
|
244
|
-
self,
|
|
245
|
-
entry_type: str,
|
|
246
|
-
key: str,
|
|
247
|
-
fields: List[Field],
|
|
248
|
-
start_line: Optional[int] = None,
|
|
249
|
-
raw: Optional[str] = None,
|
|
250
|
-
):
|
|
251
|
-
super().__init__(start_line, raw)
|
|
252
|
-
self._entry_type = entry_type
|
|
253
|
-
self._key = key
|
|
254
|
-
self._fields = fields
|
|
255
|
-
|
|
256
|
-
@property
|
|
257
|
-
def entry_type(self) -> str:
|
|
258
|
-
"""The type of the entry, e.g. ``article`` in ``@article{Cesar2013, ...}``."""
|
|
259
|
-
return self._entry_type
|
|
260
|
-
|
|
261
|
-
@entry_type.setter
|
|
262
|
-
def entry_type(self, value: str) -> None:
|
|
263
|
-
self._entry_type = value
|
|
264
|
-
|
|
265
|
-
@property
|
|
266
|
-
def key(self) -> str:
|
|
267
|
-
"""The key of the entry, e.g. ``Cesar2013`` in ``@article{Cesar2013, ...}``."""
|
|
268
|
-
return self._key
|
|
269
|
-
|
|
270
|
-
@key.setter
|
|
271
|
-
def key(self, value: str) -> None:
|
|
272
|
-
self._key = value
|
|
273
|
-
|
|
274
|
-
@property
|
|
275
|
-
def fields(self) -> List[Field]:
|
|
276
|
-
"""The key-value attributes of an entry, as ``Field`` instances."""
|
|
277
|
-
return self._fields
|
|
278
|
-
|
|
279
|
-
@fields.setter
|
|
280
|
-
def fields(self, value: List[Field]) -> None:
|
|
281
|
-
self._fields = value
|
|
282
|
-
|
|
283
|
-
@property
|
|
284
|
-
def fields_dict(self) -> Dict[str, Field]:
|
|
285
|
-
"""A dict of fields, with field keys as keys.
|
|
286
|
-
|
|
287
|
-
Note that with duplicate field keys, the behavior is undefined.
|
|
288
|
-
"""
|
|
289
|
-
return {field.key: field for field in self._fields}
|
|
290
|
-
|
|
291
|
-
def set_field(self, field: Field):
|
|
292
|
-
"""Add a new field, or replaces existing with same key."""
|
|
293
|
-
if field.key in self.fields_dict:
|
|
294
|
-
i = [f.key for f in self._fields].index(field.key)
|
|
295
|
-
self._fields[i] = field
|
|
296
|
-
else:
|
|
297
|
-
self._fields.append(field)
|
|
298
|
-
|
|
299
|
-
def pop(self, key: str, default=None) -> Optional[Field]:
|
|
300
|
-
"""Remove and return the field with the given key.
|
|
301
|
-
|
|
302
|
-
:param key: The key of the field to remove.
|
|
303
|
-
:param default: The value to return if the field does not exist.
|
|
304
|
-
"""
|
|
305
|
-
try:
|
|
306
|
-
field = self.fields_dict.pop(key)
|
|
307
|
-
except KeyError:
|
|
308
|
-
return default
|
|
309
|
-
|
|
310
|
-
self._fields = [f for f in self._fields if f.key != key]
|
|
311
|
-
return field
|
|
312
|
-
|
|
313
|
-
def get(self, key: str, default=None) -> Optional[Field]:
|
|
314
|
-
"""Return the field with the given key, or the default value if it does not exist.
|
|
315
|
-
|
|
316
|
-
:param key: The key of the field.
|
|
317
|
-
:param default: The value to return if the field does not exist.
|
|
318
|
-
"""
|
|
319
|
-
return self.fields_dict.get(key, default)
|
|
320
|
-
|
|
321
|
-
def __contains__(self, key: str) -> bool:
|
|
322
|
-
"""Dict-mimicking ``in`` operator."""
|
|
323
|
-
return key in self.fields_dict
|
|
324
|
-
|
|
325
|
-
def __getitem__(self, key: str) -> Any:
|
|
326
|
-
"""Dict-mimicking index.
|
|
327
|
-
|
|
328
|
-
This serves for partial v1.x backwards compatibility,
|
|
329
|
-
as well as for a shorthand for accessing field values.
|
|
330
|
-
|
|
331
|
-
Note that with duplicate field keys, the behavior is undefined.
|
|
332
|
-
"""
|
|
333
|
-
if key == "ENTRYTYPE":
|
|
334
|
-
return self.entry_type
|
|
335
|
-
if key == "ID":
|
|
336
|
-
return self.key
|
|
337
|
-
return self.fields_dict[key].value
|
|
338
|
-
|
|
339
|
-
def __setitem__(self, key: str, value: Any):
|
|
340
|
-
"""Dict-mimicking index.
|
|
341
|
-
|
|
342
|
-
This serves for partial v1.x backwards compatibility,
|
|
343
|
-
as well as for a shorthand for `set_field`.
|
|
344
|
-
"""
|
|
345
|
-
self.set_field(Field(key, value))
|
|
346
|
-
|
|
347
|
-
def __delitem__(self, key):
|
|
348
|
-
"""Dict-mimicking index.
|
|
349
|
-
|
|
350
|
-
This serves for partial v1.x backwards compatibility,
|
|
351
|
-
as well as for a shorthand for `pop`.
|
|
352
|
-
"""
|
|
353
|
-
self.pop(key)
|
|
354
|
-
|
|
355
|
-
def items(self):
|
|
356
|
-
"""Dict-mimicking, for partial v1.x backwards compatibility.
|
|
357
|
-
|
|
358
|
-
For newly written code, it's recommended to use `entry.entry_type`,
|
|
359
|
-
`entry.key` and `entry.fields` instead.
|
|
360
|
-
"""
|
|
361
|
-
return [
|
|
362
|
-
("ENTRYTYPE", self.entry_type),
|
|
363
|
-
("ID", self.key),
|
|
364
|
-
] + [(f.key, f.value) for f in self.fields]
|
|
365
|
-
|
|
366
|
-
def __str__(self) -> str:
|
|
367
|
-
lines = [f"Entry (line: {self.start_line}, type: `{self.entry_type}`, key: `{self.key}`):"]
|
|
368
|
-
lines.extend([f"\t`{f.key}` = `{f.value}`" for f in self.fields])
|
|
369
|
-
return "\n".join(lines)
|
|
370
|
-
|
|
371
|
-
def __repr__(self) -> str:
|
|
372
|
-
return (
|
|
373
|
-
f"Entry(entry_type=`{self.entry_type}`, key=`{self.key}`, "
|
|
374
|
-
f"fields=`{self.fields.__repr__()}`, start_line={self.start_line})"
|
|
375
|
-
)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
class ParsingFailedBlock(Block):
|
|
379
|
-
"""A block that could not be parsed due to some raised exception."""
|
|
380
|
-
|
|
381
|
-
def __init__(
|
|
382
|
-
self,
|
|
383
|
-
error: Exception,
|
|
384
|
-
start_line: Optional[int] = None,
|
|
385
|
-
raw: Optional[str] = None,
|
|
386
|
-
ignore_error_block: Optional[Block] = None,
|
|
387
|
-
):
|
|
388
|
-
super().__init__(start_line, raw)
|
|
389
|
-
self._error = error
|
|
390
|
-
self._ignore_error_block = ignore_error_block
|
|
391
|
-
|
|
392
|
-
@property
|
|
393
|
-
def error(self) -> Exception:
|
|
394
|
-
"""The exception that was raised during parsing."""
|
|
395
|
-
return self._error
|
|
396
|
-
|
|
397
|
-
@property
|
|
398
|
-
def ignore_error_block(self) -> Optional[Block]:
|
|
399
|
-
"""The possibly faulty block when ignoring the error.
|
|
400
|
-
|
|
401
|
-
This may be None, as it may not always be possible to ignore the error.
|
|
402
|
-
For errors caused by middleware, this is typically the block without
|
|
403
|
-
the middleware applied.
|
|
404
|
-
"""
|
|
405
|
-
return self._ignore_error_block
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
class MiddlewareErrorBlock(ParsingFailedBlock):
|
|
409
|
-
"""A block that could not be parsed due to a middleware error.
|
|
410
|
-
|
|
411
|
-
To get the block that caused this error, call `block.ignore_error_block`
|
|
412
|
-
(which is the block with the middleware not or only partially applied).
|
|
413
|
-
"""
|
|
414
|
-
|
|
415
|
-
def __init__(self, block: Block, error: Exception):
|
|
416
|
-
super().__init__(
|
|
417
|
-
start_line=block.start_line,
|
|
418
|
-
raw=block.raw,
|
|
419
|
-
error=error,
|
|
420
|
-
ignore_error_block=block,
|
|
421
|
-
)
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
class DuplicateBlockKeyBlock(ParsingFailedBlock):
|
|
425
|
-
"""An error-indicating block created for blocks with keys present in the library already.
|
|
426
|
-
|
|
427
|
-
To get the block that caused this error, call `block.ignore_error_block`.
|
|
428
|
-
"""
|
|
429
|
-
|
|
430
|
-
def __init__(
|
|
431
|
-
self,
|
|
432
|
-
key: str,
|
|
433
|
-
previous_block: Block,
|
|
434
|
-
duplicate_block: Block,
|
|
435
|
-
start_line: Optional[int] = None,
|
|
436
|
-
raw: Optional[str] = None,
|
|
437
|
-
):
|
|
438
|
-
super().__init__(
|
|
439
|
-
error=Exception(f"Duplicate entry key '{key}'"),
|
|
440
|
-
start_line=start_line,
|
|
441
|
-
raw=raw,
|
|
442
|
-
ignore_error_block=duplicate_block,
|
|
443
|
-
)
|
|
444
|
-
self._key = key
|
|
445
|
-
self._previous_block = previous_block
|
|
446
|
-
|
|
447
|
-
@property
|
|
448
|
-
def key(self) -> str:
|
|
449
|
-
"""The key of the entry, e.g. ``Cesar2013`` in ``@article{Cesar2013, ...}``."""
|
|
450
|
-
return self._key
|
|
451
|
-
|
|
452
|
-
@key.setter
|
|
453
|
-
def key(self, value: str) -> None:
|
|
454
|
-
self._key = value
|
|
455
|
-
|
|
456
|
-
@property
|
|
457
|
-
def previous_block(self) -> Block:
|
|
458
|
-
"""A reference to a previous block with the same key."""
|
|
459
|
-
return self._previous_block
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
class DuplicateFieldKeyBlock(ParsingFailedBlock):
|
|
463
|
-
"""An error-indicating block indicating a duplicate field key in an entry."""
|
|
464
|
-
|
|
465
|
-
def __init__(self, duplicate_keys: Set[str], entry: Entry):
|
|
466
|
-
sorted_duplicate_keys = sorted(list(duplicate_keys))
|
|
467
|
-
super().__init__(
|
|
468
|
-
error=Exception(
|
|
469
|
-
f"Duplicate field keys on entry: '{', '.join(sorted_duplicate_keys)}'."
|
|
470
|
-
f"Note: The entry (containing duplicate) is available as `failed_block.entry`"
|
|
471
|
-
),
|
|
472
|
-
start_line=entry.start_line,
|
|
473
|
-
raw=entry.raw,
|
|
474
|
-
ignore_error_block=entry,
|
|
475
|
-
)
|
|
476
|
-
self._duplicate_keys: Set[str] = duplicate_keys
|
|
477
|
-
|
|
478
|
-
@property
|
|
479
|
-
def duplicate_keys(self) -> Set[str]:
|
|
480
|
-
"""The field-keys that occurred more than once in the entry."""
|
|
481
|
-
return self._duplicate_keys
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
from typing import List, Tuple, Union
|
|
3
|
-
|
|
4
|
-
from .library import Library
|
|
5
|
-
from .model import Block, Entry, ExplicitComment, Field, ImplicitComment, Preamble, String
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class Splitter(object):
|
|
9
|
-
"""Splitter class to split standardizing bib data list to library.
|
|
10
|
-
|
|
11
|
-
Attributes:
|
|
12
|
-
regex_block_type: Regular expression to match block type.
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
def __init__(self):
|
|
16
|
-
self.regex_block_type = re.compile(r"@([a-zA-Z]+){")
|
|
17
|
-
|
|
18
|
-
def splitter(self, data_list: List[str], implicit_coments: List[List[str]]):
|
|
19
|
-
"""Split standardizing bib data list to library."""
|
|
20
|
-
_blocks = []
|
|
21
|
-
|
|
22
|
-
_blocks.extend([ImplicitComment(i[0]) for i in implicit_coments if i])
|
|
23
|
-
|
|
24
|
-
implicit_comment_blocks = []
|
|
25
|
-
explicit_comment_blocks = []
|
|
26
|
-
string_blocks = []
|
|
27
|
-
preamble_blocks = []
|
|
28
|
-
entry_blocks = []
|
|
29
|
-
|
|
30
|
-
# Initialize
|
|
31
|
-
data_list = "".join(data_list).splitlines(keepends=True)
|
|
32
|
-
data_list = [line for line in data_list if line.strip()]
|
|
33
|
-
|
|
34
|
-
line_index, len_data = 0, len(data_list)
|
|
35
|
-
while line_index < len_data:
|
|
36
|
-
line = data_list[line_index]
|
|
37
|
-
line_index += 1
|
|
38
|
-
|
|
39
|
-
if not (mch_block := self.regex_block_type.search(line)):
|
|
40
|
-
implicit_comment_blocks.append(ImplicitComment(line, line_index))
|
|
41
|
-
continue
|
|
42
|
-
|
|
43
|
-
block_type = mch_block.group(1)
|
|
44
|
-
if block_type == "comment":
|
|
45
|
-
block, line_index = self._splitter_comment(block_type, line, line_index, len_data, data_list)
|
|
46
|
-
if isinstance(block, Block):
|
|
47
|
-
explicit_comment_blocks.append(block)
|
|
48
|
-
elif isinstance(block, str):
|
|
49
|
-
implicit_comment_blocks.append(ImplicitComment(block, line_index - 1))
|
|
50
|
-
|
|
51
|
-
elif block_type == "string":
|
|
52
|
-
block, line_index = self._splitter_string(block_type, line, line_index, len_data, data_list)
|
|
53
|
-
if isinstance(block, Block):
|
|
54
|
-
string_blocks.append(block)
|
|
55
|
-
elif isinstance(block, str):
|
|
56
|
-
implicit_comment_blocks.append(ImplicitComment(block, line_index - 1))
|
|
57
|
-
|
|
58
|
-
elif block_type == "preamble":
|
|
59
|
-
block, line_index = self._splitter_preamble(block_type, line, line_index, len_data, data_list)
|
|
60
|
-
if isinstance(block, Block):
|
|
61
|
-
preamble_blocks.append(block)
|
|
62
|
-
elif isinstance(block, str):
|
|
63
|
-
implicit_comment_blocks.append(ImplicitComment(block, line_index - 1))
|
|
64
|
-
|
|
65
|
-
else:
|
|
66
|
-
block, line_index, temp = self._splitter_entry(block_type, line, line_index, len_data, data_list)
|
|
67
|
-
entry_blocks.append(block)
|
|
68
|
-
implicit_comment_blocks.extend(temp)
|
|
69
|
-
|
|
70
|
-
_blocks.extend(implicit_comment_blocks)
|
|
71
|
-
_blocks.extend(explicit_comment_blocks)
|
|
72
|
-
_blocks.extend(string_blocks)
|
|
73
|
-
_blocks.extend(preamble_blocks)
|
|
74
|
-
_blocks.extend(entry_blocks)
|
|
75
|
-
return Library(_blocks)
|
|
76
|
-
|
|
77
|
-
def _splitter_entry(self, block_type, line, line_idx, len_data, data_list) -> Tuple[Union[Block, str], int, list]:
|
|
78
|
-
regex = re.compile(r"@([a-zA-Z]+){(.*),")
|
|
79
|
-
if not (mch_entry := regex.search(line)):
|
|
80
|
-
block = Entry(block_type, "", [], line_idx)
|
|
81
|
-
else:
|
|
82
|
-
block = Entry(mch_entry.group(1), mch_entry.group(2).strip(), [], line_idx)
|
|
83
|
-
|
|
84
|
-
regex_field_type = re.compile(r"[%\s]*([\w\-]+)" + r'\s*=\s*["{](.*)["}],\n', flags=re.I)
|
|
85
|
-
regex_field_type_abbr = re.compile(r"[%\s]*([\w\-]+)" + r"\s*=\s*([\w\-]+),\n", flags=re.I)
|
|
86
|
-
implicit_comment_blocks = []
|
|
87
|
-
while line_idx < len_data:
|
|
88
|
-
new_line = data_list[line_idx]
|
|
89
|
-
if self.regex_block_type.match(new_line):
|
|
90
|
-
break
|
|
91
|
-
|
|
92
|
-
if mch := regex_field_type.match(new_line):
|
|
93
|
-
block.set_field(Field(mch.group(1), mch.group(2), start_line=None))
|
|
94
|
-
elif mch := regex_field_type_abbr.match(new_line):
|
|
95
|
-
block.set_field(Field(mch.group(1), mch.group(2), start_line=None))
|
|
96
|
-
elif (new_line.strip() == "}") or (new_line.strip() == ""):
|
|
97
|
-
pass
|
|
98
|
-
else:
|
|
99
|
-
implicit_comment_blocks.append(ImplicitComment(new_line, line_idx))
|
|
100
|
-
line_idx += 1
|
|
101
|
-
|
|
102
|
-
if not block.key:
|
|
103
|
-
block.key = self._generate_entry_key(block)
|
|
104
|
-
return block, line_idx, implicit_comment_blocks
|
|
105
|
-
|
|
106
|
-
@staticmethod
|
|
107
|
-
def _generate_entry_key(entry: Entry) -> str:
|
|
108
|
-
title = entry["title"] if "title" in entry else ""
|
|
109
|
-
year = entry["year"] if "year" in entry else ""
|
|
110
|
-
doi = entry["doi"] if "doi" in entry else ""
|
|
111
|
-
author = entry["author"] if "author" in entry else ""
|
|
112
|
-
keys = [entry.entry_type[:3]]
|
|
113
|
-
if year:
|
|
114
|
-
keys.append(year)
|
|
115
|
-
if doi:
|
|
116
|
-
keys.append(doi)
|
|
117
|
-
if author:
|
|
118
|
-
keys.append(author[:20])
|
|
119
|
-
if title:
|
|
120
|
-
keys.append(title[:70])
|
|
121
|
-
|
|
122
|
-
citation_key = re.sub(r"\W", "", "_".join(keys).lower())[:80]
|
|
123
|
-
while citation_key and citation_key[-1] == "_":
|
|
124
|
-
citation_key = citation_key[:-1]
|
|
125
|
-
return citation_key
|
|
126
|
-
|
|
127
|
-
def _splitter_comment(self, block_type, line, line_idx, len_data, data_list) -> Tuple[Union[Block, str], int]:
|
|
128
|
-
regex = re.compile(r"@comment{" + r"(.*)" + "}\n")
|
|
129
|
-
if not (mch := regex.search(line)):
|
|
130
|
-
return line, line_idx
|
|
131
|
-
|
|
132
|
-
block = ExplicitComment(mch.group(1), line_idx)
|
|
133
|
-
return block, line_idx
|
|
134
|
-
|
|
135
|
-
def _splitter_string(self, block_type, line, line_idx, len_data, data_list) -> Tuple[Union[Block, str], int]:
|
|
136
|
-
regex = re.compile(r"@string{" + r"\s*([\w]+)\s*=\s*" + r'(["{])' + r"([\w\-]+)" + r'(["}])' + "}\n")
|
|
137
|
-
if not (mch := regex.search(line)):
|
|
138
|
-
return line, line_idx
|
|
139
|
-
|
|
140
|
-
block = String(mch.group(1), mch.group(3), line_idx)
|
|
141
|
-
if not block.key:
|
|
142
|
-
block.key = re.sub(r"\W", "", block.value.lower())[:80]
|
|
143
|
-
return block, line_idx
|
|
144
|
-
|
|
145
|
-
def _splitter_preamble(self, block_type, line, line_idx, len_data, data_list) -> Tuple[Union[Block, str], int]:
|
|
146
|
-
regex = re.compile(r"@preamble{" + r'\s*(")' + r"([\w\-\\\[\]\{\}\s]+)" + r'(")\s*' + "}\n")
|
|
147
|
-
if not (mch := regex.search(line)):
|
|
148
|
-
return line, line_idx
|
|
149
|
-
|
|
150
|
-
block = Preamble(mch.group(2), line_idx)
|
|
151
|
-
return block, line_idx
|