-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
475 lines (399 loc) · 15.3 KB
/
database.py
File metadata and controls
475 lines (399 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import re
from collections.abc import Mapping
from functools import total_ordering
from pybtex.exceptions import PybtexError
from pybtex.utils import (
OrderedCaseInsensitiveDict, CaseInsensitiveDefaultDict, CaseInsensitiveSet
)
from pybtex.bibtex.utils import split_tex_string
from pybtex.errors import report_error
from pybtex import textutils
from .month_names import month_names
_whitespace_re = re.compile(r'\s+')
def normalize_whitespace(s):
return _whitespace_re.sub(' ', s)
class BibliographyDataError(PybtexError):
pass
class BibliographyData(object):
def __init__(self, entries=None, preamble=None):
self.entries = dict() #OrderedCaseInsensitiveDict()
self._preamble = []
if entries:
if isinstance(entries, Mapping):
entries = iter(entries.items())
for (key, entry) in entries:
self.add_entry(key, entry)
if preamble:
self._preamble.extend(preamble)
def __eq__(self, other):
if not isinstance(other, BibliographyData):
return super(BibliographyData, self) == other
return (
self.entries == other.entries
and self._preamble == other._preamble
)
def __repr__(self):
return 'BibliographyData(entries={entries}, preamble={preamble})'.format(
entries=repr(self.entries),
preamble=repr(self._preamble),
)
def add_to_preamble(self, *values):
self._preamble.extend(values)
def preamble(self):
return ''.join(self._preamble)
def add_entry(self, key, entry):
if not isinstance(key, EntryKey):
key = EntryKey.from_string(key)
if key in self.entries:
report_error(BibliographyDataError('repeated bibliography entry: %s' % key))
return
entry.collection = self
entry.key = key
self.entries[key] = entry
def add_entries(self, entries):
for key, entry in entries:
self.add_entry(key, entry)
class FieldDict(dict):
def __init__(self, parent, *args, **kwargw):
self.parent = parent
dict.__init__(self, *args, **kwargw)
def __missing__(self, key):
if key in self.parent.persons:
persons = self.parent.persons[key]
return ' and '.join(str(person) for person in persons)
elif 'crossref' in self:
return self.parent.get_crossref().fields[key]
else:
raise KeyError(key)
class Entry(object):
"""Bibliography entry. Important members are:
- persons (a dict of Person objects)
- fields (all dict of string)
"""
def __init__(self, type_, fields=None, persons=None, collection=None):
if fields is None:
fields = {}
if persons is None:
persons = {}
self.type = type_
self.fields = FieldDict(self, fields)
self.persons = dict(persons)
self.collection = collection
# for BibTeX interpreter
self.vars = {}
def __eq__(self, other):
if not isinstance(other, Entry):
return super(Entry, self) == other
return (
self.type == other.type
and self.fields == other.fields
and self.persons == other.persons
)
def __repr__(self):
return 'Entry({type_}, fields={fields}, persons={persons})'.format(
type_=repr(self.type),
fields=repr(self.fields),
persons=repr(self.persons),
)
def get_crossref(self):
return self.collection.entries[EntryKey.from_string(self.fields['crossref'].expand())]
def add_person(self, person, role):
self.persons.setdefault(role, []).append(person)
class Person(object):
"""Represents a person (usually human).
>>> p = Person('Avinash K. Dixit')
>>> print p.first()
['Avinash']
>>> print p.middle()
['K.']
>>> print p.prelast()
[]
>>> print p.last()
['Dixit']
>>> print p.lineage()
[]
>>> print str(p)
Dixit, Avinash K.
>>> p == Person(str(p))
True
>>> p = Person('Dixit, Jr, Avinash K. ')
>>> print p.first()
['Avinash']
>>> print p.middle()
['K.']
>>> print p.prelast()
[]
>>> print p.last()
['Dixit']
>>> print p.lineage()
['Jr']
>>> print str(p)
Dixit, Jr, Avinash K.
>>> p == Person(str(p))
True
>>> p = Person('abc')
>>> print p.first(), p.middle(), p.prelast(), p.last(), p.lineage()
[] [] [] ['abc'] []
>>> p = Person('Viktorov, Michail~Markovitch')
>>> print p.first(), p.middle(), p.prelast(), p.last(), p.lineage()
['Michail'] ['Markovitch'] [] ['Viktorov'] []
"""
valid_roles = ['author', 'editor']
style1_re = re.compile('^(.+),\\s*(.+)$')
style2_re = re.compile('^(.+),\\s*(.+),\\s*(.+)$')
def __init__(self, string="", first="", middle="", prelast="", last="", lineage=""):
self._first = []
self._middle = []
self._prelast = []
self._last = []
self._lineage = []
string = string.strip()
if string:
self.parse_string(string)
self._first.extend(split_tex_string(first))
self._middle.extend(split_tex_string(middle))
self._prelast.extend(split_tex_string(prelast))
self._last.extend(split_tex_string(last))
self._lineage.extend(split_tex_string(lineage))
def parse_string(self, name):
"""Extract various parts of the name from a string.
Supported formats are:
- von Last, First
- von Last, Jr, First
- First von Last
(see BibTeX manual for explanation)
"""
def process_first_middle(parts):
try:
self._first.append(parts[0])
self._middle.extend(parts[1:])
except IndexError:
pass
def process_von_last(parts):
von, last = rsplit_at(parts, lambda part: part.islower())
if von and not last:
last.append(von.pop())
self._prelast.extend(von)
self._last.extend(last)
def find_pos(lst, pred):
for i, item in enumerate(lst):
if pred(item):
return i
return i + 1
def split_at(lst, pred):
"""Split the given list into two parts.
The second part starts with the first item for which the given
predicate is True.
"""
pos = find_pos(lst, pred)
return lst[:pos], lst[pos:]
def rsplit_at(lst, pred):
rpos = find_pos(reversed(lst), pred)
pos = len(lst) - rpos
return lst[:pos], lst[pos:]
parts = split_tex_string(name, ',')
if len(parts) == 3: # von Last, Jr, First
process_von_last(split_tex_string(parts[0]))
self._lineage.extend(split_tex_string(parts[1]))
process_first_middle(split_tex_string(parts[2]))
elif len(parts) == 2: # von Last, First
process_von_last(split_tex_string(parts[0]))
process_first_middle(split_tex_string(parts[1]))
elif len(parts) == 1: # First von Last
parts = split_tex_string(name)
first_middle, von_last = split_at(parts, lambda part: part.islower())
if not von_last and first_middle:
last = first_middle.pop()
von_last.append(last)
process_first_middle(first_middle)
process_von_last(von_last)
else:
raise PybtexError('Invalid name format: %s' % name)
def __eq__(self, other):
if not isinstance(other, Person):
return super(Person, self) == other
return (
self._first == other._first
and self._middle == other._middle
and self._prelast == other._prelast
and self._last == other._last
and self._lineage == other._lineage
)
def __str__(self):
# von Last, Jr, First
von_last = ' '.join(self._prelast + self._last)
jr = ' '.join(self._lineage)
first = ' '.join(self._first + self._middle)
if jr or (not first and self._prelast):
return ', '.join(part for part in (von_last, jr, first) if part)
if first:
return first + ' ' + von_last
# Yes, there are people who only have 1 "name".
# Bibtex parses this as a lastname, although possibly,
# this person might only have a firstname and no lastname.
return von_last
def __repr__(self):
return f"Person({str(self)})"
def __hash__(self):
return hash(str(self))
def get_part_as_text(self, type):
names = getattr(self, '_' + type)
return ' '.join(names)
def get_part(self, type, abbr=False):
names = getattr(self, '_' + type)
if abbr:
from pybtex.textutils import abbreviate
names = [abbreviate(name) for name in names]
return names
#FIXME needs some thinking and cleanup
def bibtex_first(self):
"""Return first and middle names together.
(BibTeX treats all middle names as first)
"""
return self._first + self._middle
def first(self, abbr=False):
return self.get_part('first', abbr)
def middle(self, abbr=False):
return self.get_part('middle', abbr)
def prelast(self, abbr=False):
return self.get_part('prelast', abbr)
def last(self, abbr=False):
return self.get_part('last', abbr)
def lineage(self, abbr=False):
return self.get_part('lineage', abbr)
self.entries[key] = entry
try:
crossref = entry.fields['crossref'].expand()
except KeyError:
pass
else:
self.crossref_count[crossref] += 1
if self.crossref_count[crossref] >= self.min_crossrefs:
if self.wanted_entries is not None:
self.wanted_entries.add(crossref)
def add_entries(self, entries):
for key, entry in entries:
self.add_entry(key, entry)
class FieldDict(dict):
def __init__(self, parent, *args, **kwargw):
self.parent = parent
dict.__init__(self, *args, **kwargw)
def __missing__(self, key):
if key in self.parent.persons:
persons = self.parent.persons[key]
return ' and '.join(str(person) for person in persons)
elif 'crossref' in self:
return self.parent.get_crossref().fields[key]
else:
raise KeyError(key)
self.entries[key] = entry
def add_entries(self, entries):
for key, entry in entries:
self.add_entry(key, entry)
class EntryKeyParsingError(Exception):
def __init__(self, key):
self.key = key
message = 'Error while parsing key "{0}"'.format(key)
super(EntryKeyParsingError, self).__init__(message)
class EntryKey(object):
""" Model an entry key in our bibliographies """
def __init__(self, confkey, year, auth=None, dis=""):
self.confkey = confkey
""" conference abbreviation for key """
self.auth = auth
""" authors formatted as required in key (last name or first letters or ...) or None if this is a conference """
self.year = int(year) % 100
""" year of the conference (2 digits) """
self.dis = dis
""" disambiguation string ("", "a", "b", "-1", "-2", ...) if same conf, same authors and same year for a paper or if multiple volumes in one conf"""
_str_regexp = re.compile("^([a-zA-Z]+)(?::([a-zA-Z-_']+))?(\\d+)(.*)$")
@classmethod
def from_string(cls, s):
r = cls._str_regexp.match(s)
if r is None:
raise EntryKeyParsingError(s)
(confkey, auth, year, dis) = r.groups()
return cls(confkey, year, auth, dis)
def __str__(self):
if self.auth is None:
return "{0}{1:02d}{2}".format(self.confkey, self.year, self.dis)
else:
return "{0}:{1}{2:02d}{3}".format(self.confkey, self.auth, self.year, self.dis)
def __repr__(self):
return "EntryKey({0})".format(str(self))
def __hash__(self):
return str(self).__hash__()
def __eq__(self, other):
return (self.confkey == other.confkey and
self.auth == other.auth and
self.year == other.year and
self.dis == other.dis)
class Value(list):
""" Model a value in bibtex, i.e., a concatenation of value parts """
def expand(self):
return ''.join([value_part.expand() for value_part in self])
def __repr__(self):
return "Value({0})".format(repr(list(self)))
def to_bib(self, expand=False):
""" transform the value into a bib value; if expand=True, expand all macros EXCEPT month names """
return " # ".join([value_part.to_bib(expand=expand) for value_part in self])
class ValuePart(object):
""" Model a value part """
def __init__(self, val, normalize = True):
self.val = val if normalize == False else normalize_whitespace(val)
def __str__(self):
""" Return the original bibtex representation """
return self.val
def __repr__(self):
return repr(self.val)
def expand(self):
""" Return the expanded resulting string (after macro expansion, unquoting, ...) """
return self.val
def to_bib(self, expand=False):
return str(self)
class ValuePartNumber(ValuePart):
pass
class ValuePartQuote(ValuePart):
def __str__(self):
return '"{0}"'.format(self.val)
class ValuePartBrace(ValuePart):
def __str__(self):
return '{{{0}}}'.format(self.val)
class ValuePartMacro(ValuePart):
def __init__(self, macro_name, macro_val):
"""
@arg macro_val: value of the macro (type Value)
"""
self.macro_name = macro_name
self.macro_val = macro_val
def __str__(self):
return self.macro_name
def __repr__(self):
return 'Macro({0})'.format(self.macro_name)
def expand(self):
return self.macro_val.expand()
def to_bib(self, expand=False):
if expand==False or self.macro_name in month_names:
return self.macro_name
else:
return self.macro_val.to_bib(expand=expand)