This repository has been archived by the owner on May 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
290 lines (240 loc) · 7.69 KB
/
model.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
# -*- coding: utf-8 -*-
#
# This file is part of BiblioMedia-Data
# Copyright (C) 2016 RERO.
#
# BiblioMedia-Data is free software; you can redistribute it and/or
# modify it under the terms of the Revised BSD License; see LICENSE
# file for more details.
"""Bibliomedia UNIMARC model definition."""
import re
from json import loads
from dojson import Overdo, utils
# from dojson.utils import force_list
from pkg_resources import resource_string
unimarctojson = Overdo()
# @unimarctojson.over('__order__', '__order__')
# def order(self, key, value):
# """Preserve order of datafields."""
# order = []
# for field in value:
# name = unimarctojson.index.query(field)
# if name:
# name = name[0]
# else:
# name = field
# order.append(name)
#
# return order
@unimarctojson.over('identifiers', '^003')
@utils.ignore_value
def unimarcbnfid(self, key, value):
"""Get ID.
identifier bnfID 003
"""
values = value.split('/')
identifiers = self.get('identifiers', {})
identifiers['bnfID'] = values[-1]
return identifiers
@unimarctojson.over('title', '^200..')
@utils.ignore_value
def unimarctitle(self, key, value):
"""Get title.
title: 200$a
If there's a $e, then 245$a : $e
"""
main_title = value.get('a')
sub_title = value.get('e')
# responsability = value.get('c')
if sub_title:
main_title += ' : ' + ' : '.join(
utils.force_list(sub_title)
)
return main_title
@unimarctojson.over('titlesProper', '^500..')
@utils.for_each_value
@utils.ignore_value
def unimarctitlesProper(self, key, value):
"""Test dojson unimarctitlesProper.
titleProper: 500$a
"""
return value.get('a')
@unimarctojson.over('languages', '^101')
@utils.ignore_value
def unimarclanguages(self, key, value):
"""Get languages.
languages: 008 and 041 [$a, repetitive]
"""
languages = utils.force_list(value.get('a'))
to_return = []
schema_in_bytes = resource_string('reroils_data.documents.jsonschemas',
'documents/book-v0.0.1.json')
schema = loads(schema_in_bytes.decode('utf8'))
langs = schema[
'properties']['languages']['items']['properties']['language']['enum']
for language in languages:
if language in langs:
to_return.append({'language': language})
translatedsfrom = utils.force_list(value.get('c'))
if translatedsfrom:
self['translatedFrom'] = []
for translatedfrom in translatedsfrom:
self['translatedFrom'].append(translatedfrom)
return to_return
@unimarctojson.over('authors', '7[01][012]..')
@utils.for_each_value
@utils.ignore_value
def unimarctoauthor(self, key, value):
"""Get author.
authors: loop:
700 Nom de personne – Responsabilité principale
701 Nom de personne – Autre responsabilité principale
702 Nom de personne – Responsabilité secondaire
710 Nom de collectivité – Responsabilité principale
711 Nom de collectivité – Autre responsabilité principale
712 Nom de collectivité – Responsabilité secondaire
"""
author = {}
author['name'] = value.get('a')
author['type'] = 'person'
if key[1] == '1':
author['type'] = 'organisation'
if value.get('b'):
author['name'] += ', ' + value.get('b')
if value.get('d'):
author['name'] += ' ' + value.get('d')
if value.get('c'):
author['qualifier'] = value.get('c')
if value.get('f'):
date = value.get('f')
date = date.replace('-....', '-')
author['date'] = date
return author
@unimarctojson.over('publishers', '^210..')
@utils.ignore_value
def unimarcpublishers_publicationDate(self, key, value):
"""Get publisher.
publisher.name: 210 [$b repetitive]
publisher.place: 210 [$a repetitive]
publicationDate: 210 [$c repetitive] (take only the first one)
"""
lasttag = '?'
publishers = self.get('publishers', [])
publisher = {}
indexes = {}
lasttag = '?'
for tag in value['__order__']:
index = indexes.get(tag, 0)
data = value[tag]
if type(data) == tuple:
data = data[index]
if tag == 'a' and index > 0 and lasttag != 'a':
publishers.append(publisher)
publisher = {}
if tag == 'a':
place = publisher.get('place', [])
place.append(data)
publisher['place'] = place
elif tag == 'c':
name = publisher.get('name', [])
name.append(data)
publisher['name'] = name
elif tag == 'd' and index == 0:
# 4 digits
date = re.match(r'.*?(\d{4})', data).group(1)
self['publicationYear'] = int(date)
# create free form if different
if data != str(self['publicationYear']):
self['freeFormedPublicationDate'] = data
indexes[tag] = index + 1
lasttag = tag
publishers.append(publisher)
return publishers
@unimarctojson.over('formats', '^215..')
@utils.ignore_value
def unimarcdescription(self, key, value):
"""Get extent, otherMaterialCharacteristics, formats.
extent: 215$a (the first one if many)
otherMaterialCharacteristics: 215$b (the first one if many)
formats: 215 [$c repetitive]
"""
if value.get('a'):
if not self.get('extent', None):
self['extent'] = (
utils.force_list(value.get('a'))[0]
)
if value.get('c'):
if self.get('otherMaterialCharacteristics', []) == []:
self['otherMaterialCharacteristics'] = (
utils.force_list(value.get('c'))[0]
)
if value.get('d'):
formats = self.get('formats', None)
if not formats:
data = value.get('d')
formats = list(utils.force_list(data))
return formats
else:
return None
@unimarctojson.over('series', '^225..')
@utils.for_each_value
@utils.ignore_value
def unimarcseries(self, key, value):
"""Get series.
series.name: [225$a repetitive]
series.number: [225$v repetitive]
"""
series = {}
name = value.get('a')
if name:
series['name'] = ', '.join(utils.force_list(name))
number = value.get('v')
if number:
series['number'] = ', '.join(utils.force_list(number))
return series
@unimarctojson.over('abstracts', '^330..')
@utils.for_each_value
@utils.ignore_value
def unimarcabstracts(self, key, value):
"""Get abstracts.
abstract: [330$a repetitive]
"""
return ', '.join(utils.force_list(value.get('a')))
@unimarctojson.over('identifiers', '^073..')
@utils.ignore_value
def unimarcidentifier_isbn(self, key, value):
"""Get identifier isbn.
identifiers:isbn: 010$a
"""
identifiers = self.get('identifiers', {})
if value.get('a'):
identifiers['isbn'] = value.get('a')
return identifiers
@unimarctojson.over('notes', '^300..')
@utils.for_each_value
@utils.ignore_value
def unimarcnotes(self, key, value):
"""Get notes.
note: [300$a repetitive]
"""
return value.get('a')
@unimarctojson.over('subjects', '^6((0[0-9])|(1[0-7]))..')
@utils.for_each_value
@utils.ignore_value
def unimarcsubjects(self, key, value):
"""Get subjects.
subjects: 6xx [duplicates could exist between several vocabularies,
if possible deduplicate]
"""
to_return = ''
if value.get('a'):
to_return = value.get('a')
if value.get('b'):
to_return += ', ' + value.get('b')
if value.get('d'):
to_return += ' ' + value.get('d')
if value.get('c'):
to_return += ', ' + value.get('c')
if value.get('f'):
to_return += ', ' + value.get('f')
return to_return