-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixy.py
412 lines (296 loc) · 11.7 KB
/
fixy.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
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
from os import name
from lxml import etree
NS = {"cdm": "http://www.oclc.org/contentdm", "mods": "http://www.loc.gov/mods/v3"}
# Attributes
def add_attribute_curator_name_value_uri(mods):
global NS
xpath = "//mods:name[mods:role/mods:roleTerm/text()='curator' or mods:role/mods:roleTerm/text()='Contributing Institution']"
value_uri = "http://id.loc.gov/authorities/names/no2021021777"
curator = mods.xpath(xpath, namespaces=NS)[0]
curator.attrib["valueURI"] = value_uri
return mods
def add_attribute_finding_aid_identifier_type(mods): # NOT WORKING?
global NS
xpath = "//mods:relatedItem[@displayLabel='Collection']/mods:identifier[not(@*)]"
identifier = mods.xpath(xpath, namespaces=NS)[0]
identifier.attrib["type"] = "ark"
return mods
def add_update_attribute_roleTerm_type_text(mods):
global NS
xpath = "//mods:name/mods:role/mods:roleTerm"
roleTerms = mods.xpath(xpath, namespaces=NS)
for r in roleTerms:
r.attrib["type"] = "text"
return mods
def update_attribute_curator_authority_to_naf(mods):
global NS
xpath = "//mods:name[mods:role/mods:roleTerm/text() = 'curator']"
curator = mods.xpath(xpath, namespaces=NS)[0]
curator.attrib["authority"] = "naf"
return mods
def update_attribute_collection_relatedItem_type_to_original(mods):
global NS
xpath = "//mods:relatedItem[@displayLabel='Collection']"
collection = mods.xpath(xpath, namespaces=NS)[0]
collection.attrib["type"] = "original"
return mods
def update_attribute_language_type_to_code(mods):
global NS
xpath = "//mods:language/mods:languageTerm"
language_term = mods.xpath(xpath, namespaces=NS)[0]
language_term.attrib["type"] = "code"
# Elements
def add_element_box(mods, cdm):
global NS
mods_xpath = "//mods:titleInfo[@displayLabel='Box']"
cdm_xpath = "string(//cdm:box/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
box_number = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(etree.fromstring(f"<title>{box_number}</title>\n"))
return mods
def add_element_date_digital(mods, cdm):
global NS
mods_xpath = "//mods:originInfo"
cdm_xpath = "string(//cdm:date-digital/text())"
originInfo = mods.xpath(mods_xpath, namespaces=NS)[0]
date_digital = cdm.xpath(cdm_xpath, namespaces=NS)
originInfo.append(
etree.fromstring(
f"<dateCaptured encoding='iso8601'>{date_digital}</dateCaptured>\n"
)
)
return mods
def add_element_folder(mods, cdm):
global NS
mods_xpath = "//mods:titleInfo[@displayLabel='Folder']"
cdm_xpath = "string(//cdm:folder/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
folder_number = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(etree.fromstring(f"<title>{folder_number}</title>\n"))
return mods
def add_element_folder_title(mods, cdm):
global NS
mods_xpath = "//mods:titleInfo[@displayLabel='Folder']"
cdm_xpath = "string(//cdm:folder-title/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
folder_title = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(etree.fromstring(f"<title>{folder_title}</title>\n"))
return mods
def add_element_genre_aat(mods, cdm):
global NS
mods_xpath = "//mods:mods"
cdm_xpath = "string(//cdm:aat-type/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
genre = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(etree.fromstring(f'<genre authority="aat">{genre}</genre>\n'))
return mods
def add_element_note_hardware_software(mods, cdm):
global NS
mods_xpath = "//mods:mods"
cdm_xpath = "string(//cdm:hardware-software/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
hardware_software = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(
etree.fromstring(f'<note type="hardware/software">{hardware_software}</note>\n')
)
return mods
def add_element_record_created(mods, cdm):
global NS
mods_xpath = "//mods:recordInfo"
cdm_xpath = "string(//cdm:date-created/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
record_created = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(
etree.fromstring(
f'<recordCreationDate encoding="iso8601">{record_created}</recordCreationDate>\n'
)
)
return mods
def add_element_record_modified(mods, cdm):
global NS
mods_xpath = "//mods:recordInfo"
cdm_xpath = "string(//cdm:date-modified/text())"
fix_me = mods.xpath(mods_xpath, namespaces=NS)[0]
record_created = cdm.xpath(cdm_xpath, namespaces=NS)
fix_me.append(
etree.fromstring(
f'<recordChangeDate encoding="iso8601">{record_created}</recordChangeDate>\n'
)
)
return mods
def add_element_local_id_from_ark(mods):
global NS
xpath = "//mods:mods"
ark_xpath = "string(/mods:mods/mods:identifier[@type='ark'])"
local_id = mods.xpath(ark_xpath, namespaces=NS)
fix_me = mods.xpath(xpath, namespaces=NS)[0]
fix_me.append(
etree.fromstring(f'<identifier type="local">{local_id}</identifier>\n')
)
return mods
def add_element_local_id_noncdm(mods, local_id):
global NS
xpath = "//mods:mods"
fix_me = mods.xpath(xpath, namespaces=NS)[0]
fix_me.append(
etree.fromstring(f'<identifier type="local">{local_id}</identifier>\n')
)
return mods
def remove_element_identifier_if_empty(mods):
global NS
xpath = "//mods:identifier[not(@*) and not(text())]"
empty_identifiers = mods.xpath(xpath, namespaces=NS)
if len(empty_identifiers) > 0:
for i in empty_identifiers:
i.getparent().remove(i)
return mods
def update_element_ark(mods, new_ark):
global NS
xpath = "/mods:mods/mods:identifier[@type='ark']"
ark_elem = mods.xpath(xpath, namespaces=NS)[0]
ark_elem.text = new_ark
return mods
def update_element_digital_collection_ark(mods, new_ark):
global NS
xpath = "//mods:relatedItem[@displayLabel='Digital Collection']/mods:identifier[@type='ark']"
ark_elem = mods.xpath(xpath, namespaces=NS)[0]
ark_elem.text = new_ark
return mods
def update_element_finding_aid_ark(mods, new_ark):
global NS
xpath = (
"//mods:relatedItem[@displayLabel='Collection']/mods:identifier[@type='ark']"
)
ark_elem = mods.xpath(xpath, namespaces=NS)[0]
ark_elem.text = new_ark
return mods
def update_element_curator_to_curator(mods):
global NS
xpath = "//mods:roleTerm[text()='Contributing Institution']"
curator = mods.xpath(xpath, namespaces=NS)[0]
curator.text = "curator"
return mods
def update_element_curator_name_to_naf(mods): # NOT WORKING?
global NS
xpath = "//mods:name/mods:namePart[following-sibling::mods:role/mods:roleTerm/text()='curator' or following-sibling::mods:role/mods:roleTerm/text()='Contributing Institution']"
curator = mods.xpath(xpath, namespaces=NS)[0]
curator.text = "Iowa State University. Special Collections and University Archives"
return mods
def update_element_genre_imt_to_image_tiff(mods):
global NS
xpath = "//mods:genre[@authority='imt']"
imt_type = mods.xpath(xpath, namespaces=NS)[0]
imt_type.text = "image/tiff"
return mods
def update_element_local_id_from_filename(mods):
global NS
filename_xpath = "string(/mods:mods/mods:identifier[not(@*)])"
local_id_xpath = "/mods:mods/mods:identifier[@type='local']"
filename = mods.xpath(filename_xpath)
local_id = mods.xpath(local_id_xpath)
local_id.text = filename.split(".")[0]
def update_element_physical_collection_remove_url(mods):
global NS
xpath = "//mods:relatedItem[@displayLabel='Collection']/mods:titleInfo/mods:title"
collection_name = mods.xpath(xpath, namespaces=NS)[0]
try:
collection_name.text = collection_name.text.split(",")[0].strip()
except AttributeError:
collection_name.text = ""
return mods
def update_element_topic_split_on_semicolon(mods):
global NS
subjects_xpath = "//mods:subject[mods:topic]"
subjects = mods.xpath(subjects_xpath, namespaces=NS)
for s in subjects:
topics = s.xpath("mods:topic", namespaces=NS)
for t in topics:
if t.text is not None:
if ";" in t.text:
ts = [s.strip() for s in t.text.split(";")]
t.text = ts[0]
for tt in ts[1:]:
s.append(etree.fromstring(f"<topic>{tt}</topic>\n"))
return mods
def update_element_namePart_split_on_semicolon(mods):
global NS
xpath = "//mods:subject[mods:name]"
name_subjects = mods.xpath(xpath, namespaces=NS)
for s in name_subjects:
name_type = s.xpath("string(mods:name/@type)", namespaces=NS)
name_parts = s.xpath("mods:name/mods:namePart", namespaces=NS)
for np in name_parts:
if np.text is not None:
if ";" in np.text:
ns = [n.strip() for n in np.text.split(";")]
np.text = ns[0]
for nn in ns[1:]:
s.append(
etree.fromstring(
f' <name type="{name_type}">\n <namePart>{nn}</namePart></name>\n'
)
)
return mods
def update_element_geographic_split_on_semicolon(mods):
global NS
xpath = "//mods:subject[mods:geographic]"
subjects = mods.xpath(xpath, namespaces=NS)
for s in subjects:
geographic = s.xpath("mods:geographic", namespaces=NS)
for g in geographic:
if g.text is not None:
if ";" in g.text:
gs = [s.strip() for s in g.text.split(";")]
g.text = gs[0]
for gg in gs[1:]:
s.append(etree.fromstring(f"<geographic>{gg}</geographic>\n"))
return mods
def update_element_temporal_split_on_semicolon(mods):
global NS
xpath = "//mods:subject[mods:temporal]"
subjects = mods.xpath(xpath, namespaces=NS)
for s in subjects:
temporal = s.xpath("mods:temporal", namespaces=NS)
for t in temporal:
if t.text is not None:
if ";" in t.text:
ts = [s.strip() for s in t.text.split(";")]
t.text = ts[0]
for tt in ts[1:]:
s.append(etree.fromstring(f"<temporal>{tt}</temporal>\n"))
# Multi
def universal_changes(mods):
add_update_attribute_roleTerm_type_text(mods)
update_attribute_language_type_to_code(mods)
update_attribute_collection_relatedItem_type_to_original(mods)
update_element_curator_name_to_naf(mods)
add_attribute_curator_name_value_uri(mods)
update_element_physical_collection_remove_url(mods)
remove_element_identifier_if_empty(mods)
# File handling
def load_cdm(pid):
try:
return etree.parse(f"contentdm_data_stream/cdm-{pid}.xml")
except OSError:
try:
return etree.parse(f"cdm-{pid}.xml")
except OSError:
return etree.parse(f"{pid}.xml")
def load_mods(pid):
return etree.parse(f"{pid}.xml")
def load_pids(pid_file):
with open(pid_file, "r", encoding="utf8") as fh:
pids = [p.strip() for p in fh if not p.startswith("#")]
return pids
def save_mods(mods, pid):
out_xml = etree.tostring(
mods, xml_declaration=True, encoding="UTF-8", pretty_print=True
).decode("utf-8")
with open(f"{pid}.xml", "w", encoding="utf8") as fh:
fh.write(out_xml)
def save_dc(dc, pid):
out_xml = etree.tostring(
dc, xml_declaration=True, encoding="UTF-8", pretty_print=True
).decode("utf-8")
with open(f"dc-{pid}.xml", "w", encoding="utf8") as fh:
fh.write(out_xml)