This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
loaders.py
359 lines (293 loc) · 10.6 KB
/
loaders.py
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
# -*- coding: utf-8 -*-
"""
Bibliography Manager Tools
"""
from __future__ import unicode_literals
import datetime
import json
import logging
import sys
import bibtexparser
import eutils.client
from time import strptime
from bibtexparser import customization as bp_customization
from bibtexparser.bparser import BibTexParser
from bibtexparser.latexenc import string_to_latex
from django.utils.translation import ugettext_lazy as _
from habanero import cn
from ..exceptions import DOILoaderError, PMIDLoaderError
from ..models import Author, Journal, Entry, AuthorEntryRank
logger = logging.getLogger('td_biblio')
def to_latex(record):
"""
Convert strings to latex
"""
for val in record:
record[val] = string_to_latex(record[val])
return record
def td_biblio_customization(record):
"""
Customize BibTex records parsing
"""
# Convert crapy things to latex
record = to_latex(record)
# and then to unicode
record = bp_customization.convert_to_unicode(record)
record = bp_customization.type(record)
record = bp_customization.author(record)
record = bp_customization.editor(record)
record = bp_customization.page_double_hyphen(record)
return record
class BaseLoader(object):
def __init__(self):
self.entry_base_fields = (
'type', 'title', 'volume', 'number', 'pages', 'url',
'publication_date', 'is_partial_publication_date'
)
self.records = []
def to_record(self, input):
"""Convert an item to import to a valid record
valid_record = {
'title': 'A coarse-grained protein force field …',
'authors': [
{
'first_name': 'Julien',
'last_name': 'Maupetit'
},
{
'first_name': 'P',
'last_name': 'Tuffery'
},
{
'first_name': 'Philippe',
'last_name': 'Derreumaux'
}
],
'journal': 'Proteins: Structure, Function, and Bioinformatics',
'volume': '69',
'number': '2',
'pages': '394--408',
'year': '2007',
'publisher': 'Wiley Online Library',
'ENTRYTYPE': 'article',
'ID': 'maupetit2007coarse',
'publication_date': datetime.date(2007, 1, 1),
'is_partial_publication_date': True
}
"""
raise NotImplemented(
_(
"You should implement a to_record method for {}".format(
self.__class__.__name__
)
)
)
def load_records(self, **kwargs):
"""Load all records in self.records"""
raise NotImplemented(
_(
"You should implement a load_records method for {}".format(
self.__class__.__name__
)
)
)
def save_record(self, record):
"""Save a single record"""
logger.debug("Record: {}".format(record))
entry_fields = dict(
(k, v) for (k, v) in record.items() if k in self.entry_base_fields
)
# Foreign keys
journal, is_new = Journal.objects.get_or_create(
name=record['journal']
)
entry_fields['journal'] = journal
logger.debug("Journal: {}".format(journal))
# Save or Update this entry
entry, is_new = Entry.objects.get_or_create(**entry_fields)
# Authors
for rank, record_author in enumerate(record['authors']):
author, _ = Author.objects.get_or_create(
first_name=record_author['first_name'],
last_name=record_author['last_name'],
)
AuthorEntryRank.objects.get_or_create(
entry=entry,
author=author,
rank=rank,
)
logger.debug("(New) Entry imported with success: {}".format(entry))
def save_records(self):
"""Batch save records"""
for record in self.records:
self.save_record(record)
class BibTeXLoader(BaseLoader):
"""BibTeXLoader
This loader is designed to import a bibtex file items.
Usage:
>>> from td_biblio.utils.loaders import BibTeXLoader
>>> loader = BibTeXLoader()
>>> loader.load_records(bibtex_filename='foo.bib')
>>> loader.save_records()
"""
def to_record(self, input):
"""Convert a bibtex item to a valid record"""
# Simple fields
record = input.copy()
# Journal
record['journal'] = input['journal']
# Publication date
pub_date = {'day': 1, 'month': 1, 'year': 1900}
input_date = dict(
(k, v) for (k, v) in input.items() if k in pub_date.keys()
)
pub_date.update(input_date)
# Check if month is numerical or not
try:
int(pub_date['month'])
except ValueError:
pub_date['month'] = strptime(pub_date['month'], '%b').tm_mon
# Convert date fields to integers
pub_date = dict(
(k, int(v)) for k, v in pub_date.items()
)
record['publication_date'] = datetime.date(**pub_date)
record['is_partial_publication_date'] = not all(
[True if k in input else False for k in pub_date.keys()]
)
# Authors
record['authors'] = []
for author in input['author']:
splited = author.split(', ')
record['authors'].append(
{
'first_name': " ".join(splited[1:]),
'last_name': splited[0],
}
)
return record
def load_records(self, bibtex_filename=None):
"""Load all bibtex items as valid records"""
with open(bibtex_filename) as bibtex_file:
# Parse BibTex file
parser = BibTexParser()
parser.customization = td_biblio_customization
bp = bibtexparser.load(bibtex_file, parser=parser)
self.records = [self.to_record(r) for r in bp.get_entry_list()]
class PubmedLoader(BaseLoader):
"""PubmedLoader
This loader is designed to fetch & import a list of Pubmed IDs
Usage:
>>> from td_biblio.utils.loaders import PubmedLoader
>>> loader = PubmedLoader()
>>> loader.load_records(PMIDs=26588162)
>>> loader.save_records()
"""
def __init__(self, *args, **kwargs):
super(PubmedLoader, self).__init__(*args, **kwargs)
self.client = eutils.client.Client()
def to_record(self, input):
"""Convert eutils PubmedArticle xml facade to a valid record"""
record = {
'title': input.title,
'authors': [],
'journal': input.jrnl,
'volume': input.volume,
'number': input.issue if input.issue is not None else '',
'pages': input.pages,
'year': input.year,
'publication_date': datetime.date(int(input.year), 1, 1),
'is_partial_publication_date': True
}
for author in input.authors:
splited = author.split()
record['authors'].append(
{
'first_name': " ".join(splited[1:]),
'last_name': splited[0],
}
)
return record
def load_records(self, PMIDs=None):
"""Load all PMIDs as valid records"""
entries = self.client.efetch(db='pubmed', id=PMIDs)
self.records = []
for entry in entries:
try:
record = self.to_record(entry)
except Exception:
e, v, tb = sys.exc_info()
msg = _(
"An error occured while loading the following PMID: {}. "
"Check logs for details."
).format(
entry.pmid
)
logger.error(
'{}, error: {} [{}], data: {}'.format(msg, e, v, entry)
)
raise PMIDLoaderError(msg)
self.records.append(record)
class DOILoader(BaseLoader):
"""DOILoader
This loader is designed to fetch & import a list of DOIs
Usage:
>>> from td_biblio.utils.loaders import DOILoader
>>> loader = DOILoader()
>>> loader.load_records(DOIs='10.1021/ct500592m')
>>> loader.save_records()
"""
def to_record(self, input):
"""Convert crossref item to a valid record"""
date_parts = input['issued']['date-parts'][0]
is_partial_publication_date = len(date_parts) != 3
# Fill date parts if only a year is given
if is_partial_publication_date:
date_parts = date_parts + ((3 - len(date_parts)) * [1])
# Journal name defaults to 'short-container-title'
# It falls back to 'container-title'
if input.get('short-container-title'):
journal = input['short-container-title'][0]
else:
journal = input.get('container-title', '')
record = {
'title': input.get('title', ''),
'authors': [
{
'first_name': a.get('given', ''),
'last_name': a.get('family', '')
} for a in input.get('author')
],
'journal': journal,
'volume': input.get('volume', ''),
'number': input.get('issue', ''),
'pages': input.get('page', ''),
'year': date_parts[0],
'publication_date': datetime.date(*date_parts),
'is_partial_publication_date': is_partial_publication_date
}
return record
def load_records(self, DOIs=None):
"""Load all crossref items as valid records"""
records = cn.content_negotiation(ids=DOIs, format='citeproc-json')
# Records might be a str or unicode (python 2)
if not isinstance(records, list):
records = [records, ]
self.records = []
for r in records:
data = json.loads(r)
try:
record = self.to_record(data)
except Exception:
e, v, tb = sys.exc_info()
msg = _(
"An error occured while loading the following DOI: {}. "
"Check logs for details."
).format(
data.get('DOI')
)
logger.error(
'{}, error: {} [{}], data: {}'.format(msg, e, v, data)
)
raise DOILoaderError(msg)
self.records.append(record)